Smile-SA / elasticsuite

Smile ElasticSuite - Magento 2 merchandising and search engine built on ElasticSearch
https://elasticsuite.io
Open Software License 3.0
761 stars 341 forks source link

Widget collection position_by_sku not applying #3401

Closed ttraskback closed 3 weeks ago

ttraskback commented 1 month ago

After update from 2.4.6-p5 to 2.4.7-p2 pagebuilder widget carousel no longer works with position_by_sku.

Preconditions

Magento Version : EE 2.4.7-p2

ElasticSuite Version : 2.11.8.3

Environment : Developer and Production both

Steps to reproduce

  1. Crate a block that contains a carousel widget
  2. Add by sku
  3. Add skus at random in a order that is nether ascending or descending
  4. Set "Sort By" to Position

Expected result

  1. src/module-elasticsuite-virtual-category/Plugin/Widget/ProductsListPlugin.php:143 should get true
  2. Elasticsearch query should contain script to order them this way
  3. Products should be orders as you entered

Actual result

  1. src/module-elasticsuite-virtual-category/Plugin/Widget/ProductsListPlugin.php:143 Is true
  2. No script tag in elasitcsearch request
  3. Products are ordered in descending order

This is the log for the request: curl -XPOST 'http://elasticsearch:9200/magento2_2_finnish_catalog_product/_search?pretty=true' -d '{"size":0,"query":{"constant_score":{"filter":{"bool":{"must":[{"bool":{"must":[{"bool":{"must":[],"must_not":[],"should":[{"match":{"sku.untouched":{"query":"19843","minimum_should_match":"100%","boost":1}}},{"match":{"sku.untouched":{"query":"19844","minimum_should_match":"100%","boost":1}}},{"match":{"sku.untouched":{"query":"19858","minimum_should_match":"100%","boost":1}}},{"match":{"sku.untouched":{"query":"19884","minimum_should_match":"100%","boost":1}}},{"match":{"sku.untouched":{"query":"181264","minimum_should_match":"100%","boost":1}}},{"match":{"sku.untouched":{"query":"194488","minimum_should_match":"100%","boost":1}}},{"match":{"sku.untouched":{"query":"198599","minimum_should_match":"100%","boost":1}}},{"match":{"sku.untouched":{"query":"220158","minimum_should_match":"100%","boost":1}}},{"match":{"sku.untouched":{"query":"224904","minimum_should_match":"100%","boost":1}}},{"match":{"sku.untouched":{"query":"253491","minimum_should_match":"100%","boost":1}}},{"match":{"sku.untouched":{"query":"253493","minimum_should_match":"100%","boost":1}}},{"match":{"sku.untouched":{"query":"304010","minimum_should_match":"100%","boost":1}}},{"match":{"sku.untouched":{"query":"615925","minimum_should_match":"100%","boost":1}}}],"minimum_should_match":1,"boost":1}}],"must_not":[],"should":[],"boost":1,"_name":"Jos KAIKKI n\u00e4ist\u00e4 olosuhteista on TOTTA:\n SKU (sku) on yksi 19843,19844,19858,19884,181264,194488,198599,220158,224904,253491,253493,304010,615925"}},{"terms":{"visibility":[2,4],"boost":1}},{"terms":{"visibility":[2,4],"boost":1}}],"must_not":[],"should":[],"boost":1}},"boost":1}},"aggregations":{"attribute_set_id":{"terms":{"field":"attribute_set_id","size":10000,"order":{"_count":"desc"}}},"indexed_attributes":{"terms":{"field":"indexed_attributes","size":10000,"order":{"_count":"desc"}}}},"track_total_hits":true}'

joacols commented 3 weeks ago

Hi, i found the problem with this. src/module-elasticsuite-virtual-category/Plugin/PageBuilder/Catalog/Sorting/OptionPlugin.php position_by_sku sorting happens on a afterGetCollection plugin. After that plugin, magento will also try to sort using the vendor/magento/module-page-builder/Model/Catalog/Sorting/SimpleOption.php
image image

as you can see, nothing is done because the virtual type for position_by_sku does not meet the conditions. as you can see, smile plugin is just reseting collection when it is not required

joacols commented 3 weeks ago

also, The plugin itself is no longer needed. Here are the 3 classes that implement this method image image image image

as you can see, every class is responsible of reseting the collection when necesary

ill work on a pr, not sure if i can contribute directly

romainruaud commented 3 weeks ago

turns out that Magento moved a plugin from global di.xml to adminhtml.xml and frontend.xml : https://github.com/magento/magento2-page-builder/commit/979c95999463b00cd21f4730161cce90dfe887f6

Therefore, our plugin which was supposed to go after the Magento one, was triggered before, and our sort order was reset.

The PR #3422 contains a proposal for a fix, can you give it a try ? It's easy to report it locally.

Regards