Magento: Category Thumbnail Cropper

This is totally badass.

Magento doesn’t natively support the resizing of category images. Sad Face.

No need to worry though, as Magento Resize Category Images saves the day.

Upload the files to your app/ folder and then you can either:

Resize the image into a square format.

<img src="<?php echo $this->helper('timage')->init($_category->getImageUrl())->resize(120, 120) ?>

Or crop it to your own dimensions.

<img src="<?php 
    echo $this->helper('timage')
              ->init($_category->getImageUrl())
              ->setWidth(230)
              ->setHeight(200)
              ->crop()
              ->resize() 
    ?>" alt="alt text"/>

Simple.

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";
 
Mage::app()->setCurrentStore(Mage::getModel('core/store')->load(Mage_Core_Model_App::ADMIN_STORE_ID));
 
$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.
 
$installer->endSetup();

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 Mcnab.co, 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') ?>

Phew.

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>';
endif;