0

I'm struggling with the following thing. Because I would like to write my own kind of filter for Related Products, I will have to rewrite the class "Mage_Catalog_Block_Product_List_Related". While there has been proven the class I had to rewrite is being called, I cannot seem to edit anything inside the protected function "_prepareData". It isn't being called.

The situation currently: I have a class named "My_Module_Block_Catalog_Product_List_Related" which is extending the class "Mage_Catalog_Block_Product_List_Related". I have copied the protected function "_prepareData()" from the original class (app/code/core/Mage/Catalog/Block/Product/List/Related.php). But this function is not being called. If I test my class (if it is being called) by adding the following line of code:

public function __construct() {
    $this->_prepareData();
}

and I add an echo to the protected function "_prepareData()" I can see my text is being echo'd correctly. However, the rest of the protected function "_prepareData()" is being ignored. Also, if I remove the construct, the text will not be echo'd anymore.

I would like to show you my class and the original class.

My class

class My_Module_Block_Catalog_Product_List_Related extends Mage_Catalog_Block_Product_List_Related {

    protected $_itemCollection;


    protected function _prepareData()
    {
        echo 'Test';
        $product = Mage::registry('product');
        /* @var $product Mage_Catalog_Model_Product */

        $this->_itemCollection = $product->getRelatedProductCollection()
            ->addAttributeToSelect('required_options')
            ->setPositionOrder()
            ->addStoreFilter()
        ;


        if (Mage::helper('catalog')->isModuleEnabled('Mage_Checkout')) {
            Mage::getResourceSingleton('checkout/cart')->addExcludeProductFilter($this->_itemCollection,
                Mage::getSingleton('checkout/session')->getQuoteId()
            );
            $this->_addProductAttributesAndPrices($this->_itemCollection);
        }
//        Mage::getSingleton('catalog/product_status')->addSaleableFilterToCollection($this->_itemCollection);
        //Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($this->_itemCollection);
//NOTICE THE COMMENTED LINE ABOVE

        $this->_itemCollection->load();

        foreach ($this->_itemCollection as $product) {
            $product->setDoNotUseCategoryId(true);
        }

        return $this;
    }
}

The class I am extending

class Mage_Catalog_Block_Product_List_Related extends Mage_Catalog_Block_Product_Abstract
{
    /**
     * Default MAP renderer type
     *
     * @var string
     */
    protected $_mapRenderer = 'msrp_noform';

    protected $_itemCollection;

    protected function _prepareData()
    {
        $product = Mage::registry('product');
        /* @var $product Mage_Catalog_Model_Product */

        $this->_itemCollection = $product->getRelatedProductCollection()
            ->addAttributeToSelect('required_options')
            ->setPositionOrder()
            ->addStoreFilter()
        ;

        if (Mage::helper('catalog')->isModuleEnabled('Mage_Checkout')) {
            Mage::getResourceSingleton('checkout/cart')->addExcludeProductFilter($this->_itemCollection,
                Mage::getSingleton('checkout/session')->getQuoteId()
            );
            $this->_addProductAttributesAndPrices($this->_itemCollection);
        }
//        Mage::getSingleton('catalog/product_status')->addSaleableFilterToCollection($this->_itemCollection);
        Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($this->_itemCollection);

        $this->_itemCollection->load();

        foreach ($this->_itemCollection as $product) {
            $product->setDoNotUseCategoryId(true);
        }

        return $this;
    }

    protected function _beforeToHtml()
    {
        $this->_prepareData();
        return parent::_beforeToHtml();
    }

    public function getItems()
    {
        return $this->_itemCollection;
    }

    /**
     * Get tags array for saving cache
     *
     * @return array
     */
    public function getCacheTags()
    {
        return array_merge(parent::getCacheTags(), $this->getItemsTags($this->getItems()));
    }
}

I hope someone is able to help. Thanks!

Edit: My config.xml:

<?xml version="1.0"?>
<config>
    <modules>
        <My_Module>
            <version>0.1.0</version>
        </My_Module>
    </modules>
    <global>

        <blocks>
            <catalog>
                <rewrite>
                    <product_list_related>My_Module_Block_Catalog_Product_List_Related</product_list_related>
                </rewrite>
            </catalog>
        </blocks>
    </global>
</config>
  • show your module config.xml file? did you added rewrite code in that file ? – Akhilesh Patel Nov 30 '15 at 9:55
  • Yeah, it is. That's why _prepareData is being called in the constructor. <?xml version="1.0"?> <config> <modules> <My_Module> <version>0.1.0</version> </My_Module> </modules> <global> <blocks> <catalog> <rewrite> <product_list_related>My_Module_Block_Catalog_Product_List_Related</product_list_related> </rewrite> </catalog> </blocks> </global> </config> For a fixed identation, see my edited question. sorry, hah – Stefan R Nov 30 '15 at 10:00
1

In your module config.xml file, try to add below rewrite code in that file and check if that works

<global>
    <blocks>
        <module>
            <class>My_Module_Block</class>
        </module>
        <catalog>
            <rewrite>
                <product_list_related>My_Module_Block_Catalog_Product_List_Related</product_list_related>
            </rewrite>
        </catalog>
    </blocks>
</global>
  • I have modified my global element into your global element. Unfortunately it didn't change anything. – Stefan R Nov 30 '15 at 10:06

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.