문제

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.

도움이 되었습니까?

해결책

Replace the function with the below.

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 Mage_Adminhtml_Block_Dashboard_Grid::_prepareCollection();// change here
    }

    }

More Information Here

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top