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]

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 layout selection dropdown on the Magento admin form page.

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">
<rel>canonical</rel>
<href>http://www.yoururl.com/goes/here</href>
</action>
</reference>
[/xml]

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.

[php]
// Echoing a PHP Array value
echo $array[‘value’];

// Echoing a PHP Object value
echo $object->value;
[/php]

Now to the conversion (casting) of a PHP Array into a PHP Object. This is very simple. I just type cast the Array as an Object when returning it.

[php]
function array_to_object($array) {
return (object) $array;
}
[/php]

The above is just an example. You do not need a PHP function to convert an Array into an Object. The (object) function will do that to any PHP Array. If you ever need to change an Object into an Array, then use the (array) type casting function.

[php]
function object_to_array($object) {
return (array) $object;
}
[/php]

Now, back to some Codeigniter coding :)