Jump to content

Default Product Sorting - Best Sellers


massmagic

Recommended Posts

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

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

  • 4 weeks later...

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

  • 1 month later...

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.

image.thumb.png.2b64e1e0b574247726c7484a0d454275.png

Is there a way to sort by best-sellers for brands->products?

 

Regards

Link to comment
Share on other sites

  • 1 month later...
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

  • 6 months later...
  • 6 months later...

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