Magento Template Development Cheat Sheet

Magento Template Development Cheat Sheet

eROI

Magento_logo

After spending some time working with Magento (an open-source ecommerce platform that’s been on the development scene for a few years now), I have determined that what their documentation really needs is a WordPress-esque “Template Tags” area.

When you’re just starting out with a new platform, documentation is invaluable, and the documentation for Magento seems woefully lacking in terms of front-end development. (If you have a favorite resource, I’d love to hear about it!)

Here are some tips / shortcuts / code snippets I’ve found useful; maybe they’ll be helpful to new Magento developers as well.

For use in the .phtml files:

PAGE PATH

<?php echo $this->getUrl('myCMSpage'); ?>

IMAGE PATH

<?php echo $this->getSkinUrl('images/button.gif'); ?>

PRODUCT LINK

<?php echo $this->getProductData()->getProductUrl(); ?>

PRODUCT NAME

<?php echo $this->htmlEscape($this->getProductData()->getName()); ?>

DISPLAY ALL OF A PRODUCT’S CATEGORIES

<?php $categories = $_product->getCategoryIds();
foreach($categories as $k => $_category_id):
    $_category = Mage::getModel('catalog/category')->load($_category_id); ?>
    <a href="<?php echo $_category->getUrl() ?>"><?php echo $_category->getName() ?>
<?php endforeach; ?>

USE A STATIC BLOCK IN A TEMPLATE FILE

<?php echo $this->getLayout()->createBlock('cms/block')->
setBlockId('YOURBLOCKID')->toHtml(); ?>

TEST FOR A PRODUCT ATTRIBUTE

<?php if ($this->product['attributename'] == "attributevalue") {
    // your stuff here
} ?>

TEST IF IN CATEGORY

<?php if ($this->getCurrentCategory()->getName() == "My Category") {
    //do some stuff
} ?>

CALL AN ATTRIBUTE ON THE PRODUCT LIST PAGE

<?php if ($_product->getData('attributename')):
    $_staff_pick= $_product->getResource()->getAttribute ('attributename')->getFrontend()->getValue($_product);
    if ($_attributename == "Yes"){
        // DISPLAY ATTRIBUTE
    }
endif; ?>

MORE FUNCTIONS THAT MAY COME IN HANDY

$this->getCurrentCategory()->getImageUrl();
$this->getCurrentCategory()->getDescription();
$this->IsTopCategory();
$this->getProductListHtml();
$this->getCmsBlockHtml();

OTHER PRODUCT ATTRIBUTES YOU CAN CALL
Using $this->product['attributename'] syntax:
(Values for these attributes are here just as examples; Yours will likely be different.)

[store_id] => 1
[entity_id] => 3
[entity_type_id] => 4
[attribute_set_id] => 4
[type_id] => simple
[sku] => 2281
[category_ids] => 2,15,17,23,24
[created_at] => 2008-09-09 16:24:03
[updated_at] => 2008-09-15 22:47:51
[has_options] => 0
[name] => ProductName
[url_key] => productname
[gift_message_available] => 2
[meta_title] =>
[meta_description] =>
[image] => /p/r/product_productname.gif
[small_image] => /p/r/product_productname.gif
[thumbnail] => /p/r/product_productname.gif
[custom_design] => default/yourthemefolder
[options_container] => container2
[url_path] => productname.html
[status] => 1
[visibility] => 4
[tax_class_id] => 0
[weight] => 1.0000
[price] => 37.9900
[description] =>
[short_description] =>
[meta_keyword] =>
[custom_layout_update] =>
[media_gallery] => Array
[tier_price] => Array
[stock_item] =>

For use from within a Magento CMS page:

CALL A STATIC BLOCK FROM WITHIN A CONTENT AREA

{{block type="cms/block" block_id="your_block_identifier" template="cms/content.phtml"}}

IMAGE PATH FROM WITHIN CMS

{{skin url="images/logo.gif"}}

Notes

I work off a file very similar to this and reference it all the time. Feel free to save any or all of these examples into a file that you can reference when you’re developing a Magento theme – I know that when I was first starting out in Magento I would have killed to have a reference like this to start from. Anybody else have some cheat-sheet snippets for Magento they’d like to share?

Magento_logo

