0

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 ?

  • Can u please share your code – Amit Bera Jan 25 '16 at 16:26
0

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;
    }

}
  • I used the module approach and called the layout but the products are not displayed, i tested by trying to display a single product using this code : $this->_itemCollection = Mage::getResourceModel('catalog/product_collection')->getCollection()->addAttributeToSelect('*')->addAttributeToFilter->('entity_id',array('eq' => 443035); but without success ? I made sure the product id exists in my database. – mockingbid Jan 26 '16 at 16:18
  • code seem to be wrong: ->addAttributeToFilter->('entity_id',array('eq' => 443035) to be ->addAttributeToFilter('entity_id',array('eq' => 443035)); or ->addIdFilter(array(443035)) – Amit Bera Jan 26 '16 at 16:21
  • My class : class Mymodule_Samecollection_Block_Product_List_Customcollection extends Mage_Catalog_Block_Product_List_Upsell { protected function _prepareData() { $_product = Mage::registry('product'); Collection()->addAttributeToSelect('*')->addAttributeToFilter->('entity_id',array('eq' => 443035)); // rest of the code } - My block : <block type="samecollection/product_list_customcollection" name="custom_collection" template="catalog/product/list/upsell.phtml" /> the call from view.phtml : <?php echo $this->getChildHtml('custom_collection')?> – mockingbid Jan 26 '16 at 16:32
  • I still have the problem. I posted above my code to make things clearer. – mockingbid Jan 26 '16 at 16:49
  • why you have do mistake same syntax >addAttributeToFilter->('entity_id',arra‌​y('eq' => 443035)); error – Amit Bera Jan 26 '16 at 17:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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