Question

I would like to use the same template(catalog/product/list/upsell.phtml) to display a collection of products that share particular attributes with a specific product. The goal is to have the same design for both list of items. One way is to edit the view product file and add some php code. Is there any better why to do so and respect the standards of magento ?

Was it helpful?

Solution

Define new block type:

In magento, every template file (.phtml) have block class ,that means catalog/product/list/upsell.phtml also have a block class. Mage_Catalog_Block_Product_List_Upsell.

upsell.phtml call Call getItems() which is give the collection.

So for your case,you need create a block class which extend Mage_Catalog_Block_Product_List_Upsell and on new class at function _prepareData function you need add your custom collection.

<?php
class [ModuleNameSpace]_[ModuleName]_Block_Product_List_Customcollection extends Mage_Catalog_Block_Product_List_Upsell
{

    protected function _prepareData()
    {
        /* @var $product Mage_Catalog_Model_Product */
        /* Make  Custom Collection*/
        $this->_itemCollection =Mage::getResourceModel('catalog/product_collection');
         $this->_itemCollection->_addProductAttributesAndPrices($this->_itemCollection)
            ->addStoreFilter();

        Mage::getSingleton('catalog/product_status')->addSaleableFilterToCollection($this->_itemCollection);
        Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($this->_itemCollection);
        $this->_itemCollection->load();
        return $this;
    }

}

Now ,you can call this block by below code:

$this->getLayout()->createBlock('BLock_PreFix/product_list_customcollection')->setTemplate('catalog/product/list/upsell.phtml')->toHtml()

Or call at layout file by

<block type="BLock_PreFix/product_list_customcollection" name="custom_collection" template="catalog/product/list/upsell.phtml" />

Module Approach:

  • First of all create module control file Module name as Amit_Custommodule.xml at app/etc/modules/

<?xml version="1.0"?> <config> <modules> <Amit_Custommodule> <codePool>community</codePool> <active>true</active> </Amit_Custommodule> </modules> </config>

  • Create a config.xml ,where we define blocks type for module

<?xml version="1.0" ?> <config> <modules> <Amit_Custommodule> <version>1.0.0</version> </Amit_Custommodule> </modules> <global> <!-- start of block --> <blocks> <custommodule> <class>Amit_Custommodule_Block</class> </custommodule> </blocks> </global> </config>

  • Create block file: Customcollection.php

    app\code\community\Amit\Custommodule\Block\Product\List\

<?php
class Amit_Custommodule_Block_Product_List_Customcollection extends Mage_Catalog_Block_Product_List_Upsell
{

    protected function _prepareData()
    {
        $this->_itemCollection =Mage::getResourceModel('catalog/product_collection');
         $this->_itemCollection->_addProductAttributesAndPrices($collection)
            ->addStoreFilter();

       Mage::getSingleton('catalog/product_status')->addSaleableFilterToCollection($this->_itemCollection);
        Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($this->_itemCollection);


        $this->_itemCollection->load();



        return $this;
    }

}
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top