Jason Cheng
Jason Cheng

Reputation: 139

About mostviewed sort by in magento 1.7.0.2

Product mostviewed file is located here:

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

I have added the following code:

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

not added addViewsCount() print 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

added 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

added addViewsCount() sort by not working,working in delete addViewsCount() .

How to fix it?

Edited: Now that the problem is solved.There are something wrong with my mysql DB.The code is no problem.Thank you @liyakat @Alex.

Upvotes: 3

Views: 720

Answers (1)

liyakat
liyakat

Reputation: 11853

You can get the view count through the Mage_Reports_Model_Resource_Product_Collection model.

// set $to and $from to an empty string to disable time range filtering

$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;

please let me know if i can help you more

Upvotes: 1

Related Questions