Categories
Programming

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]

Categories
Programming

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. […]

Categories
Programming

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 / […]

Categories
Programming

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]

Categories
Programming

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]

Categories
Programming

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]

Categories
Programming

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 […]

Categories
Programming

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"> […]

Categories
Programming

PHP : Convert or Cast Array to Object & Object to Array

I love PHP Objects. Given an option of returning any data as an Array or Object, I would go for Objects. PHP Objects are clean and easy to write. Now to the conversion (casting) of a PHP Array into a PHP Object.