Jump to content

Rendre possible la sélection de produit aléatoirement


Recommended Posts

Bonjour à tous.

 

Voilà j'ai besoin de pouvoir sortir un flux avec x produits pris au hasard.

Le problème est que sauf erreur de ma part il n'est pas possible avoir un extraction de produit en aléatoire.

 

Puisque

getProductsOrder accepte comme valeur:

order by

  • name
  • price
  • date_add
  • date_upd
  • position
  • manufacturer_name
  • quantity

et pas de rand

 

J'ai donc modifier les function getProductsOrder et function getProducts

 

Si un developpeur peut me donner son avis.

 

Classes=>tools

	public static function getProductsOrder($type, $value = null, $prefix = false)
{
	switch ($type)
	{
		case 'by' :
		if ($value !="rand")
		{
			$list = array(0 => 'name', 1 => 'price', 2 => 'date_add', 3 => 'date_upd', 4 => 'position', 5 => 'manufacturer_name', 6 => 'quantity');
			$value = (is_null($value) || $value === false || $value === '') ? (int)Configuration::get('PS_PRODUCTS_ORDER_BY') : $value;
			$value = (isset($list[$value])) ? $list[$value] : ((in_array($value, $list)) ? $value : 'position');
			$order_by_prefix = '';
			if ($prefix)
			{
				if ($value == 'id_product' || $value == 'date_add' || $value == 'date_upd' || $value == 'price')
					$order_by_prefix = 'p.';
				elseif ($value == 'name')
					$order_by_prefix = 'pl.';
				elseif ($value == 'manufacturer_name' && $prefix)
				{
					$order_by_prefix = 'm.';
					$value = 'name';
				}
				elseif ($value == 'position' || empty($value))
					$order_by_prefix = 'cp.';
			}
			return $order_by_prefix.$value;
		}else {
			return "rand";
		}

 

 

title=classe>product function getProducts

$sql = 'SELECT p.*, product_shop.*, pl.* , m.`name` AS manufacturer_name, s.`name` AS supplier_name
			FROM `'._DB_PREFIX_.'product` p
			'.Shop::addSqlAssociation('product', 'p').'
			LEFT JOIN `'._DB_PREFIX_.'product_lang` pl ON (p.`id_product` = pl.`id_product` '.Shop::addSqlRestrictionOnLang('pl').')
			LEFT JOIN `'._DB_PREFIX_.'manufacturer` m ON (m.`id_manufacturer` = p.`id_manufacturer`)
			LEFT JOIN `'._DB_PREFIX_.'supplier` s ON (s.`id_supplier` = p.`id_supplier`)'.
			($id_category ? 'LEFT JOIN `'._DB_PREFIX_.'category_product` c ON (c.`id_product` = p.`id_product`)' : '').'
			WHERE pl.`id_lang` = '.(int)$id_lang.
				($id_category ? ' AND c.`id_category` = '.(int)$id_category : '').
				($front ? ' AND product_shop.`visibility` IN ("both", "catalog")' : '').
				($only_active ? ' AND product_shop.`active` = 1' : '').'
			ORDER BY '.(isset($order_by_prefix) ? pSQL($order_by_prefix).'.' : '').($order_by!="rand"?'`'.pSQL($order_by).'` '.pSQL($order_way):"rand()").
			($limit > 0 ? ' LIMIT '.(int)$start.','.(int)$limit : '');

 

Ceci fonctionne chez noi et serais intégré par override et permettrais notamment d'appeler le flux en aléatoire sous la forme

Edited by jd440 (see edit history)
Link to comment
Share on other sites

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...