Showing posts with label category. Show all posts
Showing posts with label category. Show all posts

Thursday, 8 October 2015

How to get category tree structure in Magento programatically ?

In many places we will need to get the category tree in magento programmatically. With the below peace of code you can get the tree anywhere in magento



   $rootCatId = Mage::app()->getStore()->getRootCategoryId(); // it will fetch your current store root id
   $catlistHtml = getTreeCategories($rootCatId, false);
   echo $catlistHtml;

function getTreeCategories($parentId, $isChild){
    $allCats = Mage::getModel('catalog/category')->getCollection()
                ->addAttributeToSelect('*')
                ->addAttributeToFilter('is_active','1')  // is acategory active
                ->addAttributeToFilter('include_in_menu','1') //  to check include_in_menu yes or no
                ->addAttributeToFilter('parent_id',array('eq' => $parentId))  // filter parent id
                ->addAttributeToSort('position', 'asc');
              
    $class = ($isChild) ? "sub-cat-list" : "cat-list";
    $html .= '<ul class="'.$class.'">';
    foreach($allCats as $category)
    {
        $html .= '<li><span>'.$category->getName()."</span>";
        $subcats = $category->getChildren();
        if($subcats != ''){
            $html .= getTreeCategories($category->getId(), true);
        }
        $html .= '</li>';
    }
    $html .= '</ul>';
    return $html;
}


Please let me know if there is other such easy way to do this.I will love to hear and update my post.

thanks

Wednesday, 7 October 2015

How to get products of particular category in Magento ?

Hi it is very easy to get products of particular category in magento.
Check the below peace of code , with this you can get the products of your desored category


$products =
              Mage::getModel('catalog/category')->load('put your category id here')
            ->getProductCollection()
            ->addAttributeToSelect('*') ;
          

                foreach($products as $productsss){
                                  print_r($productsss->getData());
                    }


you can also add the filters in the above collection to get your desired products
like if you want to get only enabled products in the particular category like

$products = Mage::getModel('catalog/category')->load($catid)
            ->getProductCollection()
            ->addAttributeToSelect('*')
            ->addAttributeToFilter('status', 1) ;


with this only products enabled will be show in list. You can add as many as filters you want.

Hope it will help somebody.

Thanks


Monday, 25 May 2015

Copy products from one category to another in Magento

Hi sometime we need to copy the products from one caegory to another in magento , but by default there is no such option like this in magento. So we can do this programatically with the below code




<?php
$category = Mage::getModel('catalog/category');
$category->load('2'); // Preset category id
$collection = $category->getProductCollection();
$collection->addAttributeToSelect('*');
foreach ($collection as $product) {
$product->getId();// Now get category ids and add a specific category to them and save?
$categories = $product->getCategoryIds();
$categories[] = 3; // Category to add
$product->setCategoryIds($categories);
$product->save();

}

Thursday, 26 December 2013

Get products from particular category Magento

 <?php $categoryIds = array(3,4);//category id

                                    $collection = Mage::getModel('catalog/product')
                                    ->getCollection()
                                    ->joinField('category_id', 'catalog/category_product', 'category_id', 'product_id = entity_id', null, 'left')
                                    ->addAttributeToSelect('*')
                                    ->addAttributeToFilter('category_id', array('in' => $categoryIds))
                    ?>
                   
                    <?php $_collectionSize = $collection->count() ?>
    <?php //$_columnCount = $this->getColumnCount(); ?>
    <?php $i=0; foreach ($collection as $product): ?>
        <?php if ($i++%4==0): ?>
        <ul class="products-grid">
        <?php endif ?>
            <li class="item<?php if(($i-1)%$_columnCount==0): ?> first<?php elseif($i%$_columnCount==0): ?> last<?php endif; ?>">
                <a href="<?php echo $product->getProductUrl()?>" title="<?php echo $product->getName()?>">
                        <img src="<?php echo Mage::helper('catalog/image')->init($product, 'small_image')->resize(197, 167); ?>" alt="<?php echo $product->getName()?>" border="0" />
                    </a>
                <h2 class="product-name"><a href="<?php echo $product->getProductUrl()?>" title="<?php echo $product->getName()?>"><?php echo $product->getName() ?></a></h2>
               <div class="price-box">
               <?php echo Mage::helper('core')->currency($product->getPrice(),true,false);?>
               </div>
                <div class="actions">
                    <?php if($product->isSaleable()): ?>
                        <button class="button" onclick="setLocation('<?php echo Mage::getUrl('checkout/cart/add/')?>product/<?php echo $product->getId() ?>/')" title="<?php echo $this->__('Köp');?>" type="submit"><span><span><?php echo $this->__('Köp');?></span></span></button>
                    <?php else: ?>
                        <p class="availability out-of-stock"><span><?php echo $this->__('Out of stock') ?></span></p>
                    <?php endif; ?>

                </div>
            </li>
        <?php if ($i%$_columnCount==0 || $i==$_collectionSize): ?>
        </ul>
        <?php endif ?>
        <?php endforeach ?>

Thursday, 14 November 2013

check current page is category page or another page in Magento

 
Hi In magento sometime we need to place conditions on content on the basis of page type .
For  that the best way to find the current page type is to check the controller of that page,
Below you can see how we can do this


  <?php $category = Mage::app()->getFrontController()->getRequest()->getControllerName(); // gives current controller name for eg "cart", "category"

you can check it by
 echo  $category ;  and than on the basis of that you can place your condition like below

in this i have check two controllers cart and or category thats why i have use array to check there values .
    $catpage = array("cart", "category");
if(in_array($category, $catpage)){    ;?>       

        <?php } else { ?>

<?php }?>

highlight current category link in magento

Hi with the below peace of code you can highlight the current category link. In this we get first get the current url and than match it with the category url and on the basis of that we will apply our classes  


 <?php $curr =  "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; //full current url
                                    $currcate =  $category->getURL(); // your category url (i have use this  on product list page )
                                    if ($curr == $currcate) {
                                    $class = "active";}else {

                                    $class="nonactive";}
                                    //echo $class;

                                    ?>
                            
on the bais of above match we will apply alss in the anchor tag as below
<a class="<?php echo $class; ?> " href="<?php echo $category->getURL()?>">




Wednesday, 9 January 2013

code to show category images on custom phtml files and link to their category pages

<?php

 $root_category = Mage::getModel('catalog/category')->load(2); // Put your root category ID here. like 2
$subcategories = $root_category->getChildren();
foreach(explode(',',$subcategories) as $subcategory) {
$category = Mage::getModel('catalog/category')->load($subcategory);
echo'<pre>';
//print_r($category);
echo  $category->getImageUrl(); //place this url in your image tag to show the image

  }

  ?>