关于在Magento排序mostviewed 1.7.0.2

问题描述:

产品mostviewed文件位于:关于在Magento排序mostviewed 1.7.0.2

App/code/local/Mage/Catalog/Block/Product/Mostviewed.php 

我加入以下代码:

class Mage_Catalog_Block_Product_Mostviewed extends Mage_Catalog_Block_Product_Abstract 
{ 
    public function __construct(){ 
     parent::__construct(); 
     $storeId = Mage::app()->getStore()->getId(); 
     $collection = Mage::getResourceModel('reports/product_collection')->addViewsCount(); 
     $collection->getSelect()->joinInner(array('e2' => 'catalog_product_flat_'.$storeId), 'e2.entity_id = e.entity_id'); 
     $collection->addAttributeToSelect('*'); 

     //$collection->printlogquery(true); 
     //exit(); 
     $this->setProductCollection($collection); 
    } 

    protected function _prepareLayout() 
    { 
     parent::_prepareLayout(); 

     $toolbar = $this->getLayout()->createBlock('catalog/product_list_toolbar', microtime()) 
      ->setCollection($this->getProductCollection()); 

     $pager = $this->getLayout()->createBlock('page/html_pager', microtime()); 
     $toolbar->setChild('product_list_toolbar_pager', $pager); 

     $this->setChild('toolbar', $toolbar); 
     $this->getProductCollection()->load(); 

     return $this; 
    } 

    public function getPagerHtml() 
    { 
     return $this->getChildHtml('toolbar'); 
    } 
} 

没有添加addViewsCount()打印SQL:

SELECT `e`.*, `e2`.* 
    FROM `catalog_product_entity` AS `e` 
    INNER JOIN `catalog_product_flat_1` AS `e2` 
     ON e2.entity_id = e.entity_id 

新增addViewsCount() print sql:

SELECT COUNT(report_table_views.event_id) AS `views`, `e`.*, `e2`.* 
    FROM `report_event` AS `report_table_views` 
    INNER JOIN `catalog_product_entity` AS `e` 
     ON e.entity_id = report_table_views.object_id AND e.entity_type_id = 4 
    INNER JOIN `catalog_product_flat_1` AS `e2` 
     ON e2.entity_id = e.entity_id 
    WHERE (report_table_views.event_type_id = 1) 
    GROUP BY `e`.`entity_id` 
    HAVING (COUNT(report_table_views.event_id) > 0) 
    ORDER BY `views` DESC 

加了addViewsCount()排序不工作,在删除工作addViewsCount()

如何解决?

编辑: 现在的问题是solved.There是有毛病我的mysql DB.The代码是没有problem.Thank你@liyakat @Alex。

+0

运行第二个查询时,它不排序? – 2013-05-14 07:12:02

+0

@Alex不能按页面中的任何内容排序。 – 2013-05-14 07:35:33

+0

@JasonCheng我也遇到了同样的问题添加addViewsCount()排序不工作。你在MySQL DB中做了什么? – DRAJI 2014-03-21 06:32:38

您可以通过Mage_Reports_Model_Resource_Product_Collection模型获取查看计数。

//设置$to$from为空字符串以禁用时间范围过滤

$storeId = Mage::app()->getStore()->getId(); 
    $products = Mage::getResourceModel('reports/product_collection') 
    ->addAttributeToSelect('*') 
    ->setStoreId($storeId) 
    ->addStoreFilter($storeId)   
    ->addViewsCount() 
    ->joinField('inventory_in_stock', 'cataloginventory/stock_item', 
          'is_in_stock', 'product_id=entity_id', '{{table}}.is_in_stock=1'); 



    Mage::getSingleton('catalog/product_status') 
     ->addVisibleFilterToCollection($products); 
    Mage::getSingleton('catalog/product_visibility') 
     ->addVisibleInCatalogFilterToCollection($products);   
    $products->getSelect()->limit(15);  
    return $products; 

请让我知道如果我能帮助你更

+0

使用你的代码仍然不起作用,点击排序不会改变任何东西。 – 2013-05-14 07:28:52

+0

@JasonCheng我已经更新了我的答案,请检查ñ让我知道。 – liyakat 2013-05-14 08:47:16

+0

我更新了我的代码,但不工作! – 2013-05-14 09:09:20