hryvinskyi / magento2-catalog-sorting-out-of-stock
Catalog sorting rule: in-stock products before out-of-stock ones in category listings and search results.
Package info
github.com/hryvinskyi/magento2-catalog-sorting-out-of-stock
Type:magento2-module
pkg:composer/hryvinskyi/magento2-catalog-sorting-out-of-stock
Requires
- php: ~8.1.0||~8.2.0||~8.3.0||~8.4.0||~8.5.0
- hryvinskyi/magento2-catalog-sorting: ^1.0
- magento/framework: *
- magento/module-backend: *
- magento/module-catalog-inventory: *
- magento/module-config: *
- magento/module-inventory-catalog-api: *
- magento/module-inventory-elasticsearch: *
- magento/module-store: *
Requires (Dev)
None
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
Lists in-stock products before out-of-stock ones on category pages and search results, whatever sort the shopper chose. A leading sort rule of hryvinskyi/magento2-catalog-sorting.
What it does for a store
- In-stock first. Category pages, quick search and advanced search list in-stock products first; the shopper's sort orders each group.
- One stock order on every listing. The rule owns its field: a stock clause the platform adds to a listing query (it does on search results) is replaced by this rule's when the setting is on, and removed when it is off. With the setting at No, the shopper's sort alone decides, search results included.
- Missing data never buries a product. A product without stock data in its search document counts as in stock.
- A neutral order where stock is hidden. A store that hides stock from some shoppers can give them an order that does not reveal it. Which shoppers that is comes from a module that registers a concealment (see Extending); with none registered, the setting has no effect.
- No work per request. The order is on the out-of-stock flag the platform's inventory modules already write into each product search document (
is_out_of_stock, 1 = not salable in the stock of the store's website). The module computes no stock and adds no document field.
Requirements
- PHP 8.1 to 8.5.
hryvinskyi/magento2-catalog-sorting^1.0.- The platform's Store, Config, Backend and CatalogInventory modules, and the inventory modules InventoryCatalogApi and InventoryElasticsearch (what
composer.jsonandetc/module.xmldeclare).
Installation
composer require hryvinskyi/magento2-catalog-sorting-out-of-stock bin/magento setup:upgrade bin/magento setup:di:compile bin/magento cache:flush
The rule is on by default once the module is installed.
Configuration
Stores › Configuration › Catalog › Catalog Sorting › Out-of-Stock Products
| Path | Label | Default | Scope |
|---|---|---|---|
catalog_sorting/out_of_stock/enabled |
Show Out-of-Stock Products Last | Yes | default, website, store view |
catalog_sorting/out_of_stock/neutral_when_stock_hidden |
Keep Order Neutral When Stock Is Hidden (shown when the first is Yes) | No | default, website, store view |
When Catalog › Inventory › Stock Options › Display Out of Stock Products is No at the scope being edited, the group shows a warning: out-of-stock products are not listed at all, so the setting has nothing to move.
With Keep Order Neutral When Stock Is Hidden on, a shopper from whom stock is hidden gets no stock order. Where most products are out of stock, that shopper's first page may then show mostly unavailable products.
Extending
A module that hides stock from some shoppers implements Api\StockOrderConcealmentInterface (isConcealed(int $storeId): is stock hidden from the current shopper in this store) and adds it to the concealments argument of the rule:
<type name="Hryvinskyi\CatalogSortingOutOfStock\Model\OutOfStockLastRule"> <arguments> <argument name="concealments" xsi:type="array"> <item name="vendor_hidden_stock" xsi:type="object">Vendor\Module\Model\HiddenStockConcealment</item> </argument> </arguments> </type>
Concealments are asked only in a store where Keep Order Neutral When Stock Is Hidden is Yes. The answer is about the whole listing, not one product. The rule is item out_of_stock (sortOrder 100) of the rules pool of Hryvinskyi\CatalogSorting\Model\Search\LeadingSorts.