prestashop: ps_viewedproducts remove cache

 

i'm using the ps_viewedproducts module as a "recommended products" module, so I want to remove theĀ  "viewed" limitation.

 

go to ps_viewedproduct.php and edit comment the following.

 

//        if (!isset($this->context->cookie->viewed) || empty($this->context->cookie->viewed)) {
//          return;
//       }

this means, it will not stop the code if there are no items cached (viewed). 

Then you can change your code to display the products you want. 

to do this edit the function getViewedProductIds() on the same file and edit 


//      $viewedProductsIds = array_reverse(explode(',', $this->context->cookie->viewed));
         
         $viewedProductsIds = $this->get_random_products();


where get_random_products() is a function I created, you can use as a hint below:



private function get_random_products()
{
   $existingProductsQuery = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('
          SELECT p.id_product
          FROM ' . _DB_PREFIX_ . 'product p
          WHERE p.active = 1
          group by id_manufacturer
   order by rand()
          limit 5
          '
   );
   
   return array_map(function ($entry) {
      return $entry['id_product'];
   }, $existingProductsQuery);
}

Leave a Reply

Your email address will not be published. Required fields are marked *