161 cards

Config Menu Definition

This should go in adminhtml.xml. Also see Config ACL Definition (note that the XML path of the ACL entry needs to match up with the XML path of the menu entry).

To place an entry under the sales node (alter the name for other nodes):

                <namespace_module translate="title" module="namespace_module">
                    <title>Your Module</title>

Config ACL Definition

This should go in adminhtml.xml. Also see Config Menu Definition (note that the XML path of the menu entry needs to match up with the XML path of the ACL entry). To see how to implement the checking of an ACL within an admin controller see the _isAllowed() method in Boilerplate Admin Controller.

To place an entry under the sales node (alter the name for other nodes):


Adding A Layout Handle From A Controller

Doing this is a slight pain, to quote what Alan Storm said on StackOverflow Archive :

  1. If you add your handle before you call $this->loadLayout() from a controller it's too soon
  2. If you add your handle after you call $this->loadLayout() it's too late

So, if you really need to add a handle to a controller, you need to replace the call to $this->loadLayout() with:

$update = $this->getLayout()->getUpdate();

Config URL Rewrite Definition


Config Model Rewrite Definition


Pretty Backtrace / Stack Trace

In app/code/core/Mage/Core/functions.php Magento offers a function named mageDebugBacktrace() which is nice, but I thought could be made nicer. This backtrace shows frame numbers, file names and line numbers, classes, methods and arguments where possible. It can be placed anywhere you fancy, as long as it is included by Magento - I normally add it to functions.php when needed, and remove when I'm done.

Example output:

[ 0] app/code/core/Enterprise/Search/Model/Adapter/Abstract.php:414          Enterprise_Search_Model_Adapter_HttpSt...

Add comment to form input in admin

$afterElementHtml = '<p class="nm"><small>' . ' this is the hint! ' . '</small></p>';

$linkFieldset->addField('field_name', 'text', array(
    'after_element_html' => $afterElementHtml,

Config Two-Level Memcached & DB Definition


Config Redis Cache Definition

You need Cm_Cache_Backend_Redis Archive for this

                <server>localhost</server> <!-- or absolute path to unix socket for better performance -->
                <force_standalone>0</force_standalone>  <!-- 0 for phpredis, 1 for standalone PHP -->

Category Product Collection

This snippet provides a collection of products within a category

$cat = Mage::getModel('catalog/category')->load(1);

$coll = Mage::getResourceModel('catalog/product_collection');

Block Caching

To cache a block individually, add this method to the blocks class:

protected function _construct()
        'cache_lifetime' => 3600,
        'cache_tags'     => array(Mage_Cms_Model_Block::CACHE_TAG),
        'cache_key'      => 'CACHE_KEY',

_Note - if you set cache_lifetime to 0 (or any other value which PHP evaluates to false) then the cache will actually last 7200 seconds (2 hours), so in order to have a very high cache time then the cache_lifetime should be set ...

Config Transactional Email Template Definition

                <your_module_email_something_template translate="label" module="namespace_module">


Sandbox Script

Sometimes you want to run some code to see what it does, dump an object out or check a class but the thing you want to run/dump/check is hidden deep in the bowels of the code somewhere, and you feel it might be easier to run a bit of PHP to check just that one thing, without having to fire up or your browser, or wait for cron to run - that's where this sandbox script comes in.

To use either of the examples below, save them into a file in the root of your magento install called sandbox.php, then either browse to the directory and run `php ...

Overriding A Controller

                        <your_module before="Mage_Checkout">Your_Module</your_module>

Formatting A Price For The Current Locale

$price = 500;
$formattedPrice = Mage::helper('core')->currency($price, true, false);

Resizing A Product Image

$helper = Mage::helper('catalog/image');
$helper->init($product, 'image');
$helper->resize(216, 161);

// the helpers __toString() method outputs the url
echo $helper;


Controller Methods

A list of controller methods

Collection Manipulation (Filtering, Limiting, etc)

A list of collection methods, similar to this Archive



You have to first specify an array of attributes in the filter, then a two dimensional array, with each second dimension have the filters for attribute n, as below:

    array('email', 'customer_id'),
        array('eq' => $customer->getEmail()), // filters for e-mail
        array('eq' => $customer->getId()), // filters for...
This website uses short-lived cookies to improve usability.
Accept or learn more