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):

<config>
    <menu>
        <sales>
            <children>
                <namespace_module translate="title" module="namespace_module">
                    <title>Your Module</title>
                    <sort_order>10...

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):

<adminhtml>
    <acl>
 ...

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();
$update->addHandle('default');
$thi...

Config URL Rewrite Definition

<config>
    <global>
        <rewrite>
            <namespace_module>
                <from><![CDATA[#^/some/regex/([a-z]*/?$#]]></from>
                <to><![CDATA[/frontname/whatever/whatever/blah/$1]]></to>
            </namespace_module>
        </rewrite>
    </global>
</config>

Config Model Rewrite Definition

<config>
    <global>
        <models>
            <catalog>
                <rewrite>
                    <product>Namespace_Module_Model_Catalog_Product</product>
                </rewrite>
            </catalog>
        </models>
    </global>
</config>

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>
    <global>
        <cache>
            <backend>memcached</backend>
            <slow_backend>database</slow_backend>
            <id_prefix>cache_</id_prefix>
            <memcached>
                <servers>
                    <server1>
                        <host><![CDATA[localhost]]></host>
                        <port><![CDATA[11211]]></port>
                        <persistent><![CDATA[0]]></persistent>
                        <weight><![CDATA[1]]></weight>
        ...

Config Redis Cache Definition

You need Cm_Cache_Backend_Redis Archive for this

<config>
    <global>
        <cache>
            <backend>Cm_Cache_Backend_Redis</backend>
            <backend_options>
                <server>localhost</server> <!-- or absolute path to unix socket for better performance -->
                <port>6379</port>
                <database>0</database>
                <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');
$coll->addCategoryFilter($cat);

Block Caching

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

protected function _construct()
{
    $this->addData(array(
        '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

<config>
    <global>
        <template>
            <email>
                <your_module_email_something_template translate="label" module="namespace_module">
                    <label>Something</label>
                    <file>namespace/module/something.html</file>
                    <type>html</type>
                </your_module_email_something_template>
            </email>
        </template>
    </global>
</config>

![](http://mikewhitby.co.uk/makandra-track.gif?a=config-transacti...

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

<config>
    <frontend>
        <routers>
            <checkout>
                <args>
                    <modules>
                        <your_module before="Mage_Checkout">Your_Module</your_module>
                    </modules>
                </args>
            </checkout>
        </routers>
    </frontend>
</config>

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;

Draft

Controller Methods

A list of controller methods

Collection Manipulation (Filtering, Limiting, etc)

A list of collection methods, similar to this Archive

Where

OR:

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:

$this->addFieldToFilter(
    array('email', 'customer_id'),
    array(
        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