Saturday, October 16, 2010

Magento snippets I find useful

I have created this page as I often find myself working on Magento projects in many different locations and needed a quick reference. You will often find, you will need to add these in to your scripts to get that level of functionality that you need for your magento customisation.

N.B. This page is ongoing and will be updated often. All snippets work on 1.4.11

Singles

//get current controller name
$this->getRequest()->getControllerName(); //returns string

//get Action Name
$this->getRequest()->getActionName(); //returns string

//get Module Name
$this->getRequest()->getModuleName(); //returns string

//get Number of items in user cart
$this->helper('checkout/cart')->getSummaryCount(); //returns number of products

//check if user is logged in
if ($this->helper('customer')->isLoggedIn()) //returns true/false

Combinations

//check if user is on home page
if ($this->getIsHomePage()) { } //returns true/false
//check for home page only works when in header.phtml
//use method below if outside of this file or
//create new instance of:
//Mage_Page_Block_Html_Header() like so:
//$newHeaderObject = new Mage_Page_Block_Html_Header();//
if($newHeaderObject ->getIsHomePage()){} // returns true/false

//alternative method
if (($this->getRequest()->getModuleName() == 'cms') && ($this->getRequest()->getActionName() == 'index')) { } //returns true/false
//use this to check if home page in other template (.phtml) files
//works by checking the module name and the action request.

Labels: , , , ,

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home