Categories
Programming

Magento – Returning JSON (for AJAX and API Calls) Response From Controller Action

Within your Magento Controller > Action you can use the below code to send a JSON response.

[php]
$this->getResponse()->setHeader(‘Content-type’, ‘application/json’);
$this->getResponse()->setBody($jsonData);
[/php]

Read this post on how to use Magento’s core helper to encode and decode JSON.

Categories
Programming

Magento’s Core JSON Encoding and Decoding Functions

To JSON encode an array
[php]
$jsonData = Mage::helper(‘core’)->jsonEncode($array);
[/php]

To JSON decode an array
[php]
$array = Mage::helper(‘core’)->jsonDecode($jsonData);
[/php]

Categories
Programming

Magento – How to Check if a Module is Installed and is Active

While developing Magento websites, you might wish to check to find out if a module is installed before proceeding to use that module’s functions. You can do so by using the following bit of code.
[php]
/* Returns boolean true or false */
Mage::getConfig()->getModuleConfig(‘Namespace_Modulename’)->is(‘active’, ‘true’);
[/php]

Categories
Programming

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 the custom attribute */
Zend_Debug::dump(Mage::getModel(‘catalog/product’)->load($item->getProduct()->getId())->getMyCustomAttribute());
}
[/php]

Categories
Programming

Magento – Setting Robots (Index Follow) Using XML Layout Updates

[xml]
<reference name="head">
<action method="setRobots"><value>NOINDEX,NOFOLLOW</value></action>
</reference>
[/xml]

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]


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.

To output the image from the above example, you could use the following code

[php]
echo Mage::getBaseUrl(‘media’) . Mage::getStoreConfig(‘system_config_name/group/logo’);
[/php]

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 / or configuration.

[php]
// Load the session
$session = Mage::getSingleton(‘checkout/session’);
// Array to hold the final result
$finalResult = array();
// Loop through all items in the cart
foreach ($session->getQuote()->getAllItems() as $item)
{
// Array to hold the item’s options
$result = array();
// Load the configured product options
$options = $item->getProduct()->getTypeInstance(true)->getOrderOptions($item->getProduct());
// Check for options
if ($options)
{
if (isset($options[‘options’]))
{
$result = array_merge($result, $options[‘options’]);
}
if (isset($options[‘additional_options’]))
{
$result = array_merge($result, $options[‘additional_options’]);
}
if (!empty($options[‘attributes_info’]))
{
$result = array_merge($options[‘attributes_info’], $result);
}
}
$finalResult = array_merge($finalResult, $result);
}
// Now you have the final array of all configured options
Zend_Debug::dump($finalResult);
[/php]

Source: the function – getOrderOptions() found in the file – app/code/core/Mage/Bundle/Block/Adminhtml/Sales/Order/View/Items/Renderer.php

The options will be stored in the keys ‘label’ and ‘value’ in the resulting array.

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]