Custom Cache Type Definition

Adding this XML will cause an additional entry to appear in the Magento Cache Management screen.

<global>
    <cache>
        <types>
             <your_cache_type module="your_module" translate="label description">
                <label>Your Custom Cache Label</label>
                <description>Description of cache type.</description>
                <tags>YOUR_CACHE_TAG</tags>
             </your_cache_type>
        </types>
    </cache>
</global>

![](http://mikewhitby.co.uk/makandra-track.g...

Messages and Global Messages Blocks

Both use the same block, as we can see in page.xml:

<block type="core/messages" name="global_messages" as="global_messages"/>
<block type="core/messages" name="messages" as="messages"/>

When you add a message, you add it to the session, rather than to either of the
messages blocks, so the code below causes the Mage_Customer_Model_Session object
to have an error logged to it:

Mage::getSingleton('customer/session')->addError('Please enter all required information');

At this point neither message blocks know anything about the...

Reindex Via Code

An example of code qhich allows you to reindex:

$indexer = Mage::getSingleton('index/indexer');
$process = $indexer->getProcessByCode('catalog_product_price');
$process->reindexEverything();

The following are indexer codes which you can use instead of the catalog_product_price
index above:

catalog_product_attribute     Product Attributes
catalog_product_price         Product Prices
catalog_url                   Catalog URL Rewrites
catalog_product_flat          Product Flat Data
catalog_category_flat     ...

Getting The Last Order ID From The Session

Mage::getSingleton('checkout/session')->getLastOrderId();

Widget Parameter ID Weirdness

I've not yet found out why, but this will not work:

<widgets>
    <some_widget type="some_module/block" translate="name description" module="some_module">
        <name>Widget</name>
        <description>Foo</description>
        <parameters>
            <script_tag_id translate="label description">
                <visible>1</visible>
                <required>1</required>
                <label>Script Tag ID</label>
                <description>The ID for the script tag, this looks like DoubleClickF...

EAV Tables

A list of EAV tables and their purpose.

|----------------------------+-------------------------------------------------------------------------------------|
| Table | Contains |
|----------------------------+-------------------------------------------------------------------------------------|
| eav_attribute | Attributes and their global config values |
| catalog_eav_attribute | Catalog...

Import Resetting visibility and stock attributes

If you are updating an existing product you need to include the stock and visibility attributes, otherwise the stock for the product will get set to 0 and out of stock, and the visibility will be reset to Catalog, Search.

Allowing Import Of Invisible Attributes

By setting a Product Attribute to be invisible via the visible property, you stop the ability to import that attribute unless you alter the _forcedAttributesCodes property of the relevant product type. The class names are:

  • Mage_ImportExport_Model_Import_Entity_Product_Type_Simple
  • Mage_ImportExport_Model_Import_Entity_Product_Type_Grouped
  • Mage_ImportExport_Model_Import_Entity_Product_Type_Configurable

The best method is probably to subclass the relevant pro...

Image Resize Helper Composition

The product image resizing goes through a few layers of abstraction, so I thought I'd add a quick UML diagram showing composition through the layers. The resizing ultimately gets done by the gd PHP extension.

startSetup() and endSetup()

Ever wondered what they do? Basically they disable and then enable foreign key checks, and set the SQL mode to NO_AUTO_VALUE_ON_ZERO, then back to the old SQL mode. Below is the code taken from Varien_Db_Adapter_Pdo_Mysql:

/**
 * Run additional environment before setup
 *
 * @return Varien_Db_Adapter_Pdo_Mysql
 */
public function startSetup()
{
    $this->raw_query("SET SQL_MODE=''");
    $this->raw_query("SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0");
    $this->raw_query...

Attribute Source Model

Used when you want to use a multiselect or select dropdown box on an EAV entity, such as products or categories.

class Namespace_Module_Model_Select extends Mage_Eav_Model_Entity_Attribute_Source_Abstract
{
    /**
     * Retrieve Full Option values array
     *
     * @param bool $withEmpty     Add empty option to array
     * @param bool $defaultValues
     *
     * @return array
     */
    public function getAllOptions($withEmpty = true)
    {
        if (!$this->_options) {
      ...

Javascript Cookie Functions

This doesn't really go here, but hey ho. Magento's functions in js/Mage/cookies.js are not very good.

/**
 * Get a cookie value
 *
 * @param String name The cookie name
 *
 * @return String The cookie value
 */
getCookie: function(name)
{
    var val = '';
    var cookies = document.cookie.split('; ');
    for (i = 0; i < cookies.length; i++) {
        nameVal  = cookies[i].split('=');
        if (nameVal[0] == name) {
            val = unescape(nameVal[1]);
        }
   ...

Changing The Admin Theme

Put this in your local.xml:

<config>
    <stores>
        <admin>
            <design>
                 <theme>
                     <default>yourtheme</default>
                 </theme>
            </design>
        </admin>
    </stores>
</config>

Fetching Store Contact E-Mail Addresses

// General Contact
$name =  Mage::getStoreConfig('trans_email/ident_general/name'); 
$email = Mage::getStoreConfig('trans_email/ident_general/email');

// Sales Representative
$name =  Mage::getStoreConfig('trans_email/ident_sales/name'); 
$email = Mage::getStoreConfig('trans_email/ident_sales/email');

// Customer Support
$name =  Mage::getStoreConfig('trans_email/ident_support/name'); 
$email = Mage::getStoreConfig('trans_email/ident_support/email');

// Custom Email 1
$name =  Mage::getStoreConf...

Order States and Statuses

The only state to hold more than one status by default is payment_review, it seems a little odd to me that PayPal creates a state of pending_paypal - that seems like it should be a child of pending_payment.

  • new
    • pending
  • pending_payment
    • pending_payment
  • payment_review
    • payment_review
    • fraud
  • processing
    • processing
  • complete
    • complete
  • closed
    • closed
  • canceled
    • canceled
  • holded
    • holded

Boilerplate Controller

<?php
/**
 * Yourcompany.com
 *
 * PHP Version 5
 *
 * @category  Namespace
 * @package   Namespace_Module
 * @author    Your Name <your.name@yourcompany.com>
 * @copyright 2012 yourcompany.com
 * @license   http://www.yourcompany.com/license.txt Your license
 * @link      N/A
 */

/**
 * A description of the controller
 *
 * @category Namespace
 * @package  Namespace_Module
 * @author   Your Name <your.name@yourcompany.com>
 * @license  http://www.yourcompany.co...

Paypal Express Flow

The PayPal Express Checkout Integration Guide is a great
source of well-written information for anything relating to the Express
checkout, including information regarding all of the API calls. Also, the PayPal
Express Checkout flow diagram illustrates the process well:

The same information as contained in the diagram above is also in the table
below:

|-------------------------------------------------------------------+-------------...

Disabling A Category

For some reason this does not work:

$category->setIsActive(false);

You must use this instead:

$category->setIsActive(0);

Stop Controller Dispatch

In an action controllers preDispatch() method you can stop dispatch by calling the following:

$this->setFlag('', self::FLAG_NO_DISPATCH, true);

If you were listening via an observer, your code would look like this:

public function someObserver(Varien_Event_Observer $observer)
{
    $action = $observer->getEvent()->getControllerAction();
    $action->setFlag('', Mage_Core_Controller_Varien_Action::FLAG_NO_DISPATCH, true);
}

Get A Products Stock Quantity

$product->getStockItem()->getQty();

Yes, there is no method for it - it's set using setStockItem() from Mage_CatalogInventory_Model_Stock_Item::assignProduct() via the catalog_product_load_after event.