Browsing articles in "Magento frontend"
Oct 13, 2015
kalpesh

Magento get all items in cart

Magento get all the items currently in cart programatically using below code. You can place it anywhere you wish to get information, phtml or php file. Instead of Mage::getSingleton(‘checkout/session’)->getQuote() you can also use Mage::getSingleton(‘checkout/cart’)->getQuote() to get same results. If you want to see what all product information is retrieved you can use $product->getData() inside the foreach loop to display in array format.

1
2
3
4
5
6
7
$cart = Mage::getSingleton('checkout/session')->getQuote();
//$cart->getAllItems() to get ALL items, parent as well as child, configurable as well as it's simple associated item
foreach ($cart->getAllVisibleItems() as $item) {
        $product = $item->getProduct();
    $name = $product->getName();
    $sku = $product->getSku();
}

If you want all the items in collection format, you can call below code instead:

1
$itemsCollection = Mage::getSingleton('checkout/cart')->getQuote()->getItemsCollection();
Oct 13, 2015
kalpesh

Magento add static block to cms page

You can add static block to CMS page in Magento in following 2 ways:

1.) By adding code in Layout Update XML of CMS page:

1
2
3
4
5
6
7
<reference name="left">
    <block type="cms/block" name="block_name_anything">
        <action method="setBlockId">
            <block_id>STATIC_BLOCK_ID_HERE</block_id>
        </action>
    </block>
</reference>

2.) By putting below code directly into CMS Page content area:

1
{{block type="cms/block" block_id="STATIC_BLOCK_ID_HERE"}}

Make sure you flush Blocks HTML Output cache if your changes do not reflect on website.

Mar 28, 2015
kalpesh

Magento bug – Checkout cart 500 error – Redirect loops

Magento checkout cart gives 500 error and redirect loops when there is a shopping cart rule with Category condition.

I found a bug in Magento which redirects shopping cart indefinitely causing it 500 internal server error. This can be a serious bug as customer will not able to shop if this happens. I noticed this happens when there is a shopping cart rule which have Category in conditions of the rule.

If total quantity equals or greater than 1 for a subselection of items in cart matching ALL of these conditions:
Category is 125

So for example you have a shopping cart rule where you want to give some discount or free product if at least one product is chosen from specific Category, this triggers the error in frontend shopping cart. Main reason here is Category condition. If you remove category condition then the error should go away. But if you want to keep the category condition and still want Magento to handle the shopping cart promotion rule, check the code changes below:

To solve this I copied below file to my local
app/code/core/Mage/SalesRule/Model/Rule/Condition/Product/Combine.php

and edited the function validate:

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
/**
     * Validate a condition with the checking of the child value
     * @param Varien_Object $object
     *
     * @return bool
     */
    public function validate(Varien_Object $object)
    {
        /** @var Mage_Catalog_Model_Product $product */
        $product = $object->getProduct();
        if (!($product instanceof Mage_Catalog_Model_Product)) {
            $product = Mage::getModel('catalog/product')->load($object->getProductId());
        }

        $valid = parent::validate($object);

        /* Kalpesh commented whole block, as it causes redirect loop and Segmentation fault in apache
        if (!$valid && $product->getTypeId() == Mage_Catalog_Model_Product_Type_Configurable::TYPE_CODE) {
            $children = $object->getChildren();
            //$valid = $children && $this->validate($children[0]); //Kalpesh commented, issue....
        }*/


        return $valid;
    }

Hope this helps to some troubled souls!

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 7, 2014
kalpesh

Magento: Get real IP address behind a proxy

Get real IP address if your server or customer is behind a proxy network. Magento have function Mage::helper(‘core/http’)->getRemoteAddr(); to get client IP address, but it gives proxy IP address instead of real IP if there is proxy network in between. Below code checks and returns both real IP and proxy IP address if it founds any.

1
2
3
4
5
6
7
8
9
if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
    echo $_SERVER['HTTP_CLIENT_IP'];
} else if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
    $ips = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
    echo trim($ips[count($ips) - 1]); //real IP address behind proxy IP
    // echo $_SERVER['REMOTE_ADDR']; //proxy IP address
} else {
    echo $_SERVER['REMOTE_ADDR']; //no proxy found
}
Pages:1234567...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