X
    Categories: Magento 1 How-tos, Theme and Extension ReviewsMagento Tutorials for Beginners & Experts
Reading Time: 3 minutes

Magento is a robust ecommerce platform that is just right for projects of all complexities. Perhaps because of the all-inclusive code base, creating and editing Magento based code could become a real challenge.

To help out fellow Magento coders, I have compiled the following cheat sheet of the frequently used Magento functions, which are frequently used during Magento Developments. So in no particular order, the following list covers essential Magento functions that will help you take care of basic tasks while writing Magento code.

Get the Store URL

$url = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB);

Get the Referer URL

$url = $this->getRequest()->getServer(‘HTTP_REFERER’);

Get Current Page URL

$url = Mage::helper('core/url')->getCurrentUrl();

Get Magento Page URL

$url = $this->getUrl('magentopage');

Get Product URL

$url = $this->getProductData()->getProductUrl();

Get Image From Skin Folder

$url = $this->getSkinUrl('images/yourimage.gif');

Get Image From Skin Folder (Secure Access)

$url = $this->getSkinUrl('images/yourimage.gif', array('_secure'=>true));

Get Checkout Page URL

$url = $this->getCheckoutUrl();

Get Cart Page URL

$url = $this->getUrl('checkout/cart');

Get Current Category

$_category = Mage::getModel('catalog/layer')->getCurrentCategory();

Get Category by ID

$_category = Mage::getModel('catalog/category')->load($category_id);

Get Product by ID

$_product = Mage::getModel('catalog/product')->load($product_id);

Get Product by SKU

$_product = Mage::getModel('catalog/product')->loadByAttribute('sku', $product_sku);

Add CSS File in Layout Block

<reference name="name_of_the_block">

 <action method="addItem">

 <type>skin_css</type>

   <name>css/file.css</name>

   <params />

 </action>

Add JS File in Layout Block

<reference name="name_of_the_block">

 <action method="addItem">

 <type>skin_js</type>

   <name>js/file.js</name>

   <params />

 </action>

Remove Block From Old Parent

<reference name="old_parent">

 <action method="unsetChild">

   <name>block_name</name>

 </action>

</reference>

Add Block to New Parent

<reference name="new_parent">

 <action method="insert">

   <name>block_name</name>

 </action>

</reference>

Add CMS Block in Layout Block

<block type="cms/block" name="block_name">

 <action method="setBlockId">

   <block_id>block_id</block_id>

 </action>

</block>

Insert Template File in Layout Block

<block type="cms/template" name="template_name" template="page/html/template_file.phtml"/>

Get Base URL in CMS Pages

<a href="{{base url=''}}"></a>

Get Store URL in CMS Pages

<a href="{{store url=''}}"></a>

Get Media URL in CMS Pages

<a href="{{media url='yourmedia.mp4'}}"></a>

Get Skin URL in CMS Pages

<a href="{{skin url='yourimage.gif'}}"></a>

Call a Magento Block in CMS Pages

{{block type="cms/block" block_id="block_id" template="cms/content.phtml"}}

Load Cache Object

$cache = Mage::app()->getCache();

Save Item in Cache Object

$cache->save($string, $string_key, array($tag), $lifetime);

Retrieve Saved Cache

$cache->load($string_key);

Remove Saved Cache

$cache->remove($string_key);

Save Session Value

$session_value = "This is example session value.";

Mage::getSingleton('core/session')->setValue($session_value);

Retrieve Session Value

Mage::getSingleton('core/session')->getValue();Remove Session Value

Mage::getSingleton('core/session')->unsValue();

Check Whether Customer is Logged in

Mage::getSingleton('customer/session')->isLoggedIn();

Conclusion

As you could probably guess by now, this list does not cover all the functions in the Magento universe. Magento is huge and no single blog post could even begin to cover all the available functions ( people have written entire books on the subject!).

I would recommend that you go through the list carefully while making notes on the usage of individual functions. The best way of improving your Magento code is to explore the core of the Magento platform ( PHP, XML, and PHTML files), to learn more about Magento functions.

Happy coding! 🙂

 

Fayyaz Khattak :Fayyaz is a Magento Community Manager at Cloudways - A Managed Magento Hosting Platform. His objective is to learn & share about PHP & Magento Development in Community. Fayyaz is a food lover and enjoys driving. You can email him at m.fayyaz@cloudways.com