Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't switch to descending mode #3441

Open
ioweb-gr opened this issue Nov 16, 2024 · 7 comments
Open

Can't switch to descending mode #3441

ioweb-gr opened this issue Nov 16, 2024 · 7 comments
Assignees

Comments

@ioweb-gr
Copy link

ioweb-gr commented Nov 16, 2024

Title: Descending Sort Order Doesn't Work on Categories in Elastic Suite

Preconditions

  • Magento Version: 2.4.7-p3 Community
  • Elastic Suite Version: 2.11.9.2
  • Search Engine: Elastic Suite
  • Cache Backend: Redis

Steps to Reproduce

  1. By default all categories get an entry in the database for value = asc

select * from catalog_category_entity_varchar where attribute_id = 221

1102,221,0,1,asc
1103,221,0,2,asc
1104,221,0,3,asc
1105,221,0,4,asc
1106,221,0,5,desc
1107,221,0,6,asc
1108,221,0,7,asc
1109,221,0,86,asc
1110,221,0,101,asc
1111,221,0,8,asc
1112,221,0,9,asc
1113,221,0,10,asc
1114,221,0,11,asc
1115,221,0,14,asc
1116,221,0,15,asc
1117,221,0,16,asc
1118,221,0,18,asc
1119,221,0,19,asc
1120,221,0,69,asc
1121,221,0,70,asc
1122,221,0,71,asc
1123,221,0,72,asc
1124,221,0,87,asc
1125,221,0,88,asc
1126,221,0,92,asc
1127,221,0,93,asc
1128,221,0,94,asc
1129,221,0,95,asc
1130,221,0,96,asc
1131,221,0,97,asc
1132,221,0,98,asc
1133,221,0,99,asc
1134,221,0,100,asc
1135,221,0,102,asc
1136,221,0,103,asc
1137,221,0,20,asc
1138,221,0,21,asc
1139,221,0,22,asc
1140,221,0,23,asc
1141,221,0,24,asc
1142,221,0,25,asc
1143,221,0,26,asc
1144,221,0,27,asc
1145,221,0,28,asc
1146,221,0,29,asc
1147,221,0,30,asc
1148,221,0,31,asc
1149,221,0,32,asc
1150,221,0,33,asc
1151,221,0,34,asc
1152,221,0,35,asc
1153,221,0,36,asc
1154,221,0,38,asc
1155,221,0,39,asc
1156,221,0,40,asc
1157,221,0,41,asc
1158,221,0,42,asc
1159,221,0,43,asc
1160,221,0,47,asc
1161,221,0,48,asc
1162,221,0,49,asc
1163,221,0,50,asc
1164,221,0,51,asc
1165,221,0,52,asc
1166,221,0,53,asc
1167,221,0,55,asc
1168,221,0,56,asc
1169,221,0,57,asc
1170,221,0,58,asc
1171,221,0,59,asc
1172,221,0,60,asc
1173,221,0,61,asc
1174,221,0,62,asc
1175,221,0,63,asc
1176,221,0,64,asc
1177,221,0,65,asc
1178,221,0,66,asc
1179,221,0,67,asc
1180,221,0,68,asc
1181,221,0,73,asc
1182,221,0,74,asc
1183,221,0,75,asc
1184,221,0,76,asc
1185,221,0,77,asc
1186,221,0,78,asc
1187,221,0,79,asc
1188,221,0,80,asc
1189,221,0,81,asc
1190,221,0,82,asc
1191,221,0,83,asc
1192,221,0,84,asc
1193,221,0,85,asc
1194,221,0,89,asc
  1. Set default config setting to descending
  2. Set in the category e.g. entity_id 5 the direction to descending and try with price or created_at date

For example
image

  1. Navigate to the category in the frontend. The order is not honored

image
image

  1. View the sorting results.
    Checking the use config checkbox doens't help either.

Expected Result

  • Products should be displayed in descending order based on the selected attribute.

Actual Result

  • The products do not sort correctly in descending order.

System Info

