1

I am making use of 3rd party free extension for displaying top seller block.

Code is very simple to understand, but when i use getSelect() method to display SQL query i found same query being displayed multiple times instead of once.

I am not sure why its happening.

Here is the below _getProductCollection() function

 protected function _getProductCollection()
    {
        parent::__construct();
        $storeId    = Mage::app()->getStore()->getId();
        $todayDate  = Mage::app()->getLocale()->date()->toString(Varien_Date::DATETIME_INTERNAL_FORMAT);

        $products = Mage::getModel('catalog/product')->getCollection()
            ->addAttributeToSelect('*')
            ->addAttributeToSort("entity_id","DESC")
            ->addAttributeToSelect(array('name', 'price', 'small_image'))
            ->setVisibility(Mage::getSingleton('catalog/product_visibility')->getVisibleInSiteIds())
            ->setOrder($this->get_order(), $this->get_order_dir());
        echo $products->getSelect()."<br>";
        if(Mage::getStoreConfig('catalogextensions/config4/max_product'))
        {
            $products->setPageSize(Mage::getStoreConfig('catalogextensions/config4/max_product'));
        }


        Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($products);
        Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($products);
        Mage::getSingleton('cataloginventory/stock')->addInStockFilterToCollection($products);

        $this->_productCollection = $products;

        return $this->_productCollection;
    }

And below is what i get

enter image description here

same query getting repeated multiple times .

Thank you.

1 Answer 1

1

Change the method you posted to

protected function _getProductCollection()
{
    if (!$this->_productCollection) {
        parent::__construct();
        $storeId    = Mage::app()->getStore()->getId();
        $todayDate  = Mage::app()->getLocale()->date()->toString(Varien_Date::DATETIME_INTERNAL_FORMAT);

        $products = Mage::getModel('catalog/product')->getCollection()
            ->addAttributeToSelect('*')
            ->addAttributeToSort("entity_id","DESC")
            ->addAttributeToSelect(array('name', 'price', 'small_image'))
            ->setVisibility(Mage::getSingleton('catalog/product_visibility')->getVisibleInSiteIds())
            ->setOrder($this->get_order(), $this->get_order_dir());
        echo $products->getSelect()."<br>";
        if(Mage::getStoreConfig('catalogextensions/config4/max_product'))
        {
            $products->setPageSize(Mage::getStoreConfig('catalogextensions/config4/max_product'));
        }


        Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($products);
        Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($products);
        Mage::getSingleton('cataloginventory/stock')->addInStockFilterToCollection($products);

        $this->_productCollection = $products;
    }
    return $this->_productCollection;
}

This should make it load the collection only once.
But this is wrong. You should not call parent::__construct(); in a protected method. Unless you really know what you are doing. I think who wrote this, did not really indent for that.

6
  • I have removed parent::__construct(); and i am getting proper output, but still i see same query multiple times. Commented Aug 5, 2014 at 12:41
  • Maybe the block is instantiated multiple times.
    – Marius
    Commented Aug 5, 2014 at 12:42
  • I have called it only once in a CMS page Commented Aug 5, 2014 at 12:42
  • This is strange. adding if (!$this->_productCollection) { should prevent the code from executing after the first time it does.
    – Marius
    Commented Aug 5, 2014 at 12:43
  • Yes it does prevent it execution, but i found $this->_productCollection already has values and my custom collection are not loading if i place that condition. Commented Aug 5, 2014 at 12:45

Your Answer

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

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