Find Unused Product Attributes

Just to keep things tidy!

SELECT attribute_id, attribute_code, frontend_label
FROM eav_attribute
WHERE entity_type_id = 4
AND attribute_id NOT IN(SELECT attribute_id FROM eav_entity_attribute);

Product EAV Attribute Set, Group and Attribute List

To see a list of all attribute sets, groups, and associated attributes for products, use this SQL. BONUS! Also shows the sort ordering, useful for determining which sort_order you need to place an attribute in a certain position:

SELECT s.attribute_set_name,
       g.attribute_group_name,
       a.attribute_code,
       a.frontend_label,
       ea.sort_order
FROM eav_attribute_set s
LEFT JOIN eav_attribute_group g   ON s.attribute_set_id   = g.attribute_set_id
LEFT JOIN eav_entity_attribute ea ON g.attr...

Import Not Working Until You Save

This is because you've missed out _product_websites, for which all products should have a value of base

Importing Images

Ok so to import images you first need these columns:

  • _media_attribute_id - the attribute_id of media_gallery (see below)
  • _media_image - the path of the image, i.e. /some_image.jpg
  • _media_lable - the image label, i.e. "My nice image"
  • _media_position - the position, 1, 2 etc
  • _media_is_disabled - whether the image is excluded from the gallery or not

You do the whole weird multi-line thing to import multiple images. Every image
you use (whether it's in the gallery, or being used for the image, small_image
or `thumbn...

Full Module Configuration Reference

<!-- # Module Initialisation
    app/etc/modules/Namespace_ModuleName.xml -->

<config>
    <modules>
        <Namespace_ModuleName>
            <active>true</active>
            <codePool>local</codePool>
        </Namespace_ModuleName>
    </modules>
</config>

<!-- # Module Configuration
    app/code/local/Namespace/ModuleName/etc/config.xml -->

<config>

    <modules>
        <Nmespace_ModuleName>
            <version>0.0.1</version>
        </Nmespace_ModuleName>

...

Config Helper Rewrite Definition

<config>
    <global>
        <helpers>
            <catalog>
                <rewrite>
                    <data>Namespace_Module_Helper_Catalog_Data</data>
                </rewrite>
            </catalog>
        </helpers>
    </global>
</config>

Config Block rewrite Definition

<config>
    <global>
        <blocks>
            <catalog>
                <rewrite>
                    <product>Namespace_Module_Block_Catalog_Product</product>
                </rewrite>
            </catalog>
        </blocks>
    </global>
</config>

Helper Block Definition

<config>
    <global>
        <helpers>
            <namespace_module>
                <class>Namespace_Module_Helper</class>
            </namespace_module>
        </helpers>
    </global>
</config>

Pretty Debug Output

Yeh it's not Magento, but this might help some people:

Zend_Debug::dump()

Basically it's var_dump but with <pre> tags, no biggie but it's nice to use.

Return Store Variables (Phone Number, Address, E-Mail etc)

Name:

Mage::getStoreConfig('general/store_information/name')

Phone number:

Mage::getStoreConfig('general/store_information/phone')

Address:

Mage::getStoreConfig('general/store_information/address')

E-Mail:

Mage::getStoreConfig('trans_email/ident_general/email')

Get Category Children

public function getCategories() 
{
    if (!$this->categories) {
        $categoryId = $this->getData('parent_category_id');
        $parentCategory = Mage::getModel('catalog/category')->load($categoryId);

        $categoryCollection = $parentCategory->getCollection();
        $categoryCollection->addAttributeToSelect('url_key')
            ->addAttributeToSelect('name')
            ->addAttributeToSelect('image')
            ->addAttributeToFilter('is_active', 1)
            ->addAttributeToFilte...

Get and Resize Category Images

public function getCategoryImage(Mage_Catalog_Model_Category $category, $width = 250, $height = 250)
{
    // return when no image exists
    if (!$category->getImage()) {
        return false;
    }

    // return when the original image doesn't exist
    $imagePath = Mage::getBaseDir('media') . DS . 'catalog' . DS . 'category'
               . DS . $category->getImage();
    if (!file_exists($imagePath)) {
        return false;
    }

    // resize the image if needed
    $rszImagePat...

Boilerplate Resource Model Collection Class (Before CE 1.6 / EE 1.11)

/**
 * 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
 */

/**
 * Something resource collection model
 *
 * @category Namespace
 * @package  Namespace_Module
 * @author   Your Name <your.name@yourcompany.com>
 * @license  http://www.yourcompany.com/lice...

Boilerplate Resource Model Class (Before CE 1.6 / EE 1.11)

/**
 * 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
 */

/**
 * Something resource model
 *
 * @category Namespace
 * @package  Namespace_Module
 * @author   Your Name <your.name@yourcompany.com>
 * @license  http://www.yourcompany.com/license.txt You...

Fetching All Attributes For An Entity From The DB In One Query

Using the database to inspect EAV-based entity types can be a real pain in the ass, so here are a bunch of queries that let you inspect some popular EAV entity types easily. This practice can be applied to any EAV entity type, however as there are about 25 of them, I decided to just list the popular ones, enjoy.

Output example (shortened):

|----------------------------------+----------+----------+---------------------|
| attribute_code | type | store_id | value |
|----------------------------------+-----...

Add All Attributes To A Collection

$attributes = Mage::getSingleton('catalog/config')->getProductAttributes();
$collection->addAttributeToSelect($attributes);

Add A Tab To The Admin Product Screen

Declare an admin layout XML file for your module in your config.xml:

<config>
    <adminhtml>
        <layout>
            <updates>
                <your_module>
                    <file>your-module.xml</file>
                </your_module>
            </updates>
        </layout>
    </adminhtml>
</config>

Create the layout XML in /app/design/adminhtml/default/default/layout/your-module.xml:

<?xml version="1.0"?>
<layout>
    <adminhtml_catalog_product_edit>
        <refer...

Add A Tab To The Admin Product Screen

Adding A Tab To The Product Screen

  1. Declare an admin layout XML file for your module in your config.xml:

  2. Create the layout XML in /app/design/adminhtml/default/default/layout/your-module.xml: