Custom Magento EAV Attributes

Magento has an Entity Attribute Value system in the db, neaning you can easily add custom stuff by adding rows and not columns! But that’s all done for us, so here’s how we set it up. In this example I’m wanting a home page image radio button.

In the config.xml you’ll see something like


So in this example we would bump that up to 0.3.0. Also, check you have a setup key in the xml:


Next, in the sql folder of your module, create a file called madskull_homepage_setup/upgrade-0.2.0-0.3.0.php (thats upgrading from v0.2.0 to v0.3.0) and I put the following:


/* @var $installer Mage_Catalog_Model_Resource_Setup */
$installer = $this;


        'frontend'                      => 'catalog/product_attribute_frontend_image',
        'global'                        => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
        'visible'                       => true,
        'used_in_product_listing'       => true,
        'filterable'                    => false,
        'filterable_in_search'          => false,
        'used_for_price_rules'          => false,
        'searchable'                    => false,
        'comparable'                    => false,
        'visible_on_front'              => true,
        'used_for_sort_by'              => false,
        'is_visible_in_advanced_search' => false,
        'type'                          => 'varchar',
        'input'                         => 'media_image',
        'group'                         => 'Images',
        'label'                         => 'Homepage Image',
        'user_defined'                  => true,
        'is_configurable'               => false,
        'required'                      => false


When you next visit any page, Magento will see the version has changed, and attempt to upgrade it. If successful, it will appear in the eav_attribute table. Clear your caches,  and reindex. Now if you go into your products in the admin panel, and select any product to edit, click on the Images option, and you should see our new attribute in there as a radio button! 🙂

Now you can use your variable by doing things like $collection->addAttributeToSelect([‘name’, ‘price’, ‘special_price’, ‘homepage_image’]);  and so on! 😀

Globally install PHPUnit on a puPHPet box

Phpunit is being removed from Pear, so the best way to install it now is by creating a which you drop into your puphpet/files/exec-once-unprivileged directory:

composer global require "phpunit/phpunit=4.4.*"
export PATH=$PATH:~/.composer/vendor/bin/

Then just vagrant provision, and that should be it!

Creating custom Magento modules

Modules in Magento allow you to code additional functionality into your magento store. Today I’m making a module for a Pinterest competition!

First thing we do is create the file and folder structure in app/code/local/YOURLIB/YOURMODULE :


In the etc folder, create config.xml:











To activate our module, we add a config file app/etc/modules/Madskull_Pinterest.xml :


Finally to get up and running add the controller code in controllers/IndexController.php :


Now reload the page, and you’ll have all the template gubbins rendering too.

Your module is primed and ready for you to start developing!

Getting the Magento Base Paths and URL’s

Just a little reference:

 Mage::getBaseDir(‘app’) => Get app directory path
 Mage::getBaseDir(‘media’) => Get media directory path
 Mage::getBaseDir(‘design’) => Get design directory path
 Mage::getBaseDir(‘code’) => Gives code directory file path
 Mage::getBaseDir(‘lib’) => Gives lib directory file path
 Mage::getBaseDir(‘skin’) => Gives skin directory file path
 Mage::getBaseDir(‘var’) => Gives var directory file path
 Mage::getBaseDir(‘cache’) => Gives cache directory file path
 Mage::getBaseDir(‘log’) => Gives log directory file path


 Mage::getBaseUrl() => Get base url path e.g.
 Mage::getBaseUrl('media') => Get MEDIA folder path e.g.
 Mage::getBaseUrl('js') => Get JS folder path e.g.
 Mage::getBaseUrl('skin') => Get SKIN folder path e.g.


Static Blocks

{{base url=''}}
{{skin url='images/skinimage.jpg'}}
{{media url='/mediaimage.jpg'}}
{{store url=''}}

Export WordPress post URLs

I’m making some Apache redirects from an old domain to a new domain. I need the old domains blog posts to redirect to the new domain. So I need the WordPress permalinks.

