Kriuchko
1/9/2019 - 9:01 AM

*Category*

Magento 1 category data


\!h ----- get current category from Mage:registry

$currentcategory = Mage::registry('current_category');
if(!empty($currentcategory )){ $currentcategory = Mage::getModel('catalog/category')->load($currentcategory->getId()); }

\!h ----- current category link

<?php
$currentCategory = Mage::registry('current_category');
echo $currentCategory->getUrl();
?>

\!h ----- general category data
echo $currentcategory->getName();
echo $currentcategory->getId();
echo $currentcategory->getImageUrl();
echo $currentcategory->getUrl();
    
    
\!h ----- Category Static block

<?php $currentcategory = Mage::registry('current_category'); ?>
<?php echo '<div class="info-block">'.$this->getLayout()->createBlock('cms/block')->setBlockId($currentcategory->getLandingPage())->toHtml().'</div>' ?> 

$_productCollection->getPageSize(); - получает количество постов на страницу на текущей категории

category image category thumbnail

—— in version 1.9.1 /app/etc/modules/Mage_XmlConnect.xml <active>true</active>

<?php $cat = Mage::getModel('catalog/category')->load($_category->getId()); ?>
<?php echo Mage::getBaseUrl('media').'catalog/category/'.$cat->getThumbnail(); ?>
<?php echo $cat->getImageUrl(); ?>

<?php $cat = Mage::getModel('catalog/category')->load($children->getId()); ?>
<?php $thumbnail = $cat->getThumbnail(); echo Mage::getBaseUrl('media').'catalog/category/'.$cat->getThumbnail(); ?>
<?php echo echo Mage::getBaseUrl('media').'catalog/category/'.$thumbnail; ?>

\!h -----  category image resize category image

if($imageUrl)
{                                    
  $imageName = substr(strrchr($imageUrl,"/"),1);
  $imageResized = Mage::getBaseDir('media').DS."catalog".DS."category".DS."resized".DS.$imageName;                                
  $dirImg = Mage::getBaseDir().str_replace("/",DS,strstr($imageUrl,'/media'));                                
  if (!file_exists($imageResized)&&file_exists($dirImg)) :
      $imageObj = new Varien_Image($dirImg);
      $imageObj->constrainOnly(TRUE);
      $imageObj->keepAspectRatio(TRUE);
      $imageObj->keepFrame(FALSE);
      $imageObj->resize(225, 102);
      $imageObj->save($imageResized);
  endif;                                
  $resized_img_url = Mage::getBaseUrl('media')."catalog/category/resized/".$imageName;                                    
}

------------------------------------

$width = '1126';
$height = '550';

if ($_category->getImage())
{
    $imagePath = Mage::getBaseDir('media') . DS . 'catalog' . DS . 'category'. DS . $_category->getImage();
    if (file_exists($imagePath))
    {
        $rszImagePath = Mage::getBaseDir('media') . DS . 'catalog' . DS . 'category'
                      . DS . 'cache' . DS . $width . 'x' . $height . DS
                      . $_category->getImage();
        if (!file_exists($rszImagePath)) {
            $image = new Varien_Image($imagePath);
            $image->resize($width, $height);
            $image->save($rszImagePath);
        }
        $_imgUrl = Mage::getBaseUrl('media') . '/catalog/category/cache/' . $width . 'x'. $height . '/' . $_category->getImage();
    }
}

\!h ----- Loading a Category by an Attribute

<?php
 
  // Instantiate a category collection object
  $categories = Mage::getResourceModel('catalog/categories_collection');
   
  // Select which fields to load into the category
  // * will load all fields but it is possible to pass an array of
  // select fields to load
  $categories->addAttributeToSelect('*');
   
  // Ensure the category is active
  $categories->addAttributeToFilter('is_active', 1);
   
  // Add Name filter
  $categories->addAttributeToFilter('name', 'My Category Name');

  // Limit the collection to 1 result
  $categories->setCurPage(1)->setPageSize(1);
   
  // Load the collection
  $categories->load();
   
  if ($categories->getFirstItem()) {
      $category = $categories->getFirstItem();
       
      echo $category->getName();
  }
  else {
      echo 'No category exists with the name ' . $name;
  }