Jump to content

[SOLVED] Top Sellers question


mohsart

Recommended Posts

Would it be possible to change the built-in Top Sellers block like this...

1. Show Top Sellers within the cathegory the customer's in, eg if my customer is looking for problem books for Go it would show the top selling Go Problem Books, not overall Top Selling products which may be Chess Boards.

2. When listing All Top Sellers it would make more sense to list them in top selling order rather than alphabetically.

Thanks for any ideas,

/Mats

Link to comment
Share on other sites

Is this a strange request?

I would think that eg someone selling baby clothes would want someone browsing for Boy Baby Clothings to see only top selling boy clothes and not girls, or someone in a record store looking at Metal seeing top selling Metal and not Hip Hop...

/Mats

Link to comment
Share on other sites

It seems everyone is getting less and less patient on these forums lately. %-P

It's a good idea. Try changing line 99 of classes/ProductSales.php (in PrestaShop v1.3.1) from:

static public function getBestSalesLight($id_lang, $pageNumber = 0, $nbProducts = 10)



to:

static public function getBestSalesLight($id_lang, $pageNumber = 0, $nbProducts = 10, $id_category = 0)



and line 114 from:

WHERE p.`active` = 1



to:

WHERE p.`active` = 1'.($id_category > 0 ? ' AND p.`id_category_default` = '.$id_category : '').'



and then change line 35 of modules/blockbestsellers/blockbestsellers.php from:

$bestsellers = ProductSale::getBestSalesLight(intval($params['cookie']->id_lang), 0, 5);



to:

$id_category = 0;

if (isset($_GET['id_category']) AND $_GET['id_category'] > 0)
{
   $category = new Category($_GET['id_category']);
   if (Validate::isLoadedObject($category))
       $id_category = $_GET['id_category'];
}

$bestsellers = ProductSale::getBestSalesLight(intval($params['cookie']->id_lang), 0, 5, $id_category);



You need to go to Preferences > Products and change the "Default order way" to "Decreasing" to have top sellers sorted from most sales to least sales.

Link to comment
Share on other sites

Thanks!
I just realized how you got your nick - you Rock! :-)

One weird thing though:
http://mohsart.se/13-bocker-pa-engelska and http://mohsart.se/35-pertigenomgangar-analyser
In both categories there's a book called invincible but it only shows up as a best seller in the first
Same for the Yunzi stones here
http://mohsart.se/26-stenar and http://mohsart.se/14-utrustning

/Mats
PS Sorry for being impatient

Link to comment
Share on other sites

My code above uses the default category of the product, since it will only appear as a best seller in the product's default category. It would require a more complicated query to select all categories the product is in. You'd need to instead change line 114 of classes/ProductSale.php from:

WHERE p.`active` = 1



to:

LEFT JOIN `'._DB_PREFIX_.'category_product` cp ON (p.`id_product` = cp.`id_product`)
WHERE p.`active` = 1 AND sales > 0 AND '.($id_category > 0 ? ' AND cp.`id_category` = '.$id_category : '').'



This will also exclude products that haven't been sold yet.

Link to comment
Share on other sites

  • 3 months later...
  • 1 month later...

Try:

$id_category = 0;

if (isset($_GET['id_category']) AND intval($_GET['id_category']) > 0)
{
   $category = new Category(intval($_GET['id_category']), intval($params['cookie']->id_lang));
   if (Validate::isLoadedObject($category))
       $id_category = intval($_GET['id_category']);
}
elseif (isset($_GET['id_product']) AND intval($_GET['id_product']) > 0)
{
   $product = new Product(intval($_GET['id_product']), false, intval($params['cookie']->id_lang));
   if (Validate::isLoadedObject($product))
       $id_category = intval($product->id_category_default);
}

$bestsellers = ProductSale::getBestSalesLight(intval($params['cookie']->id_lang), 0, 5, $id_category); 

Link to comment
Share on other sites

  • 1 year later...

Hello,

 

Can you say to me or put this code in the product.php file

 

Thank you

 

$id_category = 0;

if (isset($_GET['id_category']) AND intval($_GET['id_category']) > 0)
{
   $category = new Category(intval($_GET['id_category']), intval($params['cookie']->id_lang));
   if (Validate::isLoadedObject($category))
    $id_category = intval($_GET['id_category']);
}
elseif (isset($_GET['id_product']) AND intval($_GET['id_product']) > 0)
{
   $product = new Product(intval($_GET['id_product']), false, intval($params['cookie']->id_lang));
   if (Validate::isLoadedObject($product))
    $id_category = intval($product->id_category_default);
}

$bestsellers = ProductSale::getBestSalesLight(intval($params['cookie']->id_lang), 0, 5, $id_category);

Link to comment
Share on other sites

