magento web developer

Manchester based Magento and Zend Framework certified developer.

Installing XHProf on Magento

Installing XHProf on ubuntu is quite easy using the following commands:

sudo pecl install xhprof-beta

Add the following to the end of /etc/php5/conf.d/xhprof.ini (you will need to create it):


Returning HTML with ajax and layout handles

Not so long ago, when I wanted to serve HTML via ajax, my controller action would look something like this:

$myBlock = $this->getLayout()


However I have since discovered a much better way that is not ignorant to layout handles. With a module/controller/method with layout handle mymodule_index_index you can rewrite the node named ‘root’ as follows:


    <block type="core/template" name="root" output="toHtml" template="mymodule/index.phtml" />


Using XHProf for Magento profiling

Built by facebook, XHProf is a “function-level hierarchical profiler for PHP and has a simple HTML based user interface”. With Magento being a very big platform with a lot going on, it is often useful to determine where time is being spent. Of course there are fantastic tools like NewRelic, however if you are not willing to pay, or you are on a shared server, then XHProf does the job.

Fortunately, there is already a good Magento module available for XHProf available at Liip provide a fairly solid integration of XHProf into the Magento admin panel, the only thing left to be desired is perhaps a better graphical user interface. It shows a break down of time spent in each method allowing you to diagnose why a given request is taking so long.

Recently I used this and discovered that I had not configured sendmail correctly!

EDIT: I’ve posted a follow up post on how to install XHProf and set it up with Magento.

Add products to the cart (in code)

Sometimes it is quite useful to be able to add products to a customer’s cart in code.  For example, you may wish to launch an email promotion with a link to the store. When the user clicks the link, they are directed straight to the cart page on the merchant’s website with the promoted product already in their cart!

Simple Products

Adding simple products is very easy and can be achieved with the following code:

// ...
$cart = Mage::getSingleton('checkout/cart');
$product = Mage::getModel('catalog/product')

// or...

// ...

Configurable Products

Things get a bit more complex when you need to add a configurable product and you need to prepare a ‘request’ array with the following structure.

'super_attribute' =>
    [attribute_id] => [attribute_value_id],
    [attribute_id_2] => [attribute_value_id_2],
'qty' => [qty]

First up, you need to obtain the attribute id and attribute value id.

$attribute = Mage::getModel('catalog/product')

$attributeId = $attribute->getId();
$attributeValueId = $attribute

And then implement the structure in PHP as follows:

$cart = Mage::getSingleton('checkout/cart');

$request = array();
$request['qty'] = 1;
$request['super_attribute'] = array();
$request['super_attribute'][65] = 1;
$request['super_attribute'][66] = 12;

$cart->addProduct(1242, $request);

1 2