php 最受欢迎的产品

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php 最受欢迎的产品相关的知识,希望对你有一定的参考价值。

Magento: How to get most viewed products?

public function getMostViewedProducts()
{      
  /**
   * Number of products to display
   * You may change it to your desired value
   */
  $productCount = 5;
   
  /**
   * Get Store ID
   */
  $storeId    = Mage::app()->getStore()->getId();      
   
  /**
   * Get most viewed product collection
   */
  $products = Mage::getResourceModel('reports/product_collection')
      ->addAttributeToSelect('*')     
      ->setStoreId($storeId)
      ->addStoreFilter($storeId)
      ->addViewsCount()
      ->setPageSize($productCount);
   
  Mage::getSingleton('catalog/product_status')
          ->addVisibleFilterToCollection($products);
  Mage::getSingleton('catalog/product_visibility')
          ->addVisibleInCatalogFilterToCollection($products);
   
  return $products;
}

Get Most viewed products for current category
public function getMostViewedProducts()
{  
  // number of products to display
  $productCount = 5;
   
  // store ID
  $storeId    = Mage::app()->getStore()->getId();      
   
  // get most viewed products for current category
  $products = Mage::getResourceModel('reports/product_collection')
      ->addAttributeToSelect('*')     
      ->setStoreId($storeId)
      ->addStoreFilter($storeId)
      ->addViewsCount()
      ->addCategoryFilter(Mage::registry('current_category'))
      ->setPageSize($productCount);
   
  Mage::getSingleton('catalog/product_status')
          ->addVisibleFilterToCollection($products);
  Mage::getSingleton('catalog/product_visibility')
          ->addVisibleInCatalogFilterToCollection($products);
   
  return $products;
}

Get Most viewed products for last 30 days
public function getMostViewedProducts()
{  
  // number of products to display
  $productCount = 5;
   
  // store ID
  $storeId    = Mage::app()->getStore()->getId();
   
  // get today and last 30 days time
  $today = time();
  $last = $today - (60*60*24*30);

  $from = date("Y-m-d", $last);
  $to = date("Y-m-d", $today);
   
  // get most viewed products for last 30 days
  $products = Mage::getResourceModel('reports/product_collection')
      ->addAttributeToSelect('*')     
      ->setStoreId($storeId)
      ->addStoreFilter($storeId)
      ->addViewsCount()
      ->addViewsCount($from, $to)
      ->setPageSize($productCount);
   
  Mage::getSingleton('catalog/product_status')
          ->addVisibleFilterToCollection($products);
  Mage::getSingleton('catalog/product_visibility')
          ->addVisibleInCatalogFilterToCollection($products);
   
  return $products;
}

以上是关于php 最受欢迎的产品的主要内容,如果未能解决你的问题,请参考以下文章

如何找到最受欢迎和最不受欢迎的产品?

如何在 Magento 的产品列表页面上按最受欢迎(最畅销)的产品排序?

介绍NoSQL最受欢迎的产品

2015 PHP框架调查结果出炉,Laravel最受欢迎!

最受IT公司欢迎的50款开源软件

2019年最受欢迎iTOP-4418开发板_新产品研发必备利器