Magento
Magento – Product Attributes of Products in the Cart
The following piece of code can pull any attribute of the products in the shopping basket. This code can be used anywhere on the website. [php] /* Get all the items in the cart */ $cartItems = Mage::getSingleton(‘checkout/session’)->getQuote()->getAllItems(); /* Iterate through the items */ foreach ($cartItems as $item) { /* Load the product and get…
Magento – Setting Robots (Index Follow) Using XML Layout Updates
[xml] <reference name=”head”> <action method=”setRobots”><value>NOINDEX,NOFOLLOW</value></action> </reference> [/xml]
Magento – Programmatically Subscribe and Unsubscribe Newsletter Subscribers
The following can be a handy snippet. Its usage and placement is totally up to your imagination and requirements. [php] /* To Subscribe */ Mage::getModel(‘newsletter/subscriber’)->subscribe(‘[email protected]’); /* To Unsubscribe */ Mage::getModel(‘newsletter/subscriber’)->loadByEmail(‘[email protected]’)->unsubscribe(); [/php]
Adding an Image Upload Field to the Magento System.xml Configuration
[xml] Logo Allowed file types: jpeg, gif, png. image adminhtml/system_config_backend_image theme theme 1 1 1 1 [/xml] The element represents the location where the image will be uploaded to. In the above example, the image will be saved to a sub-folder under the media folder. e.g. /media/theme/. The element is used to rendering the tag.…
Magento – Get the Chosen Configuration / Options of the Products in the Cart
A Magento client of mine wanted to be able to change the calls to action on the cart and checkout pages based on the product configuration that the customers have selected for purchase. The following bit of code loops through the products in the Magento shopping basket and gets the chosen custom options and /…
Magento – Inject Body Class via Layout XML
This is an XML piece that has helped me many times. I might wish to style a page or product using a global body level class. In such a case, insert this XML snippet into your desired location. [xml] <reference name=”root”> <action method=”addBodyClass”><classname>my-body-class</classname></action> </reference> [/xml]
Magento – Set Breadcrumbs via Layout XML
I have used this XML in custom modules and custom pages. It comes handy to pass the Breadcrumb information to Google. [xml] <reference name=”breadcrumbs”> <action method=”addCrumb”><crumbName>Home</crumbName><crumbInfo><label>Home</label><title>Home</title><link>/home</link></crumbInfo></action> <action method=”addCrumb”><crumbName>Sample Form</crumbName><crumbInfo><label>Sample Form</label><title>Sample Form</title><link>/forms/sample/</link></crumbInfo></action> </reference> [/xml]
Magento – Set Meta Title, Description, Keywords and Canonical URL via Layout XML
This handy XML can be used anywhere – your custom modules, CMS pages, product pages, etc. [xml] <reference name=”head”> <action method=”setTitle” translate=”title” module=”forms”><title>Title</title></action> <action method=”setDescription” translate=”title” module=”forms”><title>Description</title></action> <action method=”setKeywords” translate=”title” module=”forms”><title>Keywords</title></action> <action method=”addLinkRel” translate=”title” module=”forms”><rel>canonical</rel><href>http://www.yoururl.com/</href></action> </reference> [/xml]
Add a Layout Select Field to the Admin Form of Magento Custom Modules
I am coding a bespoke CMS feature for a Magento project. I wanted the client to be able to change the template / layout of each page. This is similar to the Layout selection found in the standard Magento CMS page edit screen. I added the following within the _prepareForm() function. This will display a…
Magento – Adding Canonical URL for CMS Pages via Layout XML
Magento does not automatically add canonical URLs for CMS pages. Maybe they will add this feature in the future. However, if you are setting up pages and wish to add a canonical URL using the layout update xml, you can add the following code into the Custom Layout Update XML. [xml] <reference name="head"> <action method="addLinkRel">…