Jump to content

Choose 4 random manufacturers and display them.


Recommended Posts

Hello!

I've a major problem that I haven't been able to find a solution for. I've tried to search Google for a few days now, but sadly that was to no avail. As the titles states, I want to choose 4 random manufacturers and display them. I'm using a custom template, so I provide you with the code for the manufacturers block.

   {if $manufacturers}

   {foreach from=$manufacturers item=manufacturer name=manufacturer_list}
     {if $smarty.foreach.manufacturer_list.iteration <= $text_list_nb}
getmanufacturerLink($manufacturer.id_manufacturer, $manufacturer.link_rewrite)}" >


     {/if}
      {/foreach}

{else}

{l s='No manufacturer' mod='blockmanufacturer'}
{/if}




So what this code does it that it search for all the manufacturers and display an image of X manufacturer with a clickable link. The problems that occur are that this code doesn’t limit the amount of manufacturers shown and it also doesn't choose them randomly.

I would like some help with this to have a code that searches for manufacturers, chooses 4 randomly and displays them.

Pretty please? :)

Regards, Maxii

Link to comment
Share on other sites

  • 5 months later...
  • 3 weeks later...

Hi,

 

I think I got it!

 

In blockmanufacturer.php (root/modules/module name) I´ve changed the call method from:

'manufacturers' => Manufacturer::getManufacturers()

to

'manufacturers' => Manufacturer::getManufacturersRand()

 

so, in classes/Manufacturer.php i´ve copied the getManufacturers() method and pasted below with the name getManufacturersRand()

The only thing you need to change is this line with:

$sql.= ' ORDER BY m.`name` ASC'.($p ? ' LIMIT '.(((int)($p) - 1) * (int)($n)).','.(int)($n) : '');

to

$sql.= ' ORDER BY RAND()'.($p ? ' LIMIT '.(((int)($p) - 1) * (int)($n)).','.(int)($n) : '');

 

hope it helps, it worked for me in Prestashop version 1.4.3

bye

  • Like 1
Link to comment
Share on other sites

  • 1 month later...

This looks promising, thank you very much, but I have tried it and I get the error:

Fatal error: Call to undefined method Manufacturer::getManufacturersRand() in /home/koolkidz/public_html/modules/blockmanufacturer/blockmanufacturer.php on line 59

 

Do you know what might be causing this? Thanks!

Link to comment
Share on other sites

Actually I think I have it working, I misread your instructions - line 181 in classes/Manufacturer.php looks like this:

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

needs to be changed to this

static public function getManufacturersRand($getNbProducts = false, $id_lang = 0, $active = true, $p = false, $n = false, $all_group = false)

 

danielbr86, thanks so much for your help. Really appreciate it!

Link to comment
Share on other sites

Ahh... right, just realised, after altering the code on those pages, when you click Manufacuters, I get the following error:

Warning: call_user_func(Manufacturer::getManufacturers) [function.call-user-func]: First argument is expected to be a valid callback in /home/koolkidz/public_html/controllers/ManufacturerController.php on line 64
Warning: call_user_func(Manufacturer::getManufacturers) [function.call-user-func]: First argument is expected to be a valid callback in /home/koolkidz/public_html/controllers/ManufacturerController.php on line 68
Warning: Invalid argument supplied for foreach() in /home/koolkidz/public_html/controllers/ManufacturerController.php on line 70

 

To get round this, you need to revert the altered code back to its original state in classes/Manufacturer.php, so it looks like this:

 static public function getManufacturers($getNbProducts = false, $id_lang = 0, $active = true, $p = false, $n = false, $all_group = false)
{
 if (!$id_lang)
  $id_lang = (int)Configuration::get('PS_LANG_DEFAULT');
 $sql = 'SELECT m.*, ml.`description`';
 $sql.= ' FROM `'._DB_PREFIX_.'manufacturer` m
 LEFT JOIN `'._DB_PREFIX_.'manufacturer_lang` ml ON (m.`id_manufacturer` = ml.`id_manufacturer` AND ml.`id_lang` = '.(int)($id_lang).')
 '.($active ? ' WHERE m.`active` = 1' : '');
 $sql.= ' ORDER BY m.`name` ASC'.($p ? ' LIMIT '.(((int)($p) - 1) * (int)($n)).','.(int)($n) : '');
 $manufacturers = Db::getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS($sql);
 if ($manufacturers === false)
  return false;
 if ($getNbProducts)
 {
  $sqlGroups = '';
  if (!$all_group)
  {
   $groups = FrontController::getCurrentCustomerGroups();
   $sqlGroups = (count($groups) ? 'IN ('.implode(',', $groups).')' : '= 1');
  }
  foreach ($manufacturers as $key => $manufacturer)
  {
   $result = Db::getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS('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` = '.(int)($manufacturer['id_manufacturer']).
   ($active ? ' AND p.`active` = 1 ' : '').
   ($all_group ? '' : ' 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` '.$sqlGroups.')'));
   $manufacturers[$key]['nb_products'] = sizeof($result);
  }
 }
 for ($i = 0; $i < sizeof($manufacturers); $i++)
  if ((int)(Configuration::get('PS_REWRITING_SETTINGS')))
   $manufacturers[$i]['link_rewrite'] = Tools::link_rewrite($manufacturers[$i]['name'], false);
  else
   $manufacturers[$i]['link_rewrite'] = 0;
 return $manufacturers;
}

 

