Jump to content

[SOLVED] "Manufacturers" block - how to filter by "Displayed" flag? (also filter by "has product")


Recommended Posts

Try changing line 31 of modules/blockmanufacturer/blockmanufacturer.php from:

'manufacturers' => Manufacturer::getManufacturers(),



to:

'manufacturers' => Manufacturer::getManufacturers(false, 0, true),



This issue might be worth posting on the bug tracker.

Link to comment
Share on other sites

Try changing line 185 of classes/Manufacturer.php (in PrestaShop v1.3.1) from:

$manufacturers[$key]['nb_products'] = sizeof($result);



to:

if (sizeof($result) > 0)
  $manufacturers[$key]['nb_products'] = sizeof($result);
else
  unset($manufacturers[$key]);



This should delete the manufacturer from the returned results when there are no products.

Link to comment
Share on other sites

Hi Rocky,

That worked perfectly on the block, however when I click on the block title ("Manufacturer") I think that may have caused some unusual behavior:

http://tiny.cc/mjigr

As you can see, there should only be 3 manufacturers returned, while it states "There are 13 brands.", however there are only 6 results! Three different things!

Any idea how I can tweak the mod above to fix this?

Cheers

Link to comment
Share on other sites

Here's the entire modified getManufacturers function:

static public function getManufacturers($getNbProducts = false, $id_lang = 0, $active = false, $p = false, $n = false)
{
   global $cookie;

   if (!$id_lang)
       $id_lang = Configuration::get('PS_LANG_DEFAULT');
   $sql = 'SELECT m.*, ml.`description`';
   $sql.= ' FROM `'._DB_PREFIX_.'manufacturer` as m
   LEFT JOIN `'._DB_PREFIX_.'manufacturer_lang` ml ON (m.`id_manufacturer` = ml.`id_manufacturer` AND ml.`id_lang` = '.intval($id_lang).')';
   $sql.= ' ORDER BY m.`name` ASC'.($p ? ' LIMIT '.((intval($p) - 1) * intval($n)).','.intval($n) : '');
   $manufacturers = Db::getInstance()->ExecuteS($sql);
   if ($manufacturers === false)
       return false;
//    if ($getNbProducts)
       foreach ($manufacturers as $key => $manufacturer)
       {
           $sql = '
               SELECT p.`id_product`
               FROM `'._DB_PREFIX_.'product` p
               LEFT JOIN `'._DB_PREFIX_.'manufacturer` as m ON (m.`id_manufacturer`= p.`id_manufacturer`)
               WHERE m.`id_manufacturer` = '.intval($manufacturer['id_manufacturer']).'
               AND p.`id_product` IN (
                   SELECT cp.`id_product`
                   FROM `'._DB_PREFIX_.'category_group` cg
                   LEFT JOIN `'._DB_PREFIX_.'category_product` cp ON (cp.`id_category` = cg.`id_category`)
                   WHERE cg.`id_group` '.(!$cookie->id_customer ?  '= 1' : 'IN (SELECT id_group FROM '._DB_PREFIX_.'customer_group WHERE id_customer = '.intval($cookie->id_customer).')').'
               )';
           $result = Db::getInstance()->ExecuteS($sql);
           if (sizeof($result) > 0)
              $manufacturers[$key]['nb_products'] = sizeof($result);
           else
              unset($manufacturers[$key]); 
       }
   for ($i = 0; $i < sizeof($manufacturers); $i++)
       if (intval(Configuration::get('PS_REWRITING_SETTINGS')))
           $manufacturers[$i]['link_rewrite'] = Tools::link_rewrite($manufacturers[$i]['name'], false);
       else
           $manufacturers[$i]['link_rewrite'] = 0;
   return $manufacturers;
}

Link to comment
Share on other sites

  • 2 weeks later...

This issue was solved via PM. Here's the solution I came up with. Change line 157 of classes/Manufacturer.php (in PrestaShop v1.3.1) from:

static public function getManufacturers($getNbProducts = false, $id_lang = 0, $active = false, $p = false, $n = false)



to:

static public function getManufacturers($getNbProducts = false, $id_lang = 0, $active = false, $p = false, $n = false, $nonZero = false)



and add the following after line 165:

$sql.= $nonZero ? ' WHERE (SELECT COUNT(*) FROM `'._DB_PREFIX_.'product` WHERE `id_manufacturer` = m.`id_manufacturer`) > 0' : '';



then change line 31 of modules/blockmanufacturer/blockmanufacturer.php from:

'manufacturers' => Manufacturer::getManufacturers(),



to:

'manufacturers' => Manufacturer::getManufacturers(false, 0, false, false, false, true),



and line 50 of supplier.php from:

$data = call_user_func(array($className, 'get'.$className.'s'), false, intval($cookie->id_lang), true);



to:

$data = call_user_func(array($className, 'get'.$className.'s'), false, intval($cookie->id_lang), true, false, false, $className == 'Manufacturer');



and line 54 from:

$data = call_user_func(array($className, 'get'.$className.'s'), true, intval($cookie->id_lang), true, $p, $n);



to:

$data = call_user_func(array($className, 'get'.$className.'s'), true, intval($cookie->id_lang), true, $p, $n, $className == 'Manufacturer');



It is possible to do a similar thing with the suppliers by making similar modifications to classes/Suppliers.php and modules/blocksupplier/blocksupplier.php and then change the code above for supplier.php from:

$className == 'Manufacturer'



to:

true

Link to comment
Share on other sites

  • 1 month later...

Hi,

I have followed the solution above as recommended on my other topic and it works perfectly, although I do have one question.....

It would appear that Manufacturers are only displayed when items are present in the catalog. Is it possible to modify the code so that I can choose to still display a brand despite not having any products in stock for that particular manufacturer?

Thanks

Luke.

Link to comment
Share on other sites

  • 7 months later...

It works ok when a product is deleted completely from the database.
But, it doesn't disable displaying the manufacturer if a product just disabled from the BO instead of deleting it. I use 1.3.1 with a modified version of manufacturers block.

Anyhow i also need to thank Rocky for this solution.

Link to comment
Share on other sites

×
×
  • Create New...