magento / magento2

Prior to making any Submission(s), you must sign an Adobe Contributor License Agreement, available here at: https://opensource.adobe.com/cla.html. All Submissions you make to Adobe Inc. and its affiliates, assigns and subsidiaries (collectively “Adobe”) are subject to the terms of the Adobe Contributor License Agreement.
http://www.magento.com
Open Software License 3.0
11.52k stars 9.31k forks source link

Category Sort (position, name, price) behavior has changed in M2.4.5 #36045

Open n2diving-dgx opened 2 years ago

n2diving-dgx commented 2 years ago

Preconditions and environment

Category Sort (position, name, price) behavior has changed in M2.4.5

The sorted order of products appearing in catalog category pages is controlled by the Sort By setting selected by the user from a drop down appearing in the upper right corner of the category page.

Previously the out-of-stock status of a product had no effect on the sort order, but as of update to M2.4.5 this appears to be working differently, out-of-stock items are not sorting in to the correct order regardless of setting. Instead, starting with M2.4.5 the out-of-stock items are always listed at the end.

Steps to reproduce

This can be reproduced in a clean installition of M2.4.5 with the Luma data as follows...

(1) perform clean installtion of M2.4.5 with the Luma store data (2) In frontend website navigate to Home > Gear > Watches, and observe that the position of Luma Analog Watch is appropriate and correct in the order of products displayed for all Sort By selections. (3) In admin backend Catalog > Product > Luma Analog Watch > Advanced Inventory, uncheck Use Config Settings and change Out-of-Stock Threshold value to be greater than current Qty, in this case value = 101, alternately change the current QTY value to zero (either method will cause same results) then Done and Save. (4) in admin backend Stores > Configuration > Catalog > Inventory, uncheck Use System Value and change Display Out Of Stock Products value to Yes, then Save Config (5) in admin backend Cache Management, Flush Magento Cache (6) In frontend Home > Gear > Watches observe that the position of Luma Analog Watch is ALWAYS at the end of products displayed regardless of Sort By selection

Expected result

Sort order of products displayed in catalog pages is not affected by their stock status.

Actual result

Instead, starting with M2.4.5 the out-of-stock items are always listed at the end. Previously the out-of-stock status of a product had no effect on the sort order, but as of update to M2.4.5 this is working differently, out-of-stock items are not sorting in to the correct order regardless of setting.

Additional information

There are plenty of 3rd party trivial extensions and patches that do this, but the M.2.4.5 Release Notes do not document this significant change in the sort behavior that greatly impacts the presentation order of products in categories on our website and our customers ability to find them. There does not appear to be an admin method or setting to change this new behavior, at least not one I can find or that is documented in the M2.4.5 Release Notes.

Additional Information

In frontend website navigate to Home > Gear > Watches, and observe that the position of Luma Analog Watch is appropriate and correct in the order of products displayed for all Sort By selections.

dj dj 2 dj 3

in admin backend Stores > Configuration > Catalog > Inventory, uncheck Use System Value and change Display Out Of Stock Products value to Yes, then Save Config.

29

In frontend Home > Gear > Watches observe that the position of Luma Analog Watch is ALWAYS at the end of products displayed regardless of Sort By selection

30 31 32

Release note

No response

Triage and priority

m2-assistant[bot] commented 2 years ago

Hi @n2diving-dgx. Thank you for your report. To speed up processing of this issue, make sure that you provided the following information:

Make sure that the issue is reproducible on the vanilla Magento instance following Steps to reproduce. To deploy vanilla Magento instance on our environment, Add a comment to the issue:

@magento give me 2.4-develop instance - upcoming 2.4.x release

For more details, review the Magento Contributor Assistant documentation.

Add a comment to assign the issue: @magento I am working on this

To learn more about issue processing workflow, refer to the Code Contributions.


:clock10: You can find the schedule on the Magento Community Calendar page.

:telephone_receiver: The triage of issues happens in the queue order. If you want to speed up the delivery of your contribution, join the Community Contributions Triage session to discuss the appropriate ticket.

:pencil2: Feel free to post questions/proposals/feedback related to the Community Contributions Triage process to the corresponding Slack Channel

m2-assistant[bot] commented 2 years ago

Hi @engcom-Bravo. Thank you for working on this issue. In order to make sure that issue has enough information and ready for development, please read and check the following instruction: :point_down:

engcom-Bravo commented 2 years ago

@magento give me 2.4.5 instance

magento-deployment-service[bot] commented 2 years ago