Attribute Value
Category Count 93
Product Count 22,900
Attribute Count 212
Customer Count 2,846

Additional Notes

  • The issue persists across multiple categories and sorting attributes.
  • No related errors are logged in var/log.

@vahonc
Copy link
Collaborator

vahonc commented Nov 18, 2024

Hello @ioweb-gr,

I was not able to reproduce your issue according to your steps on the my test environment with the fresh Magento 2.4.7-p3 (default sample data and default Luma theme) and Elasticsuite 2.11.9.2.

He're my screenshots:

ScreenShot Tool -20241118122036

ScreenShot Tool -20241118122412

ScreenShot Tool -20241118122724

As you can see, if Price is the selected option for Default Product Listing Sort By and Sort Direction as DESC the products on the frontend are sorted by Price in descending order. The arrow after the option reflects the selected sort order, i.e. its direction depends on the Sort Direction settings of the category in the admin panel. Moreover, from your screenshot with Sort by: Price the sort order is not clear because the prices are quals.

As for if Created at is the selected option for Default Product Listing Sort By seems like it some your customization or it came from some 3rd party extension because out-the-box there are only three options for this dropdown. Could you add more details about this?

Also, are you sure that this issue is not related to some cache or custom theme (in case if it's not based on the default Luma theme)?

BR,
Vadym

@ioweb-gr
Copy link
Author

ioweb-gr commented Nov 18, 2024

I checked it out and indeed it was added with a plugin with this module. Caching is disabled

However even disabling the module doesn't fix my situation.
sort_module.zip

The theme is a child of Luma with some extra css and minimal modifications, nothing that affects the listing toolbar.

I also tested with Luma and get the same, so something must be interfering with the sorting

@ioweb-gr
Copy link
Author

Let's put it on hold until I can pinpoint it better

@ioweb-gr
Copy link
Author

ioweb-gr commented Nov 18, 2024

I managed to trace this to a GoogleTags module which is using category.products.list and product_list_toolbar blocks to load the skus but not quite sure why the conflict.

When commenting out this function everything works

private function getCategoryItems()
    {

        /** @var  ListProduct $list */
        $list = $this->getLayout()->getBlock('category.products.list');
        if (!$list) {
            return [];
        }
        $layer = $list->getLayer();
        if (!$layer) {
            $originalCollection = $list->getLoadedProductCollection();
        } else {
            $originalCollection = $list->getLayer()->getProductCollection();
        }
        $collection = clone $originalCollection;

        /** @var Toolbar $toolbar */
        $toolbar = $this->getLayout()->getBlock('product_list_toolbar');

        $direction = $toolbar->getCurrentDirection();
        $order = $toolbar->getCurrentOrder();
        $limit = $toolbar->getLimit();
        $page = $toolbar->getCurrentPage();

        $collection->setOrder($order, $direction);
        $collection->setPageSize($limit);
        $collection->setCurPage($page);

        $items = $collection->getItems();
        $ids = [];
        foreach ($items as $item) {
            $ids[] = ($this->config->isUseSkuAsIdentifier()) ? $item->getSku() : $item->getProductId();
        }
        return $ids;
    }

When it's added, the sort order is modified and unable to change.
Removing smile elastic suite also makes this work too so the conflict is between them

@vahonc
Copy link
Collaborator

vahonc commented Nov 18, 2024

@ioweb-gr

Can you specify where this code came from?

    {

        /** @var  ListProduct $list */
        $list = $this->getLayout()->getBlock('category.products.list');
        if (!$list) {
            return [];
        }
        $layer = $list->getLayer();
        if (!$layer) {
            $originalCollection = $list->getLoadedProductCollection();
        } else {
            $originalCollection = $list->getLayer()->getProductCollection();
        }
        $collection = clone $originalCollection;

        /** @var Toolbar $toolbar */
        $toolbar = $this->getLayout()->getBlock('product_list_toolbar');

        $direction = $toolbar->getCurrentDirection();
        $order = $toolbar->getCurrentOrder();
        $limit = $toolbar->getLimit();
        $page = $toolbar->getCurrentPage();

        $collection->setOrder($order, $direction);
        $collection->setPageSize($limit);
        $collection->setCurPage($page);

        $items = $collection->getItems();
        $ids = [];
        foreach ($items as $item) {
            $ids[] = ($this->config->isUseSkuAsIdentifier()) ? $item->getSku() : $item->getProductId();
        }
        return $ids;
    }

BR,
Vadym

@ioweb-gr
Copy link
Author

ioweb-gr commented Nov 18, 2024

It's coming from a module a developer of ours created to send the gtag event for page views in category listings

It generates the following code for example, based on the currently viewed products

gtag('event', 'page_view', {
    "send_to": "AW-XXXXXXXX",
    "value": "catalog\/category\/view",
    "items": [{"id": "122918Tabac", "location_id": null, "google_business_vertical": "custom"}, {
        "id": "122918Black",
        "location_id": null,
        "google_business_vertical": "custom"
    }, {"id": "132644Blue", "location_id": null, "google_business_vertical": "custom"}, {
        "id": "132644Red",
        "location_id": null,
        "google_business_vertical": "custom"
    }, {"id": "880144Bordeaux", "location_id": null, "google_business_vertical": "custom"}, {
        "id": "624295Beige",
        "location_id": null,
        "google_business_vertical": "custom"
    }, {"id": "11827054Black", "location_id": null, "google_business_vertical": "custom"}, {
        "id": "11827126Black",
        "location_id": null,
        "google_business_vertical": "custom"
    }, {"id": "624297Fuxia", "location_id": null, "google_business_vertical": "custom"}, {
        "id": "624293Grey",
        "location_id": null,
        "google_business_vertical": "custom"
    }, {"id": "624299Blue", "location_id": null, "google_business_vertical": "custom"}, {
        "id": "624292Grey",
        "location_id": null,
        "google_business_vertical": "custom"
    }, {"id": "624295Grey", "location_id": null, "google_business_vertical": "custom"}, {
        "id": "11825029Black",
        "location_id": null,
        "google_business_vertical": "custom"
    }, {"id": "780-20413 Pink", "location_id": null, "google_business_vertical": "custom"}, {
        "id": "256010Blue",
        "location_id": null,
        "google_business_vertical": "custom"
    }]
});

In default.xml it adds the following block which conditionally loads product ids in the page.

            <block class="Ioweb\GoogleTags\Block\GtagPageView" name="head.gtag.pageview" template="Ioweb_GoogleTags::gtag-page-view.phtml"  />

Then the block is rendered and to do that it invokes the function mentioned above

    {

        /** @var  ListProduct $list */
        $list = $this->getLayout()->getBlock('category.products.list');
        if (!$list) {
            return [];
        }
        $layer = $list->getLayer();
        if (!$layer) {
            $originalCollection = $list->getLoadedProductCollection();
        } else {
            $originalCollection = $list->getLayer()->getProductCollection();
        }
        $collection = clone $originalCollection;

        /** @var Toolbar $toolbar */
        $toolbar = $this->getLayout()->getBlock('product_list_toolbar');

        $direction = $toolbar->getCurrentDirection();
        $order = $toolbar->getCurrentOrder();
        $limit = $toolbar->getLimit();
        $page = $toolbar->getCurrentPage();

        $collection->setOrder($order, $direction);
        $collection->setPageSize($limit);
        $collection->setCurPage($page);

        $items = $collection->getItems();
        $ids = [];
        foreach ($items as $item) {
            $ids[] = ($this->config->isUseSkuAsIdentifier()) ? $item->getSku() : $item->getProductId();
        }
        return $ids;
    }

to retrieve the product ids from the currently viewed collection taking into account the sort / pagination settings from the toolbar.

However unfortunately the plugin used to change the order via the smile elastic suite is actually executing after the block's code is executed thus there's a conflict between them.

@ioweb-gr
Copy link
Author

Fun fact, moving the block to before.body.end makes them work without issues because the plugin is activated when the toolbar list is already loaded :) So I guess there's a workaround for now

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants