1

I have rewritten admin dashboard block that last most viewed product but it not displaying my collection data.

Thank you In advance

My config.xml is :-

<blocks>
  <adminhtml>
     <rewrite>
       <dashboard_tab_products_viewed>Companyname_Customdashboard_Block_Adminhtml_Dashboard_Tab_Products_Viewed</dashboard_tab_products_viewed>
    </rewrite>
  </adminhtml>
</blocks>

my block code is :-

class Companyname_Customdashboard_Block_Adminhtml_Dashboard_Tab_Products_Viewed extends Mage_Adminhtml_Block_Dashboard_Tab_Products_Viewed 
  {

public function _prepareCollection() {

     $categoryIds = array();
        $entityIds = array();
        $userRoleName = Mage::getSingleton('admin/session')->getUser()->getRole()->getRoleName();
        $userRoleId = Mage::getSingleton('admin/session')->getUser()->getId();
        if($userRoleName == 'Administrators') { 
            if ($this->getParam('website')) {
                $storeIds = Mage::app()->getWebsite($this->getParam('website'))->getStoreIds();
                $storeId = array_pop($storeIds);
            } else if ($this->getParam('group')) {
                $storeIds = Mage::app()->getGroup($this->getParam('group'))->getStoreIds();
                $storeId = array_pop($storeIds);
            } else {
                $storeId = (int)$this->getParam('store');
            }
            $collection = Mage::getResourceModel('reports/product_collection')
                ->addAttributeToSelect('*')
                ->addViewsCount()
                ->setStoreId($storeId)
                ->addStoreFilter($storeId)
                ->setPageSize($this->getParam($this->getVarNameLimit(), $this->_defaultLimit));
                //var_dump($collection->getSelect()->__toString());
            $this->setCollection($collection);
        } else if($userRoleName == 'salesman') {
            $collections = Mage::getModel('newgenray_adminenquiry/storeaddress')
                    ->getCollection()->getData();
            foreach($collections as $collection) {
                $userIds = $collection['user_id'];
                $userId = explode( ',', $userIds );
                if(count($userId)) {
                    if (in_array($userRoleId, $userId)) {
                        $categoryIds[] = $collection['category_id'];
                    }
                }
            }
            $getEntityIds = Mage::getModel('catalog/product')
                    ->getCollection()
                    ->joinField('category_id', 'catalog/category_product', 'category_id', 'product_id = entity_id', null, 'left')
                    ->addAttributeToSelect('*')
                    ->addAttributeToFilter('category_id', array('in' => $categoryIds))
                    ->getData();
            foreach($getEntityIds as $getEntityId) {
                $entityIds[] = $getEntityId['entity_id'];
            }   
            $collection = Mage::getResourceModel('reports/product_collection')
                ->addAttributeToSelect('*')
                ->addViewsCount()
                ->setStoreId($storeId)
                ->addStoreFilter($storeId)
                ->addAttributeToFilter('entity_id', array('in' => $entityIds))
                ->setPageSize($this->getParam($this->getVarNameLimit(), $this->_defaultLimit));;
                //var_dump($collection->getSelect()->__toString());
            $this->setCollection($collection);
        }
        return parent::_prepareCollection();
    }

    }

}

My problem is that core function _prepareCollection() is calling after my block function _prepareCollection() , that's why it take data according to Mage collection.

1

Looks like the problem is with this line

return parent::_prepareCollection();

In that code, you are calling the function of rewrited class. Hence, your changes will again be overrided. It is like Double override.

Instead of this call the Grand parent class.. i.e the class which is being extended by the original class Mage_Adminhtml_Block_Dashboard_Tab_Products_Viewed i.e Mage_Adminhtml_Block_Dashboard_Grid

So, the code is:

return Mage_Adminhtml_Block_Dashboard_Grid::_prepareCollection();

instead of

return parent::_prepareCollection();

You might find my Blog helpful. Where I shared my experiences.

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.