Browsing articles tagged with "category Archives - Kalpesh Mehta"
Mar 23, 2015
kalpesh

Magento: Change canonical URL for particular category only

In Magento, if you want to change canonical URL for just one category or quite a few categories, and don’t want to affect rest of the canonical URLs then do the following.

Note that this is helpful when you already have canonical URL in the page and want to REPLACE it with new url. If you don’t have canonical URL at all then you might want to ignore the first action tag in the below code.

- Go to the category page you want to change canonical URL in Magento Admin
- Click the tab “Custom Design”
- In the Custom Layout Update textbox, paste this:

1
2
3
4
5
6
7
8
9
10
<reference name="head">
    <action method="removeItem" block="head">
        <item>link_rel</item>
        <name>http://loca.lho.st/old-canonical-url</name>
    </action>
    <action method="addLinkRel" translate="title">
        <rel>canonical</rel>
        <href>http://loca.lho.st/new-canonical-url</href>
    </action>
</reference>

Change http://loca.lho.st/old-canonical-url and http://loca.lho.st/new-canonical-url with your desired URLs and save the category.

Basically first action tag in the code removes old canonical URL and second action tag adds the canonical URL with the new value you specify.

You may also need to clear cache.

HTH!

Nov 6, 2014
kalpesh

Magento: Get all category and subcategory products

Magento get all category and subcategory products which are assigned to categories at different levels. Below script will show you all the categories, and all the associated product names under each of those categories.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<?php
require_once('app/Mage.php');
umask(0);
Mage::app('admin');
set_time_limit(0);

$category = Mage::getModel('catalog/category');
$tree = $category->getTreeModel();
$tree->load();

$ids = $tree->getCollection()->getAllIds();

if ($ids)
{
     foreach ($ids as $id)
  {
     $cat = Mage::getModel('catalog/category');
     $cat->load($id);
     if($cat->getLevel()==3 && $cat->getIsActive()==1)
     {
        $category1 = Mage::getModel('catalog/category')->load($cat->getId());
        $products = Mage::getResourceModel('catalog/product_collection')
                                ->addAttributeToSelect('name')
                             ->addCategoryFilter($category1);
        echo "<b>".$cat->getName()."</b><br>";
        foreach ($products as $product) { //print_r($product->getData());exit;
                echo " &nbsp; &nbsp; &nbsp; " . $product->getName() . " - ". $product->getSku() . "<br/>";
        }
     }
  }
}
?>

Note the line which checks category getLevel()==3, you can change this line to get different subcategory levels by adjusting it.
For root category, getLevel() should be 1.
For all the main/primary categories, getLevel() should be 2.
For all the subcategories, getLevel() should be 3.
For all the subsubcategories, getLevel() should be 4.
and so on…

Above script will get you all the category and subcategories products, products assigned to each and every categories of your store.

Jan 5, 2014
kalpesh

Magento display categories and sub-categories

Magento display categories and sub-categories. Below code will show all the parent and child categories along with show/hide functionalities by jQuery.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<div class="block block-layered-nav">
    <div class="block-title">
        <strong><span><?php echo $this->__('Shop By Category') ?></span></strong>
    </div>

	<div class="block-content">
        <?php $productid = Mage::registry('current_product')->getId();
        $product = Mage::getSingleton('catalog/product')->load($productid);
        $parentIds = $product->getCategoryIds();
        $parentId = $parentIds[0];
        $_categories = Mage::getBlockSingleton('catalog/navigation');
        foreach ($_categories->getStoreCategories() as $_category) {
                $category = Mage::getModel('catalog/category');
                $category->load($_category->getId());
                $subcategories = explode(',', $category->getChildren());
                if(!in_array($_category->getId(),$parentIds)) { $hide="display:none"; $inactive="inactive"; } else { $hide=""; $inactive="active"; }
                ?>
                <div style="padding:5px 5px 0px 10px">
                        <div class="cat parent <?php echo $inactive;?>" style="font-size:15px"><?php echo $_category->getName() ?></div>
                        <div class="child" style="<?php echo $hide;?>">
                         <?php foreach ($subcategories as $subcategoryId) {
                                $category->load($subcategoryId);
                                if($category->getChildren() == '') {
                                        if(in_array($subcategoryId,$parentIds)) { $bold = "font-weight:bold"; } else { $bold = ""; }
                                      echo '<div class="subcat" style="'.$bold.'"><a href="' . $category->getURL() . '">' . $category->getName() . '</a></div>';
                                 } else {?>
                                <div class="subcat">
                                        <div class="parent active"><?php echo $category->getName() ?></div>
                                        <div class="child">
                                        <?php $subsubcategories = explode(',', $category->getChildren());
                                        foreach($subsubcategories as $subsubcatid) {
                                                if(in_array($subsubcatid, $parentIds)) { $bold = "font-weight:bold"; } else { $bold = ""; }
                                                $category->load($subsubcatid);
                                                echo '<div style="padding-left:10px;'.$bold.'"><a href="' . $category->getURL() . '">' . $category->getName() . '</a></div>';
                                        } ?>
                                        </div>
                                </div>
                        <?php }
                        }
                        ?>
                        </div>
                </div>
                <?php

        } ?>
    </div>
</div>

<script type="text/javascript">
	jQuery('.block-content .cat').click(function(){
        var t = jQuery(this);
        if(jQuery(this).next().css('display')=='none') {
                jQuery('.col-left .block-content .child').hide();
                t.next().show(); t.next().find('div.child').show("slow");
        } else {
                t.next().hide(); t.next().find('div.child').hide("slow");
        }
	});
</script>
Aug 17, 2013
kalpesh

Magento delete empty categories and sub-categories

Remove all empty categories and sub-categories in Magento. When there are empty categories, the website shows empty page in those categories in frontend. Create a file in the magento root, I will name it rmvEmptyCats.php, with following code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
require "app/Mage.php";
umask(0);
Mage::app();

$categoryCollection = Mage::getModel('catalog/category')->getCollection()
    ->addFieldToFilter('level', array('gteq' => 2)); //greater than root category id

foreach($categoryCollection as $category) {
    if ($category->getProductCount() === 0) {
        $category->delete();
    }
}

echo 'Empty Categories Deleted!';

Now you can easily run it by navigating to http://loca.lho.st/rmvEmptyCats.php and wait for the message Empty Categories Deleted!

Note that this is going to DELETE those categories with zero product count.

Jul 21, 2013
kalpesh

Magento get all categories of a product

Get all the categories a product belongs to in Magento. Below code will get you all the categories with details the product is attached to. Product can be shown under more than one category, so you may get more than one category ID. Either get the category collection from product, or get all the category IDs and then load them using catalog category collection model.

1
//$_product = Mage::getModel('catalog/product')->load($productID);

First way,

1
2
3
4
5
6
$catCollection = $_product->getCategoryCollection();
foreach($catCollection as $cat){
  print_r($cat->getData());
  //echo $cat->getName();
  //echo $cat->getUrl();
}

Another way, Continue reading »

Pages:12»

Welcome to my Blog

Kalpesh MehtaHelping Magento developers in their day-to-day development problems since 2011. Most of the problems and solutions here are my own experiences while working on different projects. Enjoy the blog and don't forget to throw comments and likes/+1's/tweets on posts you like. Thanks for visiting!

Certifications