massmagic Posted June 2, 2022 Share Posted June 2, 2022 Hi everyone, Is there any way to set default sorting products in "Best Sellers"? I have checked section "Preferences--> Products--> Pagination Tab" but there isn't any option for "Best sellers" My ecommerce is updated to prestashop 1.7.8.6 Thank you in advance. Link to comment Share on other sites More sharing options...
Prestachamps Posted June 3, 2022 Share Posted June 3, 2022 Hi, you can add the option into the Backoffice by modifiying these files: src\PrestaShopBundle\Form\Admin\Configure\ShopParameters\ProductPreferences\PaginationType.php: add the : 'Best Sales' => 8, public function buildForm(FormBuilderInterface $builder, array $options) { $builder ->add('products_per_page', IntegerType::class) ->add('default_order_by', ChoiceType::class, [ 'choices' => [ 'Product name' => 0, 'Product price' => 1, 'Product add date' => 2, 'Product modified date' => 3, 'Position inside category' => 4, 'Brand' => 5, 'Product quantity' => 6, 'Product reference' => 7, 'Best Sales' => 8, ], 'required' => true, ]) Also create a new override forr the Tools class and add this function for it (add the "sales" options for the array and the $prefix condtions): public static function getProductsOrder($type, $value = null, $prefix = false) { switch ($type) { case 'by': $list = [0 => 'name', 1 => 'price', 2 => 'date_add', 3 => 'date_upd', 4 => 'position', 5 => 'manufacturer_name', 6 => 'quantity', 7 => 'reference', 8 => 'sales']; $value = (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' || $value == 'sales') { $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; break; case 'way': $value = (null === $value || $value === false || $value === '') ? (int) Configuration::get('PS_PRODUCTS_ORDER_WAY') : $value; $list = [0 => 'asc', 1 => 'desc']; return (isset($list[$value])) ? $list[$value] : ((in_array($value, $list)) ? $value : 'asc'); break; } } I hope that I could help. Have a nice day, Leo. Link to comment Share on other sites More sharing options...
massmagic Posted June 4, 2022 Author Share Posted June 4, 2022 Hi Leo, thank you for this solution I will give it a try in next few days. Ti farlò sapere se è tutto ok! Ancora grazie e a presto Link to comment Share on other sites More sharing options...
massmagic Posted July 1, 2022 Author Share Posted July 1, 2022 Hi Leo, I have tried the solution provided but with no luck. The new order type 'Best sales' is present in backoffice.. I can apply it from backoffice as default sorting mode but on front office is not applied any order as default sorting (It's selected 'choose a sorting mode') Can you help me? Thanks Link to comment Share on other sites More sharing options...
Prestachamps Posted July 1, 2022 Share Posted July 1, 2022 Hi @massmagic, do you have any filter module on category pages where the sorting is not showing up? like amazzingfilter or advancedsearch module? Have a nice day, Leo. Link to comment Share on other sites More sharing options...
Delfhin Posted September 1, 2022 Share Posted September 1, 2022 Hi all, May I know if this solution is supposed to work for Brands product listing page? The codes were added in and it does sort by best sellers at product listing page but if I navigate to brands -> products, it will show a blank page with no products. Is there a way to sort by best-sellers for brands->products? Regards Link to comment Share on other sites More sharing options...
laczfy Posted October 10, 2022 Share Posted October 10, 2022 On 6/3/2022 at 3:51 PM, Prestachamps said: Hi, you can add the option into the Backoffice by modifiying these files: src\PrestaShopBundle\Form\Admin\Configure\ShopParameters\ProductPreferences\PaginationType.php: add the : 'Best Sales' => 8, public function buildForm(FormBuilderInterface $builder, array $options) { $builder ->add('products_per_page', IntegerType::class) ->add('default_order_by', ChoiceType::class, [ 'choices' => [ 'Product name' => 0, 'Product price' => 1, 'Product add date' => 2, 'Product modified date' => 3, 'Position inside category' => 4, 'Brand' => 5, 'Product quantity' => 6, 'Product reference' => 7, 'Best Sales' => 8, ], 'required' => true, ]) Also create a new override forr the Tools class and add this function for it (add the "sales" options for the array and the $prefix condtions): public static function getProductsOrder($type, $value = null, $prefix = false) { switch ($type) { case 'by': $list = [0 => 'name', 1 => 'price', 2 => 'date_add', 3 => 'date_upd', 4 => 'position', 5 => 'manufacturer_name', 6 => 'quantity', 7 => 'reference', 8 => 'sales']; $value = (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' || $value == 'sales') { $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; break; case 'way': $value = (null === $value || $value === false || $value === '') ? (int) Configuration::get('PS_PRODUCTS_ORDER_WAY') : $value; $list = [0 => 'asc', 1 => 'desc']; return (isset($list[$value])) ? $list[$value] : ((in_array($value, $list)) ? $value : 'asc'); break; } } I hope that I could help. Have a nice day, Leo. This solution seems to be working, but you have to set the Default order method option on the Shop Parameters > Product Settings page to Descending. Link to comment Share on other sites More sharing options...
haunter Posted April 13, 2023 Share Posted April 13, 2023 It's working, just clear cache after implementation. Link to comment Share on other sites More sharing options...
Digiteo Posted October 26, 2023 Share Posted October 26, 2023 Hello, In my case it didnt work - i made changes in PaginationType.php and create a new override Tools.php. Now i can see "Best Sales" option in backoffice, but it doesnt work in front (cache cleared). Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now