You have to put this code into blockbestsellers.php.

Here is how mine looks, for PS 1.3.2 and a Bestsellers block hooked on right column.

 

 

<?php
if (!defined('_CAN_LOAD_FILES_'))
exit;

class BlockBestSellers extends Module
{
private $_html = '';
private $_postErrors = array();
function __construct()
{
	$this->name = 'blockbestsellers';
	$this->tab = 'Blocks';
	$this->version = '1.1';
	parent::__construct();
	$this->displayName = $this->l('Top seller block');
	$this->description = $this->l('Add a block displaying the shop\'s top sellers');
}
public function install()
{
	if (!parent::install() OR
  !$this->registerHook('rightColumn') OR
  !$this->registerHook('updateOrderStatus') OR
  !ProductSale::fillProductSales())
return false;
  return true;
}

function hookRightColumn($params)
{
 global $smarty;
 $currency = new Currency(intval($params['cookie']->id_currency));
 $id_category = 0;

 if (isset($_GET['id_category']) AND intval($_GET['id_category']) > 0)
 {
  $category = new Category(intval($_GET['id_category']), intval($params['cookie']->id_lang));
  if (Validate::isLoadedObject($category))
$id_category = intval($_GET['id_category']);
 }
 elseif (isset($_GET['id_product']) AND intval($_GET['id_product']) > 0)
 {
  $product = new Product(intval($_GET['id_product']), false, intval($params['cookie']->id_lang));
  if (Validate::isLoadedObject($product))
$id_category = intval($product->id_category_default);
 }

 $bestsellers = ProductSale::getBestSalesLight(intval($params['cookie']->id_lang), 0, 5, $id_category);

 $best_sellers = array();
 foreach ($bestsellers AS $bestseller)
 {
  $bestseller['price'] = Tools::displayPrice(Product::getPriceStatic(intval($bestseller['id_product'])), $currency);
  $best_sellers[] = $bestseller;
 }
 $smarty->assign(array(
  'best_sellers' => $best_sellers,
  'mediumSize' => Image::getSize('medium')));
 return $this->display(__FILE__, 'blockbestsellers.tpl');
}

function hookLeftColumn($params)
{
 return $this->hookRightColumn($params);
}
}
?>

Link to comment
Share on other sites

Thank you, it's perfect. Also work on presta 1.4.6.2

 

I replaced the code into blockbestsellers.php

 

$id_category = 0;

if (isset($_GET['id_category']) AND $_GET['id_category'] > 0)
{
   $category = new Category($_GET['id_category']);
   if (Validate::isLoadedObject($category))
    $id_category = $_GET['id_category'];
}

$bestsellers = ProductSale::getBestSalesLight(intval($params['cookie']->id_lang), 0, 5, $id_category);

 

by

 

$id_category = 0;

if (isset($_GET['id_category']) AND intval($_GET['id_category']) > 0)
{
   $category = new Category(intval($_GET['id_category']), intval($params['cookie']->id_lang));
   if (Validate::isLoadedObject($category))
	    $id_category = intval($_GET['id_category']);
}
elseif (isset($_GET['id_product']) AND intval($_GET['id_product']) > 0)
{
   $product = new Product(intval($_GET['id_product']), false, intval($params['cookie']->id_lang));
   if (Validate::isLoadedObject($product))
	    $id_category = intval($product->id_category_default);
}

$bestsellers = ProductSale::getBestSalesLight(intval($params['cookie']->id_lang), 0, 5, $id_category);

Link to comment
Share on other sites

  • 1 month later...

The top sellers page on my shop is acting weird. As of now it is 'all products sold list, in no particular order'. The pagination doesn't work but I'd like to eliminate the pagination and have it like 50 top selling products, ordered by number of sales so the top selling one is the first etc. Now in the classes/productsale.php there is this function:

 

static public function getBestSales($id_lang, $pageNumber = 0, $nbProducts = 10, $orderBy=NULL, $orderWay=NULL)

{

if ($pageNumber < 0) $pageNumber = 0;

if ($nbProducts < 1) $nbProducts = 10;

if (empty($orderBy) || $orderBy == 'position') $orderBy = 'sales';

if (empty($orderWay)) $orderWay = 'DESC';

 

but it doesn't sort it by sales, as a matter of fact it looks like it sorts them randomly, and sometimes in groups based on category. The very top selling product is right in the middle. I can change the number of products here but how to make it to be sorted by sales desc?

Link to comment
Share on other sites

  • 3 years later...

Hi, i would create query to shows 

PRODUC TNAME, COUNTRY, QUANTITY SALES and SALE DATE. 

Because in STATS->BEST-SELLING PRODUCTS i can't shows also country that products are sold

 

Please help me

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