0

I need to customize the list display only for the home page. I've duplicated catalog/produtcs/list.phtml to catalog/produtcs/list_home.phtml and in the home page content, in the CMS section of the admin I've added the code:

{{block type="catalog/product_list_home" name="home.catalog.product.list" alias="produtos_home" template="catalog/product/list_home.phtml"}}

But that just didn't load anything. How do I register this "new" block?

Thanks in advance.

4
  • Does the block with the alias catalog/product_list_home exist?
    – Marius
    Dec 12, 2014 at 12:32
  • I'm sorry I'm noob with Magento, where do I create it? Dec 12, 2014 at 12:34
  • What products are you trying to display on the homepage? Is there a special category that contains these products?
    – Marius
    Dec 12, 2014 at 12:37
  • The last 8 products, no specific category. Dec 12, 2014 at 12:38

2 Answers 2

1

To create the collection for the latest 8 products, in list_home.phtml replace this (should be at the start of the file):

$_productCollection=$this->getLoadedProductCollection();

with this:

$_productCollection= Mage::getModel('catalog/product')
                ->getCollection()                   
                ->addAttributeToSort('created_at', 'desc');   
$_productCollection->getSelect()->limit(8);

Then in your CMS page:

{{block type="catalog/product_list_home" name="home.catalog.product.list" alias="produtos_home" column_count="4" template="catalog/product/list_home.phtml"}}

Please take note of the column_count="4" parameter inside the block declaration, you can change this to suit your theme.

1

You need a new module for that.
Let's call it Easylife_Homepage. Create these files:

app/etc/modules/Easylife_Homepage.xml - the declaration file

<?xml version="1.0"?>
<config>
    <modules>
        <Easylife_Homepage>
            <codePool>local</codePool>
            <active>true</active>
            <depends>
                <Mage_Catalog />
            </depends>
        </Easylife_Homepage>
    </modules>
</config>

app/code/local/Easylife/Homepage/etc/config.xml - the configuration file

<?xml version="1.0"?>
<config>
    <modules>
        <Easylife_Homepage>
            <version>1.0.0</version>
        </Easylife_Homepage>
    </modules>
    <global>
        <blocks>
            <easylife_homepage>
                <class>Easylife_Homepage_Block</class>
            </easylife_homepage>
        </blocks>
    </global>
</config>

app/code/local/Easylife/Homepage/Block/Homepage.php - your block that gets the last added products

<?php
class Easylife_Homepage_Block_Homepage  extends Mage_Catalog_Block_Product_Abstract
{
    const DEFAULT_PRODUCT_LIMIT = 8;
    protected $_products = null;
    public function getProductLimit()
    {
        return $this->getDataSetDefault('product_limit', self::DEFAULT_PRODUCT_LIMIT);
    }
    public function getLoadedProductCollection()
    {
        if (is_null($this->_products)) {
            $products = Mage::getModel('catalog/product')->getCollection()
                ->addAttributeToSelect(Mage::getSingleton('catalog/config')->getProductAttributes())
                ->addMinimalPrice()
                ->addFinalPrice()
                ->addTaxPercents()
                ->addUrlRewrite()
                ->addAttributeToSort('entity_id', 'DESC')
                ->setPage(1, $this->getProductLimit());
            Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($products);
            Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($products);
            $this->_products = $products;
        }
        return $this->_products;
    }
}

Now clear the cache and add this to the homepage content

{{block type="easylife_homepage/homepage" template="catalog/product/list_home.phtml"}}

Save and enjoy.
If you later decide you don't want 8 products but you want 10 just modify what you added in the homepage content to this:

{{block type="easylife_homepage/homepage" template="catalog/product/list_home.phtml" product_limit="10"}}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.