(no mention of the Rand call).

 

And then, copy all of that code, paste it directly underneath and make the alterations as mentioned by danielbr86:

 

static public function getManufacturersRand($getNbProducts = false, $id_lang = 0, $active = true, $p = false, $n = false, $all_group = false)
{
 if (!$id_lang)
  $id_lang = (int)Configuration::get('PS_LANG_DEFAULT');
 $sql = 'SELECT m.*, ml.`description`';
 $sql.= ' FROM `'._DB_PREFIX_.'manufacturer` m
 LEFT JOIN `'._DB_PREFIX_.'manufacturer_lang` ml ON (m.`id_manufacturer` = ml.`id_manufacturer` AND ml.`id_lang` = '.(int)($id_lang).')
 '.($active ? ' WHERE m.`active` = 1' : '');
 $sql.= ' ORDER BY RAND()'.($p ? ' LIMIT '.(((int)($p) - 1) * (int)($n)).','.(int)($n) : '');
 $manufacturers = Db::getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS($sql);
 if ($manufacturers === false)
  return false;
 if ($getNbProducts)
 {
  $sqlGroups = '';
  if (!$all_group)
  {
   $groups = FrontController::getCurrentCustomerGroups();
   $sqlGroups = (count($groups) ? 'IN ('.implode(',', $groups).')' : '= 1');
  }
  foreach ($manufacturers as $key => $manufacturer)
  {
   $result = Db::getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS('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` = '.(int)($manufacturer['id_manufacturer']).
   ($active ? ' AND p.`active` = 1 ' : '').
   ($all_group ? '' : ' 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` '.$sqlGroups.')'));
   $manufacturers[$key]['nb_products'] = sizeof($result);
  }
 }
 for ($i = 0; $i < sizeof($manufacturers); $i++)
  if ((int)(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

  • 1 year later...
  • 1 year later...

Ahh... right, just realised, after altering the code on those pages, when you click Manufacuters, I get the following error:

Warning: call_user_func(Manufacturer::getManufacturers) [function.call-user-func]: First argument is expected to be a valid callback in /home/koolkidz/public_html/controllers/ManufacturerController.php on line 64
Warning: call_user_func(Manufacturer::getManufacturers) [function.call-user-func]: First argument is expected to be a valid callback in /home/koolkidz/public_html/controllers/ManufacturerController.php on line 68
Warning: Invalid argument supplied for foreach() in /home/koolkidz/public_html/controllers/ManufacturerController.php on line 70
To get round this, you need to revert the altered code back to its original state in classes/Manufacturer.php, so it looks like this:

  static public function getManufacturers($getNbProducts = false, $id_lang = 0, $active = true, $p = false, $n = false, $all_group = false)
{
  if (!$id_lang)
   $id_lang = (int)Configuration::get('PS_LANG_DEFAULT');
  $sql = 'SELECT m.*, ml.`description`';
  $sql.= ' FROM `'._DB_PREFIX_.'manufacturer` m
  LEFT JOIN `'._DB_PREFIX_.'manufacturer_lang` ml ON (m.`id_manufacturer` = ml.`id_manufacturer` AND ml.`id_lang` = '.(int)($id_lang).')
  '.($active ? ' WHERE m.`active` = 1' : '');
  $sql.= ' ORDER BY m.`name` ASC'.($p ? ' LIMIT '.(((int)($p) - 1) * (int)($n)).','.(int)($n) : '');
  $manufacturers = Db::getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS($sql);
  if ($manufacturers === false)
   return false;
  if ($getNbProducts)
  {
   $sqlGroups = '';
   if (!$all_group)
   {
    $groups = FrontController::getCurrentCustomerGroups();
    $sqlGroups = (count($groups) ? 'IN ('.implode(',', $groups).')' : '= 1');
   }
   foreach ($manufacturers as $key => $manufacturer)
   {
    $result = Db::getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS('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` = '.(int)($manufacturer['id_manufacturer']).
    ($active ? ' AND p.`active` = 1 ' : '').
    ($all_group ? '' : ' 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` '.$sqlGroups.')'));
    $manufacturers[$key]['nb_products'] = sizeof($result);
   }
  }
  for ($i = 0; $i < sizeof($manufacturers); $i++)
   if ((int)(Configuration::get('PS_REWRITING_SETTINGS')))
    $manufacturers[$i]['link_rewrite'] = Tools::link_rewrite($manufacturers[$i]['name'], false);
   else
    $manufacturers[$i]['link_rewrite'] = 0;
  return $manufacturers;
}
(no mention of the Rand call).

 

And then, copy all of that code, paste it directly underneath and make the alterations as mentioned by danielbr86:

 

static public function getManufacturersRand($getNbProducts = false, $id_lang = 0, $active = true, $p = false, $n = false, $all_group = false)
{
  if (!$id_lang)
   $id_lang = (int)Configuration::get('PS_LANG_DEFAULT');
  $sql = 'SELECT m.*, ml.`description`';
  $sql.= ' FROM `'._DB_PREFIX_.'manufacturer` m
  LEFT JOIN `'._DB_PREFIX_.'manufacturer_lang` ml ON (m.`id_manufacturer` = ml.`id_manufacturer` AND ml.`id_lang` = '.(int)($id_lang).')
  '.($active ? ' WHERE m.`active` = 1' : '');
  $sql.= ' ORDER BY RAND()'.($p ? ' LIMIT '.(((int)($p) - 1) * (int)($n)).','.(int)($n) : '');
  $manufacturers = Db::getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS($sql);
  if ($manufacturers === false)
   return false;
  if ($getNbProducts)
  {
   $sqlGroups = '';
   if (!$all_group)
   {
    $groups = FrontController::getCurrentCustomerGroups();
    $sqlGroups = (count($groups) ? 'IN ('.implode(',', $groups).')' : '= 1');
   }
   foreach ($manufacturers as $key => $manufacturer)
   {
    $result = Db::getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS('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` = '.(int)($manufacturer['id_manufacturer']).
    ($active ? ' AND p.`active` = 1 ' : '').
    ($all_group ? '' : ' 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` '.$sqlGroups.')'));
    $manufacturers[$key]['nb_products'] = sizeof($result);
   }
  }
  for ($i = 0; $i < sizeof($manufacturers); $i++)
   if ((int)(Configuration::get('PS_REWRITING_SETTINGS')))
    $manufacturers[$i]['link_rewrite'] = Tools::link_rewrite($manufacturers[$i]['name'], false);
   else
    $manufacturers[$i]['link_rewrite'] = 0;
  return $manufacturers;
}

 

 

Does anyone have a up-to-date version of these mods?  Ive tried everything here and it just results in my website loading blank.. really desperate to figure out how to randomize manufactures logos.  At least in the side left box list.  thanks!!

Link to comment
Share on other sites

Ok, I figured out how to change it on the current version.

 

In the following code, found in Manufacturer.php in classes/, go to around line 174 where you will see;

 

 
'.Shop::addSqlAssociation('manufacturer', 'm');
if ($active)
$sql .= '
WHERE m.`active` = 1';
$sql .= '
GROUP BY m.id_manufacturer
ORDER BY m.`name` ASC'.
($p ? ' LIMIT '.(((int)$p - 1) * (int)$n).','.(int)$n : '');

And remove the following piece of codes;

 

$sql .= '
GROUP BY m.id_manufacturer
ORDER BY m.`name` ASC'.
 
and replace with;
 
 
  $sql.= ' ORDER BY RAND()'.

Now the images on the sidebar for Manufacturers will always load at random, displaying different ones each time the page loads.

 

Hope this is helpful to anyone needing to do this :)

Link to comment
Share on other sites

  • 7 months later...

Ok, I figured out how to change it on the current version.

 

In the following code, found in Manufacturer.php in classes/, go to around line 174 where you will see;

 

 
'.Shop::addSqlAssociation('manufacturer', 'm');
if ($active)
$sql .= '
WHERE m.`active` = 1';
$sql .= '
GROUP BY m.id_manufacturer
ORDER BY m.`name` ASC'.
($p ? ' LIMIT '.(((int)$p - 1) * (int)$n).','.(int)$n : '');

And remove the following piece of codes;

 

$sql .= '
GROUP BY m.id_manufacturer
ORDER BY m.`name` ASC'.
 
and replace with;
 
 
  $sql.= ' ORDER BY RAND()'.

Now the images on the sidebar for Manufacturers will always load at random, displaying different ones each time the page loads.

 

Hope this is helpful to anyone needing to do this :)

 

Hello,

@omgzhobbs

In presta 1.6.0.9 it's different, how to randomize manufactures list ?can you post the code ?

THX.

 

    '.Shop::addSqlAssociation('manufacturer', 'm').'
        INNER JOIN `'._DB_PREFIX_.'manufacturer_lang` ml ON (m.`id_manufacturer` = ml.`id_manufacturer` AND ml.`id_lang` = '.(int)$id_lang.')
        '.($active ? 'WHERE m.`active` = 1' : '')
        .($group_by ? ' GROUP BY m.`id_manufacturer`' : '' ).'
        ORDER BY m.`name` ASC
        '.($p ? ' LIMIT '.(((int)$p - 1) * (int)$n).','.(int)$n : ''));
Link to comment
Share on other sites

  • 3 months later...

Hi friends,

this code paste to classes/Manufacturers.php

/**
	  * Return manufacturers
	  *
	  * @param boolean $get_nb_products [optional] return products numbers for each
	  * @param int $id_lang
	  * @param bool $active
	  * @param int $p
	  * @param int $n
	  * @param bool $all_group
	  * @return array Manufacturers
	  */
	public static function getManufacturersRAND($get_nb_products = false, $id_lang = 0, $active = true, $p = false, $n = false, $all_group = false, $group_by = false)
	{
		if (!$id_lang)
			$id_lang = (int)Configuration::get('PS_LANG_DEFAULT');
		if (!Group::isFeatureActive())
			$all_group = true;

		$manufacturers = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('
		SELECT m.*, ml.`description`, ml.`short_description`
		FROM `'._DB_PREFIX_.'manufacturer` m
		'.Shop::addSqlAssociation('manufacturer', 'm').'
		INNER JOIN `'._DB_PREFIX_.'manufacturer_lang` ml ON (m.`id_manufacturer` = ml.`id_manufacturer` AND ml.`id_lang` = '.(int)$id_lang.')
		'.($active ? 'WHERE m.`active` = 1' : '')
		.($group_by ? ' GROUP BY m.`id_manufacturer`' : '' ).'
		ORDER BY RAND()
		'.($p ? ' LIMIT '.(((int)$p - 1) * (int)$n).','.(int)$n : ''));
		if ($manufacturers === false)
			return false;

		if ($get_nb_products)
		{
			$sql_groups = '';
			if (!$all_group)
			{
				$groups = FrontController::getCurrentCustomerGroups();
				$sql_groups = (count($groups) ? 'IN ('.implode(',', $groups).')' : '= 1');
			}

			foreach ($manufacturers as $key => $manufacturer)
			{
				$manufacturers[$key]['nb_products'] = Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue('
				SELECT COUNT(DISTINCT p.`id_product`)
				FROM `'._DB_PREFIX_.'product` p
				'.Shop::addSqlAssociation('product', 'p').'
				WHERE p.`id_manufacturer` = '.(int)$manufacturer['id_manufacturer'].'
				AND product_shop.`visibility` NOT IN ("none")
				'.($active ? ' AND product_shop.`active` = 1 ' : '').'
				'.($all_group ? '' : ' 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` '.$sql_groups.'
				)'));
			}
		}

		$total_manufacturers = count($manufacturers);
		$rewrite_settings = (int)Configuration::get('PS_REWRITING_SETTINGS');
		for ($i = 0; $i < $total_manufacturers; $i++)
			$manufacturers[$i]['link_rewrite'] = ($rewrite_settings ? Tools::link_rewrite($manufacturers[$i]['name']) : 0);
		return $manufacturers;
	} 

And change your module in  $manufacturers = Manufacturer::getManufacturers(false,0,true);  to

$manufacturers = Manufacturer::getManufacturersRAND(false,0,true);

 

BO : Advanded Conf>Perfomance 

select Force Compile  and UseCache=off

Link to comment
Share on other sites

  • 1 year later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...