Hi @engcom-Bravo. Thank you for your request. I'm working on Magento instance for you.

magento-deployment-service[bot] commented 2 years ago

Hi @engcom-Bravo, here is your Magento Instance: https://5e5d0f349cf5a8b92a9a20ae6a6614a9.instances.magento-community.engineering Admin access: https://5e5d0f349cf5a8b92a9a20ae6a6614a9.instances.magento-community.engineering/admin_6bb8 Login: f78e120c Password: 9d49ef37185d

engcom-Bravo commented 2 years ago

@magento give me 2.4-develop instance

magento-deployment-service[bot] commented 2 years ago

Hi @engcom-Bravo. Thank you for your request. I'm working on Magento instance for you.

magento-deployment-service[bot] commented 2 years ago

Hi @engcom-Bravo, here is your Magento Instance: https://aa1378467a4c00f6164c0bf8f7924648.instances.magento-community.engineering Admin access: https://aa1378467a4c00f6164c0bf8f7924648.instances.magento-community.engineering/admin_864c Login: 5711046f Password: 97fa6d798e23

engcom-Bravo commented 2 years ago

Hi @n2diving-dgx,

Precondition: • Magento 2.4-develop && 2.4.5 • PHP 8.1

Thank you for reporting and collaboration.

Verified the issue on Magento 2.4-develop branch and 2.4.5 project-community-edition as well the issue is reproducible.

Kindly refer the screenshots.

1.In frontend website navigate to Home > Gear > Watches, and observe that the position of Luma Analog Watch is appropriate and correct in the order of products displayed for all Sort By selections.

dj dj 2 dj 3
  1. In admin backend Catalog > Product > Luma Analog Watch > Advanced Inventory, uncheck Use Config Settings and change Out-of-Stock Threshold value to be greater than current Qty, in this case value = 101, alternately change the current QTY value to zero (either method will cause same results) then Done and Save.
28

3.in admin backend Stores > Configuration > Catalog > Inventory, uncheck Use System Value and change Display Out Of Stock Products value to Yes, then Save Config.

29

4.In frontend Home > Gear > Watches observe that the position of Luma Analog Watch is ALWAYS at the end of products displayed regardless of Sort By selection

30 31 32

Products (out-of-stock items) are always listed at the end.

Hence Confirming this issue.

Thanks.

github-jira-sync-bot commented 2 years ago

:white_check_mark: Jira issue https://jira.corp.adobe.com/browse/AC-6587 is successfully created for this GitHub issue.

m2-assistant[bot] commented 2 years ago

:white_check_mark: Confirmed by @engcom-Bravo. Thank you for verifying the issue.
Issue Available: @engcom-Bravo, You will be automatically unassigned. Contributors/Maintainers can claim this issue to continue. To reclaim and continue work, reassign the ticket to yourself.

pmonosolo commented 2 years ago

Try this fix: https://github.com/magento/inventory/issues/3360

codebase-md commented 2 years ago

Hi there,

this is happening due to a change in function \Magento\Elasticsearch\Model\ResourceModel\Fulltext\Collection\SearchResultApplier::apply(). In line 108 the function getProductIdsBySaleability() checks setting cataloginventory/options/show_out_of_stock and if enabled and a category id is given(in other words you are on a category page) it calls the function categoryProductByCustomSortOrder($categoryId)(line 190) which resets the entire collections select statement and adds on line 213 a sort order for is_salable with hardcoded direction DESC. Additionally we found out that this function introduces another issue. It prevents custom sort orders to be applied due to the reset of the select statement. I created a patch which fixes this behavior(allows custom sorting an removes sorting by stock status):

Index: vendor/magento/module-elasticsearch/Model/ResourceModel/Fulltext/Collection/SearchResultApplier.php
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/vendor/magento/module-elasticsearch/Model/ResourceModel/Fulltext/Collection/SearchResultApplier.php b/vendor/magento/module-elasticsearch/Model/ResourceModel/Fulltext/Collection/SearchResultApplier.php
--- a/vendor/magento/module-elasticsearch/Model/ResourceModel/Fulltext/Collection/SearchResultApplier.php   
+++ b/vendor/magento/module-elasticsearch/Model/ResourceModel/Fulltext/Collection/SearchResultApplier.php   (date 1662619604901)
@@ -210,20 +210,14 @@
         $storeId = $this->collection->getStoreId();
         $searchCriteria = $this->searchResult->getSearchCriteria();
         $sortOrders = $searchCriteria->getSortOrders() ?? [];
