anarchy99 Posted May 24, 2009 Share Posted May 24, 2009 Hello world ; )How can I add " Sort by Best Sellers " in product page?I try to find the way out by my self first, but it's not working.What I have to do with product-sort.tpl files ?Thank you guys : ) Link to comment Share on other sites More sharing options...
rocky Posted May 25, 2009 Share Posted May 25, 2009 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 More sharing options...
rocky Posted May 25, 2009 Share Posted May 25, 2009 Oops, that last one was supposed to be:EDIT: See code above 1 Link to comment Share on other sites More sharing options...
rocky Posted May 25, 2009 Share Posted May 25, 2009 You also need to change the line 4 of product-sort.php from:EDIT: See code above Link to comment Share on other sites More sharing options...
anarchy99 Posted May 25, 2009 Author Share Posted May 25, 2009 Thank you very much, rocky.it's work very great.Please accept my respect.; ) Link to comment Share on other sites More sharing options...
anarchy99 Posted May 26, 2009 Author Share Posted May 26, 2009 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 More sharing options...
Dio Posted May 26, 2009 Share Posted May 26, 2009 Rocky rocks :-) Thanks m8! All works fine! Link to comment Share on other sites More sharing options...
rocky Posted May 26, 2009 Share Posted May 26, 2009 You are right. Sorry for that oversight. Replace the ProductSales.php function above with:EDIT: See code above Link to comment Share on other sites More sharing options...
anarchy99 Posted May 26, 2009 Author Share Posted May 26, 2009 Thank you again , Rocky.Another question is, how can I choose to sort by best sales in " Manufacturer " ?Regards, Link to comment Share on other sites More sharing options...
eqilibrium Posted July 20, 2009 Share Posted July 20, 2009 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 More sharing options...
mark2 Posted March 3, 2010 Share Posted March 3, 2010 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.tplRegards, Link to comment Share on other sites More sharing options...
Emmanuel Paris Posted December 6, 2010 Share Posted December 6, 2010 Great code. Thanks Rocky !Any idea on how to apply this to the Best Sales Block also ? Link to comment Share on other sites More sharing options...
Daniel - PrestaBR Posted March 18, 2011 Share Posted March 18, 2011 Couldn't Get it Working... Can't find the error =(Can anybody post the working files for versions 1.3 / 1.4 ??Thanks.DGV Link to comment Share on other sites More sharing options...
Rolige Posted December 14, 2011 Share Posted December 14, 2011 Hello rocky, It's posibble use this modification in PS 1.4.6.2? Link to comment Share on other sites More sharing options...
Nagraj Posted August 25, 2012 Share Posted August 25, 2012 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 More sharing options...
fernandokerber Posted September 12, 2012 Share Posted September 12, 2012 Is this still workin on 1.4.x? Link to comment Share on other sites More sharing options...
chetwyn Posted January 24, 2013 Share Posted January 24, 2013 bump? Link to comment Share on other sites More sharing options...
PanMS Posted May 16, 2013 Share Posted May 16, 2013 Rocky, man, can you modify the code to make this work in presta 1.5? Link to comment Share on other sites More sharing options...
RazrElite Posted June 24, 2013 Share Posted June 24, 2013 Has there been any update to this for more recent version of Prestashop? Such as the latest 1.4 versions? Or does anyone know any alternatives or modules that does this? Link to comment Share on other sites More sharing options...
iztok Posted August 5, 2013 Share Posted August 5, 2013 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 More sharing options...
PrestaFanBoy Posted August 23, 2013 Share Posted August 23, 2013 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? 1 Link to comment Share on other sites More sharing options...
GraceGoh Posted September 30, 2013 Share Posted September 30, 2013 Rocky, is there a fix for version 1.5.4.x ? Really appreciate if you could advise us Link to comment Share on other sites More sharing options...
iztok Posted November 19, 2013 Share Posted November 19, 2013 (edited) The module i made has been updated here is the latest version, it now supports excluding categories and also ordering by value of sales not just number Any updates can also be found on my github orderproducts.zip Edited November 19, 2013 by iztok (see edit history) 2 Link to comment Share on other sites More sharing options...
tricksaren Posted November 26, 2016 Share Posted November 26, 2016 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 More sharing options...
Recommended Posts