In MySQL, you can say the following to get your permalinks.

SELECT wpp.post_title, wpp.guid,wpp.post_date,
    wpo.option_value,'%year%', date_format(wpp.post_date,'%Y'))
    ,'%postname%',wpp.post_name )
    ,'%category%',wpc.slug )
) as permalink
FROM wp_posts wpp
INNER JOIN wp_options wpo ON wpo.option_name='permalink_structure'
INNER JOIN wp_options wpo_su ON wpo_su.option_name='siteurl'
    SELECT wtr.object_id ID, max(wpt.slug) slug
    FROM wp_term_relationships wtr
    INNER JOIN wp_term_taxonomy wtt ON wtt.term_taxonomy_id=wtr.term_taxonomy_id AND wtt.taxonomy='category'
    INNER JOIN wp_terms wpt on wpt.term_id=wtt.term_id
    GROUP BY  wtr.object_id
) wpc ON wpc.ID=wpp.ID
WHERE wpp.post_type = 'post'
AND wpp.post_status = 'publish'
ORDER BY wpp.post_date DESC

Developing a Magento Home Page

I’m on chapter 3 of my book, and you’ve stumble upon my notes! So far, I have made a custom theme, brought in bootstrap js and css, create the main theme templates, and created blocks and cms-blocks. Now I’m on chapter three, its time to get a groovy looking front page:

custom magento home page
as it will look by the end of chapter 3

As you can see, there’s a slider row, and we’ll have some more cms blocks for the carousel, banners, and home page proper.


First up, lets create the slider row. In the admin panel, goto CMS > Static Blocks, and add a block called Home Block Full width, labelled home-fullwidth:

<div class="row">
     <div class="col-md-8">
       {{block type="cms/block" block_id="slider-home" template="cms/content.phtml"}}
     <div class="col-md-4">
       {{block type="cms/block" block_id="home-right-banner" template="cms/content.phtml"}}

Using the {{ blahblah }} puts the CMS blocks in place. Now we add the slider-home cms block:

<div data-ride="carousel" class="carousel slide" id="slider-home">
    <ol class="carousel-indicators">
        <li data-slide-to="0" data-target="#slider-home"></li>
        <li data-slide-to="1" data-target="#slider-home" class="active"></li>
        <li data-slide-to="2" data-target="#slider-home" class=""></li>
    <div class="carousel-inner">
        <div class="item">
            <img src="{{skin_url='images/slide1.png'}}" alt="Slide 1 Alt Text">
        <div class="item active">
            <img src="{{skin_url='images/slide1.png'}}" alt="Slide 2 Alt Text">
        <div class="item">
            <img src="{{skin_url='images/slide1.png'}}" alt="Slide 3 Alt Text">
    <a data-slide="prev" href="#slider-home" class="left carousel-control">
        <span class="glyphicon glyphicon-chevron-left"></span>
    <a data-slide="next" href="#slider-home" class="right carousel-control">
        <span class="glyphicon glyphicon-chevron-right"></span>

The {{skin_url}} stuff is like the XML config. It looks in the public folder for that theme for images, css, and javascript.

The 3 banners next to the slider go in  cms block home-right-banner :

In the Admin panel, click through to CMS Pages, you’ll see home page is already there. Click on it, and in the content tab, stick some placeholder text (I like Zombie Ipsum!) and in the design tab switch layout to 2 columns with left bar.

Earlier in my previous post we made CMS block home-fullwidth (a bootstrap row with two columns). However, if we added that CMS block to our Home Page, it would come out wrong, as it’ll appear in the right hand column of the layout. To sort that, we add a thing called a reference block. We add another bit of config to /app/design/frontend/bookstore/default/layout/local.xml, in the section <reference name=”root”> :

<!-- define the reference block slider -->
<block type="core/text_list" name="slider" as="slider" translate="label">
    <label>Slider Block</label>

Now if you open your template files (in app/design/frontend/bookstore/default/ template/page), you would see we already added the following:

