Magento Tips and Tricks


How to call staic block any XML file

<reference name="left">
    <block type="cms/block" name="sample_block" before="-">
        <action method="setBlockId"><block_id>sample_block</block_id></action>
    </block>   
</reference> 

How to call staick block in your template or .phtml files

<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('my_static_block_name')->toHtml() ?>

How to call static block in CMS->Pages

{{block type="cms/block" block_id="your_block_id"}}

How to call staick block in your template or .phtml files

<?php echo $this->getLayout()->createBlock('page/html')->setTemplate('page/html/homepageleft.phtml')->toHtml();?>

Filter Variable

Mage::getModel('core/email_template_filter')->filter($description); /* for filter */
Mage::getModel('core/email_template_filter')->setVariables(array('custom_url' => Mage::getUrl('*/*/custom'), 'custom_var' => 100))->filter($description); /* for adding custom variable */

Increase memory limit and execution time
Put it on your index.php file

ini_set("memory_limit","10000M");
ini_set("post_max_size","30M");
ini_set("max_execution_time",1000);
ini_set("max_input_time",1000);

Get URL by Type

Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_JS);
//http://magento.demo/js/

Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_LINK);
//http://magento.demo/index.php/

Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA);
//http://magento.demo/media/

Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_SKIN);
//http://magento.demo/skin/

Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB);
//http://magento.demo/

Add or remove css Or Js in any specific page with block class

$headBlock = $this->getLayout()->getBlock('head');
$headBlock->addJs('somefolder/yay.js');
$this->getLayout->getBlock('head')->removeItem('js', 'calendar/calendar.js');

Get Admin skin/images url in magento


Mage::getDesign()->getSkinUrl('images/success_msg_icon.gif')

Get Adminhtml url for custom module


$URL = Mage::helper("adminhtml")->getUrl("mymodule/adminhtml_mycontroller/myaction/",array("param1"=>1,"param2"=>2));

or

$URL = Mage::helper("adminhtml")->getUrl("mymodule/adminhtml_mycontroller/myaction/");

Check if customer is logged in

if(!Mage::getSingleton('customer/session')->isLoggedIn())
{
 $this->_redirect('customer/account/login/');
}

Load Database row using your custom field

 $element = Mage::getModel('module/model')
            ->load($value, $key); 

Get addition custom option for configurable product on cart

$session                    = Mage::getSingleton('checkout/session');
$helper = Mage::helper('catalog/product_configuration');
foreach ($session->getQuote()->getAllItems() as $item) {
$customOptions = $helper->getCustomOptions($item)
}
Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s