Jump to content

Sort by "date added"


philbauch

Recommended Posts

Hi,

I wonder if there's an option to add some "Order by date added" to the order dropdown in a product list. Or as a filter in facetted search (e.g. "New products"

Usually you always want to show your latest arrivals first, but PS orders obviously by id ASC, which means that oldest products are showing first.

Any ideas welcome, thanks in advance,

Phil

Link to comment
Share on other sites

13 hours ago, philbauch said:

Hi,

I wonder if there's an option to add some "Order by date added" to the order dropdown in a product list. Or as a filter in facetted search (e.g. "New products"

Usually you always want to show your latest arrivals first, but PS orders obviously by id ASC, which means that oldest products are showing first.

Any ideas welcome, thanks in advance,

Phil

You can change it in Advanced Settings -> Products...

sortby.thumb.JPG.be6da8a70e6f4415248eadd374db4d7d.JPG

And if you want to add a sort order, you can edit /modules/ps_facetedsearch/src/product/searchprovider.php and add some code in the getAvailableSortOrders() function...

    private function getAvailableSortOrders()
    {
        $sortPosAsc = new SortOrder('product', 'position', 'asc');
        $sortNameAsc = new SortOrder('product', 'name', 'asc');
        $sortNameDesc = new SortOrder('product', 'name', 'desc');
        $sortPriceAsc = new SortOrder('product', 'price', 'asc');
        $sortPriceDesc = new SortOrder('product', 'price', 'desc');
        $sortDateAsc = new SortOrder('product', 'date_add', 'asc');
        $sortDateDesc = new SortOrder('product', 'date_add', 'desc');
        $translator = $this->module->getTranslator();

        return [
            $sortPosAsc->setLabel(
                $translator->trans('Relevance', [], 'Modules.Facetedsearch.Shop')
            ),
            $sortNameAsc->setLabel(
                $translator->trans('Name, A to Z', [], 'Shop.Theme.Catalog')
            ),
            $sortNameDesc->setLabel(
                $translator->trans('Name, Z to A', [], 'Shop.Theme.Catalog')
            ),
            $sortPriceAsc->setLabel(
                $translator->trans('Price, low to high', [], 'Shop.Theme.Catalog')
            ),
            $sortPriceDesc->setLabel(
                $translator->trans('Price, high to low', [], 'Shop.Theme.Catalog')
            ),
            $sortDateAsc->setLabel(
                $translator->trans('Data, do mais antigo para o mais recente', [], 'Modules.Facetedsearch.Shop')
            ),
            $sortDateDesc->setLabel(
                $translator->trans('Data, do mais recente para o mais antigo', [], 'Modules.Facetedsearch.Shop')
            ),
        ];
    }

Best Regards

  • Like 2
  • Thanks 2
Link to comment
Share on other sites

Fantastic, lots of thanks! Starting step by step to get deeper in PS and it's structure, helped a lot.

One last question concerning translations (although not that urgent, in the beginning shop will be in english only):

Can new translation patterns easily be added in the backend? Or is it just the only way to download the xlf's and code it the files? Don't want to destroy nothing, and in the translations I haven't found e.g. "Date, newest first" or something like this...

Thanks & best regards

Phil

Link to comment
Share on other sites

1 hour ago, philbauch said:

Can new translation patterns easily be added in the backend? Or is it just the only way to download the xlf's and code it the files? Don't want to destroy nothing, and in the translations I haven't found e.g. "Date, newest first" or something like this...

I didn´t find it etheir, it should be on Modules.Facetedsearch.Shop translations, if you look there it says 2 untranslated expressions but they don´t show up, that´s why i translated it in the code. I also looked into the xfl file but there are some fields there that i didn´t understood the logic!

Best regards

Link to comment
Share on other sites

  • 3 months later...
On 12/28/2019 at 4:23 AM, PCQUATRO said:

You can change it in Advanced Settings -> Products...

sortby.thumb.JPG.be6da8a70e6f4415248eadd374db4d7d.JPG

And if you want to add a sort order, you can edit /modules/ps_facetedsearch/src/product/searchprovider.php and add some code in the getAvailableSortOrders() function...


    private function getAvailableSortOrders()
    {
        $sortPosAsc = new SortOrder('product', 'position', 'asc');
        $sortNameAsc = new SortOrder('product', 'name', 'asc');
        $sortNameDesc = new SortOrder('product', 'name', 'desc');
        $sortPriceAsc = new SortOrder('product', 'price', 'asc');
        $sortPriceDesc = new SortOrder('product', 'price', 'desc');
        $sortDateAsc = new SortOrder('product', 'date_add', 'asc');
        $sortDateDesc = new SortOrder('product', 'date_add', 'desc');
        $translator = $this->module->getTranslator();

        return [
            $sortPosAsc->setLabel(
                $translator->trans('Relevance', [], 'Modules.Facetedsearch.Shop')
            ),
            $sortNameAsc->setLabel(
                $translator->trans('Name, A to Z', [], 'Shop.Theme.Catalog')
            ),
            $sortNameDesc->setLabel(
                $translator->trans('Name, Z to A', [], 'Shop.Theme.Catalog')
            ),
            $sortPriceAsc->setLabel(
                $translator->trans('Price, low to high', [], 'Shop.Theme.Catalog')
            ),
            $sortPriceDesc->setLabel(
                $translator->trans('Price, high to low', [], 'Shop.Theme.Catalog')
            ),
            $sortDateAsc->setLabel(
                $translator->trans('Data, do mais antigo para o mais recente', [], 'Modules.Facetedsearch.Shop')
            ),
            $sortDateDesc->setLabel(
                $translator->trans('Data, do mais recente para o mais antigo', [], 'Modules.Facetedsearch.Shop')
            ),
        ];
    }

