Browsing articles in "Magento frontend"
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>
Nov 17, 2013
kalpesh

Magento enterprise: show top mini cart when product is added to cart

Magento Enterprise comes with a top header mini-cart, which displays all the items with their custom options added to cart, when you click on My Cart in the header. This is a good feature, but what if you want to show this mini-cart each time a product is added, without clicking on that link? I will show you here how to display your mini cart automatically when a product is added to cart.

Open your cartheader.php file, which is located at:
app/design/frontend/enterprise/YOUR_DESIGN/template/checkout/cart/cartheader.phtml

In the last few lines of this file, you should find the below line in javascript:

1
2
3
Enterprise.TopCart.initialize('topCartContent');
// Below can be used to show minicart after item added
// Enterprise.TopCart.showCart(7);

Replace the last line, //Enterprise.TopCart.showCart(7); with the below lines:

1
2
3
4
5
6
7
8
9
jQuery( document ).ready(function() {
    if( jQuery('#messages_product_view').children().length ){ 
        if(jQuery('#messages_product_view').children().children().attr('class') == 'success-msg') {
            if(jQuery('.success-msg ul li span').text().indexOf('was added to your shopping cart') > -1) {
                Enterprise.TopCart.showCart(7);
            }
        }
    }
});

So whenever in the page, there will be an element with ID “#messages_product_view” and it has a children with class “success-msg” and it has a ul/li/span with text containing “was added to your shopping cart”, we will show the top mini-cart. This is only true when an product is added to shopping cart.

You can also show top mini-cart without this jquery hack, by making a new module in Magento and catch the event when product is added to cart. Then programmatically clicking the top mini cart to display it. But according to me this small piece of code is better than to create whole new Magento module.

HTH!

Nov 3, 2013
kalpesh

Magento remove session id from URL

Magento displays session ID in url, something like:

1
http://loca.lho.st?__SID=2wewesfdgfsdm

You can remove this in two ways:

1. Go to your Magento admin panel > System > Configuration > Web.
Under Session Validation Settings, set “No” against label “Use SID on the Frontend”.
If this doesn’t work, then move to option two below.

2. Edit the file at app/code/core/Mage/Core/Model/App.php (somewhere around line 222),

1
protected $_useSessionInUrl = true;

Change that value to “false”. That should now prevent session IDs appearing in URL.

Aug 17, 2013
kalpesh

Magento debug XML (layout, config) files

In Magento if there is any error in XML file, Magento silently ignores it and continues further parsing. So you never get to know where is the actual error and makes it difficult to debug. You can’t even do any logging in XML file and also Magento don’t tell that error is in XML. It makes debugging almost impossible and ends up wasting in hours to find some silly mistake.

But we can know if there is any error in XML (layout.xml or config.xml or any xml file) if you use below code in the controller action which is being called. The browser will display WHOLE XML code and if it encounters any error in it, simply gives where is the error in the XML tree.

If you are trying to load let’s say Product View page, then put this code in Mage/Catalog/controllers/ProductController.php file’s viewAction() method temporarily to display whole XML tree to find out error(s) if any. As we are saying to display the page as XML, the page will break if it finds any mal-formed XML code and will show where is the mistake.

1
2
header("Content-Type: text/xml");
echo Mage::app()->getConfig()->getNode()->asXml();exit;

If you want to debug Layout Handles only, you can just check by this code:

1
2
header("Content-Type: text/xml");
echo Mage::app()->getLayout()->getUpdate()->getHandles();exit;
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.

Pages:«12345678...14»

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