After spending some time working with Magento (an open-source ecommerce platform that’s been on the development scene for a few years now), I have determined that what their documentation really needs is a WordPress-esque “Template Tags” area.

When you’re just starting out with a new platform, documentation is invaluable, and the documentation for Magento seems woefully lacking in terms of front-end development. (If you have a favorite resource, I’d love to hear about it!)

Here are some tips / shortcuts / code snippets I’ve found useful; maybe they’ll be helpful to new Magento developers as well.

For use in the .phtml files:

PAGE PATH

<?php echo $this->getUrl('myCMSpage'); ?>

IMAGE PATH

<?php echo $this->getSkinUrl('images/button.gif'); ?>

PRODUCT LINK

<?php echo $this->getProductData()->getProductUrl(); ?>

PRODUCT NAME

<?php echo $this->htmlEscape($this->getProductData()->getName()); ?>

DISPLAY ALL OF A PRODUCT’S CATEGORIES

<?php $categories = $_product->getCategoryIds();
foreach($categories as $k => $_category_id):
    $_category = Mage::getModel('catalog/category')->load($_category_id); ?>
    <a href="<?php echo $_category->getUrl() ?>"><?php echo $_category->getName() ?>
<?php endforeach; ?>

USE A STATIC BLOCK IN A TEMPLATE FILE

<?php echo $this->getLayout()->createBlock('cms/block')->
setBlockId('YOURBLOCKID')->toHtml(); ?>

TEST FOR A PRODUCT ATTRIBUTE

<?php if ($this->product['attributename'] == "attributevalue") {
    // your stuff here
} ?>

TEST IF IN CATEGORY

<?php if ($this->getCurrentCategory()->getName() == "My Category") {
    //do some stuff
} ?>

CALL AN ATTRIBUTE ON THE PRODUCT LIST PAGE

<?php if ($_product->getData('attributename')):
    $_staff_pick= $_product->getResource()->getAttribute ('attributename')->getFrontend()->getValue($_product);
    if ($_attributename == "Yes"){
        // DISPLAY ATTRIBUTE
    }
endif; ?>

MORE FUNCTIONS THAT MAY COME IN HANDY

$this->getCurrentCategory()->getImageUrl();
$this->getCurrentCategory()->getDescription();
$this->IsTopCategory();
$this->getProductListHtml();
$this->getCmsBlockHtml();

OTHER PRODUCT ATTRIBUTES YOU CAN CALL
Using $this->product['attributename'] syntax:
(Values for these attributes are here just as examples; Yours will likely be different.)

[store_id] => 1
[entity_id] => 3
[entity_type_id] => 4
[attribute_set_id] => 4
[type_id] => simple
[sku] => 2281
[category_ids] => 2,15,17,23,24
[created_at] => 2008-09-09 16:24:03
[updated_at] => 2008-09-15 22:47:51
[has_options] => 0
[name] => ProductName
[url_key] => productname
[gift_message_available] => 2
[meta_title] =>
[meta_description] =>
[image] => /p/r/product_productname.gif
[small_image] => /p/r/product_productname.gif
[thumbnail] => /p/r/product_productname.gif
[custom_design] => default/yourthemefolder
[options_container] => container2
[url_path] => productname.html
[status] => 1
[visibility] => 4
[tax_class_id] => 0
[weight] => 1.0000
[price] => 37.9900
[description] =>
[short_description] =>
[meta_keyword] =>
[custom_layout_update] =>
[media_gallery] => Array
[tier_price] => Array
[stock_item] =>

For use from within a Magento CMS page:

CALL A STATIC BLOCK FROM WITHIN A CONTENT AREA

{{block type="cms/block" block_id="your_block_identifier" template="cms/content.phtml"}}

IMAGE PATH FROM WITHIN CMS

{{skin url="images/logo.gif"}}

Notes

I work off a file very similar to this and reference it all the time. Feel free to save any or all of these examples into a file that you can reference when you’re developing a Magento theme – I know that when I was first starting out in Magento I would have killed to have a reference like this to start from. Anybody else have some cheat-sheet snippets for Magento they’d like to share?

If you like this story subscribe to our newsletter.

Get updates directly to your email and never miss a thing.

Thanks!

This entry was posted in Development, Tips & Tricks. Bookmark the permalink. Trackbacks are closed, but you can post a comment.
Google+