Jump to content

Sort By Best sellers ?


Recommended Posts

Hi anarchy99,

This is tricky, since the ProductSales::getBestSales() function returns only products that have sold at least once and none of the unsold products. I had to write my own function to include unsold products and limit the products to the current category.

Add the following function to classes/ProductSale.php:

/*
** Get products sorted by sales including unsold products
**    
** @param integer $id_lang Language id
** @param integer $pageNumber Start from (optional)
** @param integer $nbProducts Number of products to return (optional)
** @return array from Product::getProductProperties
*/
static public function getProductsBySales($id_lang, $id_category, $pageNumber = 0, $nbProducts = 10, $orderBy=NULL, $orderWay=NULL)
{
   global $link, $cookie;

   if ($pageNumber < 0) $pageNumber = 0;
   if ($nbProducts < 1) $nbProducts = 10;
   if (empty($orderBy)) $orderBy = 'sales';
   if (empty($orderWay)) $orderWay = 'DESC';

   $result = Db::getInstance()->ExecuteS('
   SELECT p.*, pl.`description`, pl.`description_short`, pl.`link_rewrite`, pl.`meta_description`, pl.`meta_keywords`, pl.`meta_title`, pl.`name`, i.`id_image`, il.`legend`, ps.`quantity` AS sales, t.`rate`, pl.`meta_keywords`, pl.`meta_title`, pl.`meta_description`
   FROM `'._DB_PREFIX_.'category_product` cp
   LEFT JOIN `'._DB_PREFIX_.'product` p ON (p.`id_product` = cp.`id_product`)
   LEFT JOIN `'._DB_PREFIX_.'product_sale` ps ON ps.`id_product` = p.`id_product`
   LEFT JOIN `'._DB_PREFIX_.'product_lang` pl ON (p.`id_product` = pl.`id_product` AND pl.`id_lang` = '.intval($id_lang).')
   LEFT JOIN `'._DB_PREFIX_.'image` i ON (i.`id_product` = p.`id_product` AND i.`cover` = 1)
   LEFT JOIN `'._DB_PREFIX_.'image_lang` il ON (i.`id_image` = il.`id_image` AND il.`id_lang` = '.intval($id_lang).')
   LEFT JOIN `'._DB_PREFIX_.'tax` t ON (t.`id_tax` = p.`id_tax`)
   WHERE cp.`id_category` = '.intval($id_category).($active ? ' AND p.`active` = 1' : '').'
   GROUP BY p.`id_product`
   ORDER BY '.(isset($orderByPrefix) ? $orderByPrefix.'.' : '').'`'.pSQL($orderBy).'` '.pSQL($orderWay).'
   LIMIT '.intval($pageNumber * $nbProducts).', '.intval($nbProducts));
   if($orderBy == 'price')
   {    
       Tools::orderbyPrice($result,$orderWay);
   }
   if (!$result)
       return false;
   return Product::getProductsProperties($id_lang, $result);
}


Then change line 49 of category.php from:

$cat_products = $category->getProducts(intval($cookie->id_lang), intval($p), intval($n), $orderBy, $orderWay);        



to:

if ($orderBy == 'sales')
   $cat_products = ProductSale::getProductsBySales(intval($cookie->id_lang), $category->id_category, intval($p) - 1, intval($n), $orderBy, $orderWay);
else
   $cat_products = $category->getProducts(intval($cookie->id_lang), intval($p), intval($n), $orderBy, $orderWay);



Then add the following option to product-sort.tpl in your theme directory, somewhere below the select, depending on where in the dropdown box you want it to appear:

{l s='best sellers'}



You also need to change the line 4 of product-sort.php from:

$orderByValues = array(0 => 'name', 1 => 'price', 2 => 'date_add', 3 => 'date_upd', 4 => 'position', 5 => 'manufacturer_name', 6 => 'quantity'); 



to:

$orderByValues = array(0 => 'name', 1 => 'price', 2 => 'date_add', 3 => 'date_upd', 4 => 'position', 5 => 'manufacturer_name', 6 => 'quantity', 7 => 'sales'); 

Link to comment
Share on other sites

Hello Rocky,

I found some problem that when I choose to sort by best sales in categories 1, they will include best sales in another categories too.

So I will have best sales from another categories showing up on categories 1.

I try to solve it by my self but an error occur.

What I have to change for fixing these problem?

Thank you

Link to comment
Share on other sites

  • 1 month later...

Hi,

Is there a way to simply show just the best selling products above 2 or more units?

I ask you this because my best sellers page is full of products, and I would like, at least, to be able to order them by sales quantity, being the most selled the first one to show.

I've tried to change the code of my files to the one above, but tit didn't work for me, gave me error in the shop.

Thank's

Link to comment
Share on other sites

  • 7 months later...

Thanks for posting that code Rocky.

I needed to add $active to :

static public function getProductsBySales($id_lang, $id_category, $pageNumber = 0, $nbProducts = 10, $orderBy=NULL, $orderWay=NULL, $active=true)

and add something like:

{l s='meilleures ventes'}

to product-list.tpl

Regards,

Link to comment
Share on other sites

  • 9 months later...
  • 3 months later...
  • 8 months later...
  • 8 months later...
Hi anarchy99,

 

This is tricky, since the ProductSales::getBestSales() function returns only products that have sold at least once and none of the unsold products. I had to write my own function to include unsold products and limit the products to the current category.

 

Add the following function to classes/ProductSale.php:

/*
** Get products sorted by sales including unsold products
**
** @param integer $id_lang Language id
** @param integer $pageNumber Start from (optional)
** @param integer $nbProducts Number of products to return (optional)
** @return array from Product::getProductProperties
*/
static public function getProductsBySales($id_lang, $id_category, $pageNumber = 0, $nbProducts = 10, $orderBy=NULL, $orderWay=NULL)
{
global $link, $cookie;

if ($pageNumber < 0) $pageNumber = 0;
if ($nbProducts < 1) $nbProducts = 10;
if (empty($orderBy)) $orderBy = 'sales';
if (empty($orderWay)) $orderWay = 'DESC';

$result = Db::getInstance()->ExecuteS('
SELECT p.*, pl.`description`, pl.`description_short`, pl.`link_rewrite`, pl.`meta_description`, pl.`meta_keywords`, pl.`meta_title`, pl.`name`, i.`id_image`, il.`legend`, ps.`quantity` AS sales, t.`rate`, pl.`meta_keywords`, pl.`meta_title`, pl.`meta_description`
FROM `'._DB_PREFIX_.'category_product` cp
LEFT JOIN `'._DB_PREFIX_.'product` p ON (p.`id_product` = cp.`id_product`)
LEFT JOIN `'._DB_PREFIX_.'product_sale` ps ON ps.`id_product` = p.`id_product`
LEFT JOIN `'._DB_PREFIX_.'product_lang` pl ON (p.`id_product` = pl.`id_product` AND pl.`id_lang` = '.intval($id_lang).')
LEFT JOIN `'._DB_PREFIX_.'image` i ON (i.`id_product` = p.`id_product` AND i.`cover` = 1)
LEFT JOIN `'._DB_PREFIX_.'image_lang` il ON (i.`id_image` = il.`id_image` AND il.`id_lang` = '.intval($id_lang).')
LEFT JOIN `'._DB_PREFIX_.'tax` t ON (t.`id_tax` = p.`id_tax`)
WHERE cp.`id_category` = '.intval($id_category).($active ? ' AND p.`active` = 1' : '').'
GROUP BY p.`id_product`
ORDER BY '.(isset($orderByPrefix) ? $orderByPrefix.'.' : '').'`'.pSQL($orderBy).'` '.pSQL($orderWay).'
LIMIT '.intval($pageNumber * $nbProducts).', '.intval($nbProducts));
if($orderBy == 'price')
{
Tools::orderbyPrice($result,$orderWay);
}
if (!$result)
return false;
return Product::getProductsProperties($id_lang, $result);
}

Then change line 49 of category.php from:

 

$cat_products = $category->getProducts(intval($cookie->id_lang), intval($p), intval($n), $orderBy, $orderWay);

 

to:

 

if ($orderBy == 'sales')
$cat_products = ProductSale::getProductsBySales(intval($cookie->id_lang), $category->id_category, intval($p) - 1, intval($n), $orderBy, $orderWay);
else
$cat_products = $category->getProducts(intval($cookie->id_lang), intval($p), intval($n), $orderBy, $orderWay);

 

Then add the following option to product-sort.tpl in your theme directory, somewhere below the select, depending on where in the dropdown box you want it to appear:

 

{l s='best sellers'}

 

You also need to change the line 4 of product-sort.php from:

 

$orderByValues = array(0 => 'name', 1 => 'price', 2 => 'date_add', 3 => 'date_upd', 4 => 'position', 5 => 'manufacturer_name', 6 => 'quantity');

 

to:

 

$orderByValues = array(0 => 'name', 1 => 'price', 2 => 'date_add', 3 => 'date_upd', 4 => 'position', 5 => 'manufacturer_name', 6 => 'quantity', 7 => 'sales');

 

 

Hi Rocky,

 

What all are the changes we need to do for 1.4.8 version.

 

Thanks.

Link to comment
Share on other sites

  • 3 weeks later...
  • 4 months later...
  • 3 months later...
  • 1 month later...
  • 1 month later...

I've made this simplistic module for a customer with similar wishes, to have products ordered by top sellers.

What it does is basically, where possible order products by number of sold where possible instead of the manual order positions, if theres none sold original positions are kept. You need to go to configure and fire the reorder manually.

Its not exactly what people were trying to achieve in this topic, but does not require any core changes and still far better then reordering by hand.

Note: module has not yet been thoroughly tested, so report please let me know of any problems. It was written for prestashop 1.5

orderproducts.zip

Link to comment
Share on other sites

  • 3 weeks later...

Hi anarchy99,

 

This is tricky, since the ProductSales::getBestSales() function returns only products that have sold at least once and none of the unsold products. I had to write my own function to include unsold products and limit the products to the current category.

 

Add the following function to classes/ProductSale.php:

/*
** Get products sorted by sales including unsold products
**	
** @param integer $id_lang Language id
** @param integer $pageNumber Start from (optional)
** @param integer $nbProducts Number of products to return (optional)
** @return array from Product::getProductProperties
*/
static public function getProductsBySales($id_lang, $id_category, $pageNumber = 0, $nbProducts = 10, $orderBy=NULL, $orderWay=NULL)
{
global $link, $cookie;

if ($pageNumber < 0) $pageNumber = 0;
if ($nbProducts < 1) $nbProducts = 10;
if (empty($orderBy)) $orderBy = 'sales';
if (empty($orderWay)) $orderWay = 'DESC';

$result = Db::getInstance()->ExecuteS('
SELECT p.*, pl.`description`, pl.`description_short`, pl.`link_rewrite`, pl.`meta_description`, pl.`meta_keywords`, pl.`meta_title`, pl.`name`, i.`id_image`, il.`legend`, ps.`quantity` AS sales, t.`rate`, pl.`meta_keywords`, pl.`meta_title`, pl.`meta_description`
FROM `'._DB_PREFIX_.'category_product` cp
LEFT JOIN `'._DB_PREFIX_.'product` p ON (p.`id_product` = cp.`id_product`)
LEFT JOIN `'._DB_PREFIX_.'product_sale` ps ON ps.`id_product` = p.`id_product`
LEFT JOIN `'._DB_PREFIX_.'product_lang` pl ON (p.`id_product` = pl.`id_product` AND pl.`id_lang` = '.intval($id_lang).')
LEFT JOIN `'._DB_PREFIX_.'image` i ON (i.`id_product` = p.`id_product` AND i.`cover` = 1)
LEFT JOIN `'._DB_PREFIX_.'image_lang` il ON (i.`id_image` = il.`id_image` AND il.`id_lang` = '.intval($id_lang).')
LEFT JOIN `'._DB_PREFIX_.'tax` t ON (t.`id_tax` = p.`id_tax`)
WHERE cp.`id_category` = '.intval($id_category).($active ? ' AND p.`active` = 1' : '').'
GROUP BY p.`id_product`
ORDER BY '.(isset($orderByPrefix) ? $orderByPrefix.'.' : '').'`'.pSQL($orderBy).'` '.pSQL($orderWay).'
LIMIT '.intval($pageNumber * $nbProducts).', '.intval($nbProducts));
if($orderBy == 'price')
{	
	Tools::orderbyPrice($result,$orderWay);
}
if (!$result)
	return false;
return Product::getProductsProperties($id_lang, $result);
}

Then change line 49 of category.php from:

 

$cat_products = $category->getProducts(intval($cookie->id_lang), intval($p), intval($n), $orderBy, $orderWay);		

 

to:

 

if ($orderBy == 'sales')
$cat_products = ProductSale::getProductsBySales(intval($cookie->id_lang), $category->id_category, intval($p) - 1, intval($n), $orderBy, $orderWay);
else
$cat_products = $category->getProducts(intval($cookie->id_lang), intval($p), intval($n), $orderBy, $orderWay);

 

Then add the following option to product-sort.tpl in your theme directory, somewhere below the select, depending on where in the dropdown box you want it to appear:

 


{l s='best sellers'}

 

You also need to change the line 4 of product-sort.php from:

 

$orderByValues = array(0 => 'name', 1 => 'price', 2 => 'date_add', 3 => 'date_upd', 4 => 'position', 5 => 'manufacturer_name', 6 => 'quantity');

 

to:

 

$orderByValues = array(0 => 'name', 1 => 'price', 2 => 'date_add', 3 => 'date_upd', 4 => 'position', 5 => 'manufacturer_name', 6 => 'quantity', 7 => 'sales');

 

 

Hello Rocky,

 

This post looks helpful. Thank you.

 

Does this work for PS 1.5.3.1 or do you suggest any further changes?

  • Like 1
Link to comment
Share on other sites

  • 1 month later...
  • 1 month later...
  • 3 years later...

Similarly, your website will be good in Search engine’s eyes if it has links from quality websites but if it has links from spam or irrelevant sites then Search Engines especially Google is not going to value your website in fact, your site’s existing positions might be negatively affected. Tricks Arena

Thus, a website should always have backlinks from good and relevant websites or blogs to perform well in SERPs!

In order to build backlinks safely, you just have to earn them, instead of using black hat or grey hat techniques. You can build SEO friendly backlinks you can write quality guest posts, post infographics, do influencer marketing etc. You can also create a backlink strategy by participating in expert round up posts, writing case studies and so on. Read below to learn more about the strategies to create quality backlinks to your site.

Link to comment
Share on other sites

×
×
  • Create New...