0

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.

0

1 Answer 1

0

Your posted code has an error

In your Mymodule_Samecollection_Block_Product_List_Customcollection class, change this line

$this->_itemCollection = Mage::getModel('catalog/product')->getaddAttributeToSelect('*')->addAttributeToFilter('author',array('like'=>$product->getAuthor()));

for this one

$this->_itemCollection = Mage::getModel('catalog/product')->addAttributeToSelect('*')->addAttributeToFilter('author',array('like'=>$product->getAuthor()));

Notice the change: getaddAttributeToSelect() does not exist (in a default installation, at least)

Besides that, maybe you want to add '%' in your LIKE query to get better results. But this depends by the author attribute definition

UPDATE

In addition to this, you are getting the product from the registry & assigning it to $_product variable, but in your query you are using $product->getAuthor(), without the underscore. Again, a typo error

3
  • It was just a typo ! the problem is that nothing is displayed; like if the block is not called inside view.phtml of product. I tested the collection, it renders products correclty when called directly from view.phtml
    – mockingbid
    Jan 27, 2016 at 17:09
  • In addition to this, you are getting the product from the registry & assigning it to $_product variable, but in your query you are using $product->getAuthor(), without the underscore. Again, a typo error Jan 27, 2016 at 17:14
  • I corrected all the possible typos, i even created a hello world template in the same location as upsell.phtml and i tried to render the layout using that block to no avail. <block type="samecollection/product_list_customcollection" name="same_collection" template="catalog/product/list/helloworld.phtml"></block>
    – mockingbid
    Jan 27, 2016 at 17:31

Your Answer

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

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