0

I want to Implement a new ordering function to order products in product lists pages based on Qty (Stock). I tried but something goes wrong. Help me out.

di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Catalog\Model\Layer">
        <plugin name="product_sort_by_stock" type="Company\Module\Model\Plugin\Layer"/>
    </type>
</config>

....\Plugin\Catalog\Block

class SortStock
{

    const STOCK_SORT_BY = 'qty';

    public function afterGetAttributeUsedForSortByArray(Config $subject, $result)
    {
        return array_merge($result, [self::STOCK_SORT_BY => __('Qty')]);
    }

    
    public function beforeSetCollection(Toolbar $subject, Collection $collection)
    {

        if ($subject->getCurrentOrder() === self::STOCK_SORT_BY) {

            $collection->getSelect()->joinLeft(
                array('_inv' => $collection->getResource()->getTable('cataloginventory_stock_status')),
                "_inv.product_id = e.entity_id",
                array('stock_status')
            );
            $collection->addExpressionAttributeToSelect('in_stock', 'IFNULL(_inv.stock_status,0)', array());
            $collection->getSelect()->reset('order');
            $collection->getSelect()->order('in_stock DESC');

        }
    }
}
1
  • I am getting this error : "You cannot define a correlation name '_inv' more than once" Commented Mar 3, 2022 at 10:08

2 Answers 2

0

Use <plugin name="product_sort_by_stock" type="Company\Module\Model\Plugin\Layer"/>

to

<plugin name="product_sort_by_stock" type="Company\Module\Model\Plugin\Catalog\Block\SortStock"/>.

Thank You!

1
  • Thanks @mukesh, Solved error but products are not loading. It gives this msg : "We can't find products matching the selection." Commented Mar 4, 2022 at 4:41
0

This is how I have fixed this one. It would help others.

di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Catalog\Model\Layer">
        <plugin name="product_sort_by_stock" type="Company\Vendor\Model\Plugin\Layer"/>
    </type>
    <type name="Magento\Catalog\Model\Config">
        <plugin name="after_get_available_orders" type="Company\Vendor\Model\Plugin\Layer"/>
    </type>
</config>

Company\Module\Model\Plugin\Layer.php

<?php

namespace Company\Vendor\Model\Plugin;
use Magento\Catalog\Model\Config;
use Magento\Framework\Data\Collection;

class Layer
{
    /**
     * Stock Quantity sorting attribute
     */

    const STOCK_SORT_BY = 'stock_status';

    /**
     * @param Config $subject
     * @param $result
     * @return mixed
     */
    public function afterGetAttributeUsedForSortByArray(Config $subject, $result)
    {
        return array_merge($result, [self::STOCK_SORT_BY => __('Stock Qty')]);
    }

    /**
     * Collection to get data sort from stock qty
     *
     * @param \Magento\Catalog\Model\Layer $subject
     * @param Collection $collection
     * @return void
     */
    public function beforeSetCollection(\Magento\Catalog\Model\Layer $subject, Collection $collection)
    {
        if ($subject->getCurrentOrder() === self::STOCK_SORT_BY) {

            $setDirection = $subject->getCurrentDirection();
            $collection->getSelect()->joinLeft(
                array('_stockqty' => $collection->getResource()->getTable('cataloginventory_stock_status')),
                "_stockqty.product_id = e.entity_id",
                array('stock_status')
            );
            $collection->addExpressionAttributeToSelect('in_stock', 'IFNULL(_stockqty.stock_status,0)', array());
            $collection->getSelect()->reset('order');
            $collection->getSelect()->setOrder('in_stock ' .$setDirection);
            return $collection->getData();
        }
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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