Can anyone please suggest on this .

I have more than 50k products in my website and I have created an attribute name "Discontinued Items" with yes/no option to show the product status like in stock or out of stock so there is no need to disable the products for SEO purpose if product is not longer exist in warehouse.

Now I want to list products in the below order at category page and search result page also product order should be work with sort by filter(position, price, product name) options .

  1. In stock Products
  2. Out if stock Products
  3. Discontinued Products

Currently I am using magento 2.2.3 version.

Can anyone please give the suggestion how it will be done.

up vote 1 down vote accepted

You can modify the products sort order for listing by using the custom observer on catalog_block_product_list_collection event.

I assume the "Discontinued Items" attribute code is discontinued_items_flag

From admin you can perform below steps:

go to Stores->Attributes->Product than click and edit your attribute.

Used in Product Listing = Yes

Used for Sorting in Product Listing = Yes

Your observer code will be look like:

$collection = $observer->getEvent()->getCollection();

//If you have multi location inventory
//$websiteId = $this->_storeManager->getStore()->getWebsiteId();
$websiteId = 0;

/* For In/Out of stock Products */
$stockId = 'stock_id';
$collection->getSelect()->joinLeft(
    array('_inv' => $collection->getResource()->getTable('cataloginventory_stock_status')),
    "_inv.product_id = e.entity_id and _inv.website_id=$websiteId and _inv.stock_id=$stockId",
    array('stock_status')
);

$collection->addExpressionAttributeToSelect('in_stock', 'IFNULL(_inv.stock_status,0)', array());
$collection->getSelect()->reset('order');

$collection->addAttributeToSort('in_stock', 'DESC');
$collection->addAttributeToSort('discontinued_items_flag', 'ASC');
return $this;

To work with filter you can do like:

/* set __construct dependency for  \Magento\Framework\App\Request\Http $request */
$sortOrder = $this->request->getParam('product_list_order');
$sortDir = $this->request->getParam('product_list_dir');
$sortDir = (empty($sortDir))?'ASC':$sortDir;

switch ($sortOrder) {
        case 'position':
        $collection->addAttributeToSort('in_stock', 'DESC');
        $collection->addAttributeToSort('discontinued_items_flag', 'ASC');
        $collection->addAttributeToSort('position', $sortDir);
        break;

        case 'name':
        //$finalSortOrder = 'name';
        $collection->addAttributeToSort('in_stock', 'DESC');
        $collection->addAttributeToSort('discontinued_items_flag', 'ASC');
        $collection->addAttributeToSort('name', $sortDir);
        break;

        default:
        $collection->addAttributeToSort('in_stock', 'DESC');
        $collection->addAttributeToSort('discontinued_items_flag', 'ASC');
        $collection->addAttributeToSort('position', $sortDir);
        break;
    }
  • Thanks @Suman Singh it will help to me – Jack Dec 12 at 13:54

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.