i'm trying to use upsell template of magento to display a list of products that have same specific attribute, as @Amit Bera suggested in this question Using upsells products template.
I created a custom module with namespace Mymodule and name "Samecollection" so the structure is /magento/app/code/local/Mymodule/Samecollection/Block/Product/List
. Inside it i created a block class Customcollection that inherits Mage_Catalog_Block_Product_List_Upsell
as follow :
class Mymodule_Samecollection_Block_Product_List_Customcollection extends Mage_Catalog_Block_Product_List_Upsell {
protected function _prepareData() {
$_product = Mage::registry('product');
$this->_itemCollection = Mage::getModel('catalog/product')->addAttributeToSelect('*')->addAttributeToFilter('author',array('like'=>$product->getAuthor()));
$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();
}
}
I declared the block in /etc/config.xml
of Mymodule_SameCollection
as follow :
<?xml version="1.0" ?>
<config>
<modules>
<Mymodule_Samecollection>
<version>1.0.0</version>
</Mymodule_Samecollection>
</modules>
<global>
<!-- start of block -->
<blocks>
<samecollection>
<class>Mymodule_Samecollection_Block</class>
</samecollection>
</blocks>
</global>
</config>
I then added my block to the layout inside <catalog_product_view></catalog_product_view>
:
<block type="samecollection/product_list_customcollection" name="same_collection" template="catalog/product/list/upsell.phtml">
</block>
Finally in my product view, i called the layout :
<?php echo $this->getChildHtml('same_collection');?>
I have checked if my module was activated and it is.
I tried adding another block class similar to upsell class to the mage/core/catalog/bloc/product/list
(not recommended, i'm aware of that) in an test environment with another template just to debug and check if my collection is populated and the child is rendered with method getChildHtml()
and it was rendered.
I want to use this module approach but i don't know but i'm doing wrong.