Best Regards

Thank you PCQUATRO. That's exactly what I was looking for. But I have one problem. After the changes in the file "searchprovider.php" the sort by date is displaying on category pages but not on the search result pages. How to add sort by date to serach result pages as well? 

Link to comment
Share on other sites

15 minutes ago, bartes1987 said:

Thank you PCQUATRO. That's exactly what I was looking for. But I have one problem. After the changes in the file "searchprovider.php" the sort by date is displaying on category pages but not on the search result pages. How to add sort by date to serach result pages as well? 

Can you post images?

Best Regards

Link to comment
Share on other sites

11 minutes ago, bartes1987 said:

This is product listing on search result page where the new sort options aren't displaying:

Have you cleared the cache on both the browser and prestashop? you can also try to delete /var/cache...

What is your store´s url?

Best Regards

Link to comment
Share on other sites

7 hours ago, bartes1987 said:

Yes, I cleared the cache on both: browser and the store. Store url is: https://ohbutik.pl/

It looks like you have another search module on your shop, that code only works in the faceted search module, you have to search in your search module the code for sorting and change it accordingly. You can also try disabling it and enabling faceted search to see what you get...

Best Regards

Link to comment
Share on other sites

9 hours ago, PCQUATRO said:

It looks like you have another search module on your shop, that code only works in the faceted search module, you have to search in your search module the code for sorting and change it accordingly. You can also try disabling it and enabling faceted search to see what you get...

Best Regards

I don't think so. I checked this method on other store: https://zero-procent.pl which uses the default theme and definitely has no other search or filter module and the results are the same. It displays correctly on category listings but doesn't work on the search result listings. 

Edited by bartes1987 (see edit history)
Link to comment
Share on other sites

2 hours ago, bartes1987 said:

I don't think so. I checked this method on other store: https://zero-procent.pl which uses the default theme and definitely has no other search or filter module and the results are the same. It displays correctly on category listings but doesn't work on the search result listings. 

As i told you, you must also find the code that controls the search module, witch in this case is in /src/core/product/search/SortOrdersCollection.php and change it accordingly.

Best Regards 

Link to comment
Share on other sites

  • 3 months later...

Hello,

Thank you very much worked like a charm

But how to override these changes so we don't loose them in the next update of the module ?

I tried to create the same directory in MYTHEME/modules but seems like it does take only CSS and tpl changes according to this documentation

https://devdocs.prestashop.com/1.7/themes/reference/overriding-modules/

Any idea please ?

Thank you

Link to comment
Share on other sites

3 minutes ago, Amorino said:

I tried that but still didn't work

The problem is when module will update I will surely loose my changes

That is a way of overriding, you don´t loose overrides!!

You can also save the changed files with another name, write down the changes, than when there is an update to the module just redo the changes.

Best Regards

Link to comment
Share on other sites

On 12/28/2019 at 8:53 AM, PCQUATRO said:

You can change it in Advanced Settings -> Products...

sortby.thumb.JPG.be6da8a70e6f4415248eadd374db4d7d.JPG

And if you want to add a sort order, you can edit /modules/ps_facetedsearch/src/product/searchprovider.php and add some code in the getAvailableSortOrders() function...


    private function getAvailableSortOrders()
    {
        $sortPosAsc = new SortOrder('product', 'position', 'asc');
        $sortNameAsc = new SortOrder('product', 'name', 'asc');
        $sortNameDesc = new SortOrder('product', 'name', 'desc');
        $sortPriceAsc = new SortOrder('product', 'price', 'asc');
        $sortPriceDesc = new SortOrder('product', 'price', 'desc');
        $sortDateAsc = new SortOrder('product', 'date_add', 'asc');
        $sortDateDesc = new SortOrder('product', 'date_add', 'desc');
        $translator = $this->module->getTranslator();

        return [
            $sortPosAsc->setLabel(
                $translator->trans('Relevance', [], 'Modules.Facetedsearch.Shop')
            ),
            $sortNameAsc->setLabel(
                $translator->trans('Name, A to Z', [], 'Shop.Theme.Catalog')
            ),
            $sortNameDesc->setLabel(
                $translator->trans('Name, Z to A', [], 'Shop.Theme.Catalog')
            ),
            $sortPriceAsc->setLabel(
                $translator->trans('Price, low to high', [], 'Shop.Theme.Catalog')
            ),
            $sortPriceDesc->setLabel(
                $translator->trans('Price, high to low', [], 'Shop.Theme.Catalog')
            ),
            $sortDateAsc->setLabel(
                $translator->trans('Data, do mais antigo para o mais recente', [], 'Modules.Facetedsearch.Shop')
            ),
            $sortDateDesc->setLabel(
                $translator->trans('Data, do mais recente para o mais antigo', [], 'Modules.Facetedsearch.Shop')
            ),
        ];
    }

Best Regards

Thanks Man!!!
It saved lots of time....😃

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