仪表板网格块重写无法正常工作

时间:2014-05-13 06:06:31

标签: php magento

我重写了管理仪表板块,这是最常查看的产品,但它没有显示我的收藏数据。

提前谢谢

我的config.xml是: -

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

我的区块代码是: -

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();
    }

    }

}

我的问题是核心函数_prepareCollection()在我的块函数_prepareCollection()之后调用,这就是它根据Mage集合获取数据的原因。

1 个答案:

答案 0 :(得分:0)

用下面的替换功能。

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
    }

    }

更多信息Here