Magento: How to get a drop down attribute for your category

Today I was required to create different “areas” to display categories. This required the creation of a Category Attribute.

After much deliberation and viewing many articles (including an article by my colleague Tom Cafferkey), I found a way.

To start with, create a file within your root (call it anything), containing the following:

require_once "app/Mage.php";
$installer = new Mage_Sales_Model_Mysql4_Setup;
$attribute = array(
'group' => 'General',
'input' => 'select',
'type' => 'int',
'label' => 'Location of featured category',
'backend' => '',
'visible' => true,
'required' => false,
'visible_on_front' => true,

Now for the fun part. Include this following value in your array:

    'option'   => array (
            'values' => array( //Careful here, I learnt the hard way by using 'value' instead of 'values' :-/
                0 => 'None',
                1 => 'Top',
                2 => 'First Line',
                3 => 'Second Line',
                4 => 'Text',

And then to round it off: (Note I’ve included a quick removal line at the bottom for the clumsy ones out there).

'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL

$installer->addAttribute('catalog_category', 'category_featured', $attribute);

// $installer->removeAttribute('catalog_category', 'category_featured'); //Unquote this if you mess it up and need to remove it.

And that’s it! I hope this saves someone at least an hour or two.

Magento: Not getting a homepage success message.

Spent a pointlessly long time on this.

Thanks to, I sorted it out.

I’d created a custom homepage template which had the following line:

<?php echo $this->getHtml('global_notices') ?>

When instead it should be:

<?php echo $this->getHtml('global_messages') ?>


EDIT: Additionally, if you have products on your homepage with “Add to Cart” buttons, you’ll need a message for that too.

<div id="messages_product_view"><?php echo $this->getMessagesBlock()->toHtml() ?></div>

Plonk that wherever you like.

Getting attributes to show on Recently Viewed block.

I created a Recently Viewed block for use on a homepage and I couldn’t for the life of me get the product attributes to feed through.

$man = Mage::getModel('catalog/product')->load($_product->getId())->getManufacturer();
$roundel = Mage::getModel('catalog/product')->load($_product->getId())->getRoundel();
$roundel_txt =  Mage::getModel('catalog/product')->load($_product->getId())->getAttributeText('roundel');

And I could use these attributes within the item!

if ($roundel):
  echo '<div class="product-roundel roundel-' . $roundel . '">'. $roundel_txt .'</div>';