Browsing articles in "Magento"
Jun 22, 2014
kalpesh

Magento: Enable logs on API calls

There is no setting to enable/disable API calls in Magento, but you can do so by modifying a file which handles all the API calls and logging the requests in a custom log file. This is useful when you know that there are many API calls being requested by third party applications to your website but not sure what is actually being called, how many times, and at what day and time.

With that said, open the file: app/code/core/Mage/Api/Model/Server/Handler/Abstract.php
and find the function “call” which handles all the API calls. Scroll down the function and find the if block which says:

if (method_exists($model, $method)) {
        if (isset($methodInfo->arguments) && ((string)$methodInfo->arguments) == 'array') {
            return $model->$method((is_array($args) ? $args : array($args)));
        } elseif (!is_array($args)) {
            return $model->$method($args);
        } else {
            return call_user_func_array(array(&$model, $method), $args); 
        }       
} else {
        throw new Mage_Api_Exception('resource_path_not_callable');
}

and replace it with:

if (method_exists($model, $method)) {
        Mage::log($method, null, 'api.log'); //logs the method
        Mage::log($args, null, 'api.log'); //logs the arguments
        if (isset($methodInfo->arguments) && ((string)$methodInfo->arguments) == 'array') {
            return $model->$method((is_array($args) ? $args : array($args)));
        } elseif (!is_array($args)) {
            return $model->$method($args);
        } else {
            return call_user_func_array(array(&$model, $method), $args);
        }
} else {
        throw new Mage_Api_Exception('resource_path_not_callable');
}

Now you should have your var/log/api.log file filled with the “method” and “arguments” requested by third party systems on your server. This should help you in knowing what was requested and at what time in your new log file.

Jan 5, 2014
kalpesh

Magento continue shopping link to last added product’s category page

In Magento checkout cart, link Continue shopping button to last added product’s category page. Below code will check last added product’s id in cart and gets the last category assigned to the product.

Put below code at the start of the checkout/cart.phtml file

$lastProductIdAddedToCart = Mage::getSingleton('checkout/session')->getLastAddedProductId();
if($lastProductIdAddedToCart) {
    $productCategoryIdsArray = Mage::getModel('catalog/product')->load($lastProductIdAddedToCart)->getCategoryIds();
    //print_r($productCategoryIdsArray);    
    $continueShoppingCategoryUrl = Mage::getModel('catalog/category')->load(end($productCategoryIdsArray))->getUrl();
}

Replace continue shopping button with below code, in the checkout/cart.phtml file

<?php if($this->getContinueShoppingUrl()): ?>
    <button type="button" title="<?php echo $this->__('Continue Shopping') ?>" class="button btn-continue" onclick="setLocation('<?php echo (isset($continueShoppingCategoryUrl)) ? $continueShoppingCategoryUrl : $this->getContinueShoppingUrl(); ?>')"><span><span><?php echo $this->__('Continue Shopping') ?></span></span></button>
<?php endif; ?>
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.

<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>
Dec 1, 2013
kalpesh

Magento Redis read error on connection

Magento read error on connection when using Redis. If you are using Redis as cache in Magento and read timeout is not specified, you may get this error. Below is how it looks in the error report.

read error on connection

Trace:
#1 lib/Mage/Cache/Backend/Redis.php(210): Credis_Client->hGet('zc:k:0cd_FPC_DE...', 'd')
#2 lib/Zend/Cache/Core.php(303): Mage_Cache_Backend_Redis->load('0cd_FPC_DESIGN_...', false)
#3 lib/Varien/Cache/Core.php(158): Zend_Cache_Core->load('FPC_DESIGN_EXCE...', false, false)
#4 app/code/core/Mage/Core/Model/Cache.php(379): Varien_Cache_Core->load('FPC_DESIGN_EXCE...')
#5 app/code/core/Enterprise/PageCache/Model/Processor.php(185): Mage_Core_Model_Cache->load('FPC_DESIGN_EXCE...')
#6 app/code/core/Enterprise/PageCache/Model/Processor.php(146): Enterprise_PageCache_Model_Processor->_getDesignPackage()
#7 app/code/core/Enterprise/PageCache/Model/Processor.php(108): Enterprise_PageCache_Model_Processor->_createRequestIds()
#8 app/code/core/Mage/Core/Model/Cache.php(703): Enterprise_PageCache_Model_Processor->__construct()
#9 app/code/core/Mage/Core/Model/Cache.php(685): Mage_Core_Model_Cache->_getProcessor('Enterprise_Page...')
#10 app/code/core/Mage/Core/Model/App.php(340): Mage_Core_Model_Cache->processRequest()
#11 app/Mage.php(683): Mage_Core_Model_App->run(Array)
#12 /var/www/source/index.php(87): Mage::run('', 'store')

The solution (atleast for us) was to add a read_timeout config option in app/etc/local.xml where you have configured Redis cache for Magento.

<cache>
        <backend>Mage_Cache_Backend_Redis</backend>
        <backend_options>
                <server>127.0.0.1</server>
                <port>6379</port>
                <persistent></persistent>
                <database>0</database>
                <password></password>
                <force_standalone>0</force_standalone>
                <connect_retries>1</connect_retries>
                <read_timeout>10</read_timeout><!--add this-->
                <lifetimelimit>57600</lifetimelimit>
                <compress_data>0</compress_data>
        </backend_options>
</cache>

HTH!

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:

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:

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!

Pages:«1...567891011...29»

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

Honor

Recognition

Magento top 50 contributors

Magento top 50 contributors

Contributions