<section id="after-header">
    <div class="container">          
        <?= $this->getChildHtml('slider') ?> 

So all we need to do to is update the layout XML and tell the slider block which cms blocks to load in:

<reference name="slider">
    <block type="cms/block" name="home-fullwidth" as="home-fullwidth">
        <action method="setBlockId">

Bob is now your uncle. If you refresh, you’ll see our slider and right panel 🙂

Again, it looks better styled:

/* Carousel */
#after-header {
    background-color: #efefef;
#slider-home {
    padding:40px 0;
#slider-home .carousel-control.right,#slider-home .carousel-control.
   left {
#slider-home .carousel-indicators {
/* Carousel Bullets */
.carousel-indicators li {
.carousel-indicators li:hover {
#slider-home .carousel-indicators .active {
/* 3 Banners */
#banner-home {

#banner-home div {
    margin:25px 0;
    background: #ddd;
#banner-home h3 {
/* Text Banners Background Colors */
#banner-home {
#banner-home {
#banner-home {

Main content Row

You’ll notice in the main content left bar that it’s displaying some placeholder text with a 555 nphone number. This is a default template coming from the base theme. Again we need to put some remove tags in our xml file. To find these when you are working away, look in the base/default/layout xml files to find the name of the blocks you want: In our <reference name=”left”> section:

<reference name="left">
    <remove name="left.newsletter"/>
    <remove name="left.permanent.callout"/>
    <remove name="tags_popular"/>

Ok we need our vertical Nav menu on the left. Create a new block again in the left reference:

<block type="catalog/navigation" before="-" name="leftNav" as="leftNav" template="catalog/navigation/leftnav.phtml"/>

Create the catalog/navigation/leftnav.phtml in your template folder.

<div class="block block-leftnav">
    <div class="block-title">
        <strong><?= $this->__('Browse Category'); ?></strong>
    <div class="block-content">
            <?php foreach ($this->getStoreCategories() as $_category) {
                    echo $this->drawItem($_category);
            } ?>

Again, adding CSS:

/* Left Menu */
.block-leftnav {
    margin-top: 35px;

.block-leftnav .block-title {
    font-size: 16px;
    border-bottom: 1px solid #E1E1E1;
    padding-bottom: 10px;

.block-leftnav .block-content {

.block-leftnav .block-content ul {
    padding: 0;
    list-style-position: inside;

.block-leftnav .block-content ul li {

.block-leftnav .block-content ul {
    background: #333;
    color: #fff;
    padding-left: 10px;

.block-leftnav .block-content ul a {
    color: #FA9221;

.block-leftnav .block-content ul li a {
    font-size: 13px;
    color: #333

.block-leftnav .block-content ul li a:hover {
    color: #FA9221;

.block-leftnav .block-content > ul > li.level0 {
    border-bottom: 1px solid #E1E1E1;
    padding: 10px;

.block-leftnav .block-content > ul > li.level0:last-child {
    border-bottom: 0;

.block-leftnav .block-content > ul > li.level0:hover {
    background-color: #efefef

.block-leftnav .block-content > ul > li.level0 > a {
    font-weight: bold;

.block-leftnav .block-content ul ul {
    padding-left: 15px;

.block-leftnav .block-content ul ul ul a {
    color: #999

 Main content

Our main content has placeholder text so far. We want some text at the top, a block of products from a particular category, and a block of text information for each product. Create default/template/catalog/product/list-home.phtml


$collection = $this->getLoadedProductCollection();
$helper = $this->helper('catalog/output');

if (!$collection->count()){ ?>
    <p class="note-msg"><?= $this->__('There are no products matching the selection.') ?></p>
<?php } else { ?>
    <div class="category-products">

        $count = $collection->count() ;
        $columns = $this->getColumnCount();
        $i = 0;

        foreach ($collection as $_product) {
            if ($i++ % $columns == 0) { ?>
                <ul class="products-grid row">
            <?php } ?>
            <li class="item<?php if (($i - 1) % $columns == 0) { ?>first<?php } elseif ($i % $columns == 0) { ?> last<?php } ?> col-md-4">

                <a href="<?= $_product->getProductUrl() ?>"
                   title="<?= $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>"
                    <img src="<?= $this->helper('catalog/image')->init($_product, 'small_image')->resize(250, 310); ?>"
                         alt="<?= $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>"
                         class="img-responsive img-thumbnail"/>

                <h3 class="panel-title product-name">
                    <a href="<?= $_product->getProductUrl() ?>"
                       title="<?= $this->stripTags($_product->getName(), null, true); ?>">
                        <?= $helper->productAttribute($_product, $_product->getName(), 'name') ?>

                <div class="pull-left"> <?= $this->getPriceHtml($_product, true) ?> </div>
                <div class="pull-right">
                    <?php if ($_product->isSaleable()) { ?>
                        <button type="button" title="<?= $this->__('Add to Cart') ?>"
                                class="btn btn-warning btn-cart"
                                onclick="setLocation('<?= $this->getAddToCartUrl($_product) ?>')">
                            <?= $this->__('Add to Cart') ?>
                    <?php } else { ?>
                        <p class="btn btn-alert availability out-of-stock"><span><?= $this->__('Out of stock'); ?></span></p>
                    <?php } ?>
            <?php if ($i == 6) {break;}
                  if ($i % $columns == 0 || $i == $count){?> </ul> <?php } ?>
        <?php } ?>
<?php }

Ok, if you havent played around in the admin panel and created some categories and products, now is the time to do it! Catalog > Manage Categories, & Catalog > Manage Products. Create Books as a subcategory of the default category. Add some subcategories, Fiction, Reference etc!

Add a few products too. Remember to set the categories it will appear in, and set the stock levels to more than zero! Click around, you’ll find it! Anyway, click the book category, and make a note of the ID number. (mine said ‘Books (ID: 3)’). We want to bung the products list block into our CMS home content. Add this to it:

{{block type="catalog/product_list" category_id="3"  template="catalog/product/list-home.phtml"}}

And of course, style it:

/* Product List */
.products-grid {
.products-grid li.item {
    margin-bottom: 30px;
.products-grid .product-name {
.products-grid .panel-footer {
.product-name a {

.price-box p {
.products-grid .product-image {

Job done, our home page is complete!

Other Pages


Apply these techniques and you’ll be able to customise any other blocks in your theme. Take the products grid for example. Turn on the developer config for template path hints. Now go to the home page. Click on the books category. You will see the main products grid looks crap, but you can see that it comes from frontend/base/default/template/catalog/product/list.phtml . Create that file in your theme. Now if you nose into the base themes catalog.xml, you’ll see various blocks defined etc. The products category page has two handles, <catalog_category_default translate=”label”> and <catalog_category_layered translate=”label”>. Layered refers to layered navigation. To use layered navigation, we need to set the Category setting Is Anchor to Yes. You can find that in the Category Display Settings.

To set the default category view to a one column layout, add this in your XML :

     <reference name="root">
        <action method="setTemplate">

And we’ll set a 2 column layout for the ‘layered’ categories:

     <reference name="root">
        <action method="setTemplate">

Now the thing with this is, the 2 columns right template doesnt have a leftnav section, but we have a right section. So we add the following to the catalog_category_layered XML:

    <reference name="root">
        <action method="setTemplate">
    <reference name="right">
        <block type="catalog/layer_view" name="catalog.leftnav" after="currency" template="catalog/layer/view.phtml" before="-"/>

Copy the code from the base layouts, and tweak with your bootstrap classes etc, and style, you dont need me to tell you how dto do that part!


Again looking at the template paths, you’ll find it in app/design/frontend/base/default/template/catalog/product/view.phtml and the layout handle is <catalog_product_view>. I’m setting it to a one column layout:

    <reference name="root">
        <action method="setTemplate">

The rest

Cart page.


Login Area.


The user account registration page



By now you should have the idea! Get tweaking!