-        $sortOrders = array_merge(['is_salable' => \Magento\Framework\DB\Select::SQL_DESC], $sortOrders);

         $connection = $this->collection->getConnection();
-        $query = clone $connection->select()
+        $query = clone $this->collection->getSelect()
             ->reset(\Magento\Framework\DB\Select::ORDER)
             ->reset(\Magento\Framework\DB\Select::LIMIT_COUNT)
-            ->reset(\Magento\Framework\DB\Select::LIMIT_OFFSET)
-            ->reset(\Magento\Framework\DB\Select::COLUMNS);
-        $query->from(
-            ['e' => $this->collection->getTable('catalog_product_entity')],
-            ['e.entity_id']
-        );
+            ->reset(\Magento\Framework\DB\Select::LIMIT_OFFSET);
+
         $this->stockStatusApplier->setSearchResultApplier(true);
-        $query = $this->stockStatusFilter->execute($query, 'e', 'stockItem');
         $query->join(
             ['cat_index' => $this->collection->getTable('catalog_category_product_index_store' . $storeId)],
             'cat_index.product_id = e.entity_id'

I can't promise that it's the best solution but it solved the problem for now. Feedback welcome.

pmonosolo commented 2 years ago

$storeId = $this->collection->getStoreId(); $searchCriteria = $this->searchResult->getSearchCriteria(); $sortOrders = $searchCriteria->getSortOrders() ?? [];

  • $sortOrders = array_merge(['is_salable' => \Magento\Framework\DB\Select::SQL_DESC], $sortOrders);

     $connection = $this->collection->getConnection();
  • $query = clone $connection->select()
  • $query = clone $this->collection->getSelect() ->reset(\Magento\Framework\DB\Select::ORDER) ->reset(\Magento\Framework\DB\Select::LIMIT_COUNT)
  • ->reset(\Magento\Framework\DB\Select::LIMIT_OFFSET)
  • ->reset(\Magento\Framework\DB\Select::COLUMNS);
  • $query->from(
  • ['e' => $this->collection->getTable('catalog_product_entity')],
  • ['e.entity_id']
  • );
  • ->reset(\Magento\Framework\DB\Select::LIMIT_OFFSET);
  •  $this->stockStatusApplier->setSearchResultApplier(true);
  • $query = $this->stockStatusFilter->execute($query, 'e', 'stockItem'); $query->join( ['cat_index' => $this->collection->getTable('catalog_category_product_index_store' . $storeId)], 'cat_index.product_id = e.entity_id'

This does not work for me unfortunately - the toolbar still shows the full catalog:

image

It means that the whole catalog is being outputted regardless of the search query.

codebase-md commented 2 years ago

@pmonosolo

$storeId = $this->collection->getStoreId(); $searchCriteria = $this->searchResult->getSearchCriteria(); $sortOrders = $searchCriteria->getSortOrders() ?? [];

  •  $sortOrders = array_merge(['is_salable' => \Magento\Framework\DB\Select::SQL_DESC], $sortOrders);
    
     $connection = $this->collection->getConnection();
  •  $query = clone $connection->select()
  •  $query = clone $this->collection->getSelect()
         ->reset(\Magento\Framework\DB\Select::ORDER)
         ->reset(\Magento\Framework\DB\Select::LIMIT_COUNT)
  •      ->reset(\Magento\Framework\DB\Select::LIMIT_OFFSET)
  •      ->reset(\Magento\Framework\DB\Select::COLUMNS);
  •  $query->from(
  •      ['e' => $this->collection->getTable('catalog_product_entity')],
  •      ['e.entity_id']
  •  );
  •      ->reset(\Magento\Framework\DB\Select::LIMIT_OFFSET);
  •  $this->stockStatusApplier->setSearchResultApplier(true);
  •  $query = $this->stockStatusFilter->execute($query, 'e', 'stockItem');
     $query->join(
         ['cat_index' => $this->collection->getTable('catalog_category_product_index_store' . $storeId)],
         'cat_index.product_id = e.entity_id'

This does not work for me unfortunately - the toolbar still shows the full catalog:

image

It means that the whole catalog is being outputted regardless of the search query.

I'm sorry to hear that. But this patch is not intended to fix problems with the amount of products shown in your catalog. It is intended to fix the issue in regards to sort orders and position at which out of stock products are shown.

You might have a different issue here since I'm not able to reproduce the behavior described by you.