Magento Functions Cheat Sheet

Magento is a very powerful and thus slightly difficult to tame CMS. It is built on the mighty Zend framework, and it often becomes a difficult task to create or edit Magento templates. 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. Here in this article, I"ll try to compile a brief list of useful Magento template development functions, which are used frequently during theme development. We have to cover a lot in this article, so without further ado, I`ll start explaining the functions.

I thought it was about time that someone created a place developers could go to find some of the most commonly used Magento methods/functions.

Useful PHP Functions

Although XML files are an important part of any Magento template that defines template layouts, PHP files are the real building blocks which do the actual task of filling up those layout blocks with useful content.

General Store-Related Functions

These are the functions which are used throughout the template pages.

We can use two different approaches to generate many of these URLs. One is by using getURLs (like getBaseUrl, getSkinUrl, etc.), and the other is by using a helper/model class. To keep things simple here, we"ll try to use the getURL approach in most cases. For example, to get the store URL you can use this simple function:

Get the Store URL

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

Get the Referer URL

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

Similarly, we can generate the URL of the current page like this:

Get Current Page URL

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

In the same fashion, we can dynamically generate the URL of any specific page using the code below:

Get Magento Page URL

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

Get Product URL

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

Get Checkout Page URL

$url = $this->getCheckoutUrl();

Get Cart Page URL

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

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));

Product Related Functions

Product and Category pages are the most important part of any eCommerce CMS. While creating Magento templates, you"ll be working on these pages most of the time. Here I"ve listed some frequently used functions for these pages, which will facilitate you in your development endeavors.

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);

Call a Magento Block in CMS Pages

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

Get product collection

$collection = Mage::getModel("catalog/product")->getCollection();

Get product collection by filtering EAV collections

$collection = Mage::getModel("catalog/product")->getCollection()
        ->addAttributeToFilter("some_attribute", $filter);

Get product collection by filtering Non-EAV collections

$collection = Mage::getModel("catalog/product")->getCollection()
        ->addFieldToFilter("some_attribute", $filter);

Get product collection and add all available attributes to the collection

$collection = Mage::getModel("catalog/product")->getCollection()

Get product collection and select the attributes that we want to retrieve in our collection

$collection = Mage::getModel("catalog/product")->getCollection()

Get product info by product sku

$collection = Mage::getModel("catalog/product")->loadByAttribute("sku", $sku);

Get product info by product id

$collection = Mage::getModel("catalog/product")->load($productId);

Get product collection based on the category id

$category = new Mage_Catalog_Model_Category();
$collection = $category->getProductCollection();

Get product collection based on the category id and filter by attribute option id

$collection = Mage::getModel("catalog/product")->getCollection()
                            "eq" => Mage::getResourceModel("catalog/product")
                    ->joinField("category_id", "catalog/category_product", "category_id", "product_id = entity_id", null, "left")
                    ->addAttributeToFilter("category_id", $categoryId);


CMS Page-Related Functions

CMS Pages are the pages of the store where user-created content is published. Some examples of such pages are the About Us page, the Privacy Policy page, and the Terms & Conditions page. Though these pages usually serve as a place to put user-created static content, many times you have to enter dynamic content in them, for example a dynamically generated link to the store URL or the skin files URL. Below we`ll discuss how to handle such scenarios.

Get Base URL in CMS Pages

{{base url=""}}

Get Store URL in CMS Pages

{{store url=""}}

Get Media URL in CMS Pages

{{media url="yourmedia.mp4"}}

Get Skin URL in CMS Pages

{{skin url="yourimage.gif"}}

Viewed 15 Times

Post On 2022-12-03