0

I am trying to get most viewed products for last month and last day by using this code but apparently both are returning me same no. of views

Last Day:

$from = date('Y-m-d', strtotime(' -1 day'));
            $to=now();

Last Month:

 $from  = new DateTime("first day of last month");
            $to = new DateTime("last day of last month");

             from ->format('Y-m-d'); // 2012-02-01
            $to->format('Y-m-d'); // 2012-02-29

CODE:

 $productCount = 5;
          $storeId = Mage::app()->getStore()->getId();
           $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;

Along with this I also need to get product view count for all the time.

EDIT: With certain fixes in my phtml file above code works for last day and all time. But the last month is giving me error on this line

->addViewsCount($from, $to)
0

After trying this code worked:

CODE:

public function getMostViewedProducts($requested) {
         Mage::log($requested,null,'size.log',true);
        $productCount = 5;
        $storeId = Mage::app()->getStore()->getId();
        if ($requested == "last_day") {
            $from = date('Y-m-d', strtotime(' -1 day'));
            $to = now();
            $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;
        } else if ($requested == "last_month") {
            $month_ini = new DateTime("first day of last month");
            $month_end = new DateTime("last day of last month");

            $from=$month_ini->format('Y-m-d'); // 2012-02-01
            $to=$month_end->format('Y-m-d'); // 2012-02-29
            $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;
        } else if($requested==""){
            $products = Mage::getResourceModel('reports/product_collection')
                    ->addAttributeToSelect('*')
                    ->setStoreId($storeId)
                    ->addStoreFilter($storeId)
                    ->addViewsCount()
                    //->addViewsCount()
                    ->setPageSize($productCount);

            Mage::getSingleton('catalog/product_status')
                    ->addVisibleFilterToCollection($products);
            Mage::getSingleton('catalog/product_visibility')
                    ->addVisibleInCatalogFilterToCollection($products);
            foreach($products as $a){
              //  Mage::log($a->getData(),null,'size.log',true);//exit;
            }
            return $products;
        }
    }
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.