Search code examples
phpsmartyprestashopprestashop-1.5prestashop-1.6

Prestashop: Show the number of products on my wishlist header menu item


How can I show the number of products from my wishlist header menu? This is how I want to look like this:

I tried something like this:

<a href="/index.php?fc=module&module=blockwishlist&controller=mywishlist" 
title="{l s='My wishlists' mod='blockwishlist'}" 
rel="nofollow">{l s='Wishlist' mod='blockwishlist'} ({'wishlist'|count})
</a>

But it seems to count my wishlists and not my products from the wishlist.


Solution

  • Here's a quick solution for this problem. This is not 100% efficient but it will do the trick.

    1. Go to \modules\blockuserinfo and edit blockuserinfo.php
    2. Update hookTop() function to this (This will count the products from the active wishlist of the logged user and assign the value to $count_products variable) :

      public function hookTop($params) { if (!$this->active) return;

      $current_user = (int)$this->context->cookie->id_customer;
      
      $id_wishlist = Db::getInstance()->getValue("SELECT id_wishlist FROM `"._DB_PREFIX_."wishlist` WHERE id_customer = '$current_user'");
      $count_products = Db::getInstance()->getValue("SELECT COUNT(id_wishlist_product) FROM `"._DB_PREFIX_."wishlist_product` WHERE id_wishlist = '$id_wishlist'");
      
      
      $this->smarty->assign(array(
          'current_user' => $count_products,
          'cart' => $this->context->cart,
          'cart_qties' => $this->context->cart->nbProducts(),
          'logged' => $this->context->customer->isLogged(),
          'customerName' => ($this->context->customer->logged ? $this->context->customer->firstname.' '.$this->context->customer->lastname : false),
          'firstName' => ($this->context->customer->logged ? $this->context->customer->firstname : false),
          'lastName' => ($this->context->customer->logged ? $this->context->customer->lastname : false),
          'order_process' => Configuration::get('PS_ORDER_PROCESS_TYPE') ? 'order-opc' : 'order'
      ));
      return $this->display(__FILE__, 'blockuserinfo.tpl');
      

      }

    3. Now we have to display it on the menu. Edit blockuserinfo.tpl that it's in the same directory and add this:

      {l s='Wishlist' mod='blockwishlist'} ({$count_products})

    4. Save all files. It should displayed in the frontend the number of products

    *Note: If the user has multiple wishlists, the trick will work just for a wishlist