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:
<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!
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.
<?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 " " . $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.
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.
<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> |
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:
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.
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.
//$_product = Mage::getModel('catalog/product')->load($productID); |
First way,
$catCollection = $_product->getCategoryCollection(); | |
foreach($catCollection as $cat){ | |
print_r($cat->getData()); | |
//echo $cat->getName(); | |
//echo $cat->getUrl(); | |
} |
Another way, Continue reading »
Welcome to my Blog
Certifications
Honor
Recognition
Contributions
Categories
- Apache (2)
- ChatGPT (1)
- Domain name (2)
- eCommerce (2)
- htaccess (1)
- Humor (3)
- Instagram API (1)
- jQuery (4)
- JSON (1)
- Linux (10)
- Magento (142)
- Magento admin (58)
- Magento Certification (5)
- Magento error (13)
- Magento frontend (68)
- Magento Imagine (2)
- Magento Interview (5)
- Magento Master (2)
- Magento2 (10)
- Mobile (1)
- MySQL (7)
- OpenAI (1)
- OroCRM (2)
- Performance (2)
- PHP (8)
- Prototype JS (3)
- Security (4)
- Wordpress (3)
- XML (2)