Adding a ‘sort by sales’ option



  • Offering the possibility to order products by sales when browsing categories is a great way to have your customers know about your best items. The following guide by @Nemo works in thirty bees. As he is talking about an old PS version in it, don’t bother with his Download Project Files, just follow his manual directions and it will work great. Thanks @Nemo!



  • I vote for adding it to the core



  • If anyone wants to do it, and is having problems, I can give you my files…



  • @foolab said in Adding a ‘sort by sales’ option:

    I vote for adding it to the core

    Me too…



  • Hi,

    I get a right best sales sort, with some little modifications, i ll try to don’t forget some there, there is 4 files to modifify:

    In Tools.php, replace the function getProductsOrder() by:

        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 = (is_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') {
                            $order_by_prefix = 'p.';
                        } elseif ($value == 'name') {
                            $order_by_prefix = 'pl.';
                        } elseif ($value == 'sales') {
                            $order_by_prefix = 'psa.';
                            $value = 'quantity desc, psa.sale_nbr';
                        } 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 = (is_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;
            }
        }
    

    In modules/blocklayered/blocklayered.php, line 3146, insert just after 'LEFT JOIN '.DB_PREFIX.‘manufacturer m ON […]’ and before 'WHERE ‘.$alias_where […]’:

    LEFT JOIN `'._DB_PREFIX_.'product_sale` psa ON psa.id_product = p.id_product
    

    so you get:

    LEFT JOIN '._DB_PREFIX_.'manufacturer m ON (m.id_manufacturer = p.id_manufacturer)
    				'.Product::sqlStock('p', 0).'
    LEFT JOIN `'._DB_PREFIX_.'product_sale` psa ON psa.id_product = p.id_product
    WHERE '.$alias_where.'.`active` = 1 AND '.$alias_where.'.`visibility` IN ("both", "catalog")
    

    And in themes/community-theme-default/product-sort.tpl, add this line, where you want in the select tag:

              {if $page_name == 'category'}
                <option value="sales:desc"{if $orderby eq 'sales' AND $orderway eq 'desc'} selected="selected"{/if}>{l s='Best Sales'}</option>
              {/if}
    

    Last file, in ‘themes/community-theme-default/js/modules/blocklayered/blocklayered.js’, Line 637 and 643, find this line :

    reloadContent('forceSlide');
    

    and replace by :

    reloadContent('&forceSlide');
    

    That’s all, clear your cache-file, and run.
    Regards.

    Edit: One more file to change, and delete wrong uploads.



  • @Lathaneo , nice job. You might want to make the first two changes overrides though, so you’re not modifying the core…



  • @lathaneo said in Adding a ‘sort by sales’ option:

    In modules/blocklayered/blocklayered.php, just before the ‘where […]’ line 3146, insert:

    LEFT JOIN `'._DB_PREFIX_.'product_sale` psa ON psa.id_product = p.id_product
    

    so you get:

    LEFT JOIN '._DB_PREFIX_.'manufacturer m ON (m.id_manufacturer = p.id_manufacturer)
    				'.Product::sqlStock('p', 0).'
    LEFT JOIN `'._DB_PREFIX_.'product_sale` psa ON psa.id_product = p.id_product
    WHERE '.$alias_where.'.`active` = 1 AND '.$alias_where.'.`visibility` IN ("both", "catalog")
    

    there are two instances of that code, nether are on line 3146, do you change one, or both?

    [0_1515061457658_blocklayered.php](Uploading 100%)

    [0_1515061485981_product-sort.tpl](Uploading 100%) [0_1515061499590_Tools.php](Uploading 100%)

    Your files didnt show up, might wanna try again.



  • Hi,

    Sorry for the uploaded files, I don’t have enough privileges to share them.
    I edited and added some modifications.

    You can download file in my git repository, but have still have to change the template file manually.
    (I changed the file name of blocklayered.js in blocklayered_mod.js cause the template override)

    https://github.com/Lathanao/blocklayered

    Regards.



  • @Nemo, I have an issue with this, and that is, in best-sales, new-products, and prices-drop, the sort by sales does not work. When viewing any category it works fine, but in those ‘special’ pages, it just doesn’t work. You can see this at my website. Any thoughts?


  • Global Moderator

    Yeah, they do not use category::getProducts, but each a specific function (getPricesDrop, getNewProducts…) of the product class



  • Is there modification to each of those functions one can do to make the sort work, or is it not feasible?



  • Hi,

    @SLiCK_303, you’re right, that why i add the select option only for category page.

        {if $page_name == 'category'}
                <option value="sales:desc"{if $orderby eq 'sales' AND $orderway eq 'desc'} selected="selected"{/if}>{l s='Best Sales'}</option>
              {/if}
    

    The sort in category page is made only by the blocklayered module.
    If you want add the sort ‘Best Sales’ on other pages, you have just to modify the class which is doing the sort. For BestSales page, it’s in Productsale::getBestSales(), etc… so, each page get it’s own sort function.

    And, yes it’s should be really cool to get only one function, fully parameterizable, for all this sorts.
    And, yes, of course it’s faisable.

    Regards.



  • @lathaneo said in Adding a ‘sort by sales’ option:

              {if $page_name == 'category'}
                <option value="sales:desc"{if $orderby eq 'sales' AND $orderway eq 'desc'} selected="selected"{/if}>{l s='Best Sales'}</option>
              {/if}
    

    Thanks for that, I didn’t think of doing it…


Log in to reply
 

Looks like your connection to thirty bees forum was lost, please wait while we try to reconnect.