Brijesh Patel Last Update - 2023-06-08
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.
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:
$url = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB);
$url = $this->getRequest()->getServer(‘HTTP_REFERER’);
Similarly, we can generate the URL of the current page like this:
$url = Mage::helper("core/url")->getCurrentUrl();
In the same fashion, we can dynamically generate the URL of any specific page using the code below:
$url = $this->getUrl("magentopage");
$url = $this->getProductData()->getProductUrl();
$url = $this->getCheckoutUrl();
$url = $this->getUrl("checkout/cart");
$url = $this->getSkinUrl("images/yourimage.gif");
$url = $this->getSkinUrl("images/yourimage.gif", array("_secure"=>true));
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.
$_product = Mage::getModel("catalog/product")->load($product_id);
$_product = Mage::getModel("catalog/product")->loadByAttribute("sku", $product_sku);
{{block type="cms/block" block_id="block_id" template="cms/content.phtml"}}
$collection = Mage::getModel("catalog/product")->getCollection();
$collection = Mage::getModel("catalog/product")->getCollection()
->addAttributeToFilter("some_attribute", $filter);
$collection = Mage::getModel("catalog/product")->getCollection()
->addFieldToFilter("some_attribute", $filter);
$collection = Mage::getModel("catalog/product")->getCollection()
->addAttributeToSelect("*");
$collection = Mage::getModel("catalog/product")->getCollection()
->addAttributeToSelect("some_attribute");
$collection = Mage::getModel("catalog/product")->loadByAttribute("sku", $sku);
$collection = Mage::getModel("catalog/product")->load($productId);
$category = new Mage_Catalog_Model_Category();
$category->load($categoryId);
$collection = $category->getProductCollection();
$collection = Mage::getModel("catalog/product")->getCollection()
->addAttributeToSelect("*")
->addAttributeToFilter(
$attributeCode,
array(
"eq" => Mage::getResourceModel("catalog/product")
->getAttribute($attributeCode)
->getSource()
->getOptionId($attributeOptionId)
))
->joinField("category_id", "catalog/category_product", "category_id", "product_id = entity_id", null, "left")
->addAttributeToFilter("category_id", $categoryId);
$collection->getSelect()
->group("e.entity_id");
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.
{{base url=""}}
{{store url=""}}
{{media url="yourmedia.mp4"}}
{{skin url="yourimage.gif"}}