0

I want to filter product collection by stock and get only In stock products. I have tried multiple ways and followed multiple stack solutions but nothing works as expected.
I am using the Smartwave Porto theme. In the Porto theme, there's is a feature for showing the latest products on the front end. So, I want to filter the latest products collection by stock.

Block File Code:

public function getProducts() {
        $count = $this->getProductCount();
        $category_id = $this->getData("category_id");
        $collection = clone $this->_collection;
        $collection->clear()->getSelect()->reset(\Magento\Framework\DB\Select::WHERE)->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::GROUP);

        if(!$category_id) {
            $category_id = $this->_storeManager->getStore()->getRootCategoryId();
        }
        $category = $this->categoryRepository->get($category_id);
        if(isset($category) && $category) {
            $collection->addMinimalPrice()
                ->addFinalPrice()
                ->addTaxPercents()
                ->addAttributeToSelect('name')
                ->addAttributeToSelect('image')
                ->addAttributeToSelect('small_image')
                ->addAttributeToSelect('thumbnail')
                ->addAttributeToSelect($this->_catalogConfig->getProductAttributes())
                ->addUrlRewrite() 
                ->addCategoryFilter($category)
                ->addAttributeToSort('created_at','desc');
        } else {
            $collection->addMinimalPrice()
                ->addFinalPrice()
                ->addTaxPercents()
                ->addAttributeToSelect('name')
                ->addAttributeToSelect('image')
                ->addAttributeToSelect('small_image')
                ->addAttributeToSelect('thumbnail')
                ->addAttributeToSelect($this->_catalogConfig->getProductAttributes())
                ->addUrlRewrite()
                ->addAttributeToSort('created_at','desc');
        }

        $collection->getSelect()
                ->order('created_at','desc')
                ->limit($count);

        return $collection;
    }

    public function getLoadedProductCollection() {
        return $this->getProducts();
    }

PHTML code:

$_productCollection = $block->getLoadedProductCollection();

I used the below code in the phtml as well as block file as per @davidas answer, but not working:

$_productCollection->setFlag('has_stock_status_filter', true);
$_productCollection = $_productCollection
->joinField('qty',
        'cataloginventory_stock_item',
        'qty',
        'product_id=entity_id',
        '{{table}}.stock_id=1',
        'left'
    )->joinTable('cataloginventory_stock_item', 'product_id=entity_id', array('stock_status' => 'is_in_stock'))
    ->addAttributeToSelect('stock_status')
    ->addFieldToFilter('stock_status', ['eq' => 1])
->load();

Note: MSI feature is used in Magento.

Magento version 2.3.3-p1

1 Answer 1

1

try below code

$productCollection = $objectManager->create('Magento\Catalog\Model\ResourceModel\Product\CollectionFactory');
$collection = $productCollection->create();
$collection->setFlag('has_stock_status_filter', true);
$collection = $collection->addAttributeToFilter('entity_id', array('from' => $start ,'to' => $end))->addAttributeToSelect(array('name', 'sku'))
->addAttributeToSort('created_at', 'DESC')
->joinField('qty',
        'cataloginventory_stock_item',
        'qty',
        'product_id=entity_id',
        '{{table}}.stock_id=1',
        'left'
    )->joinTable('cataloginventory_stock_item', 'product_id=entity_id', array('stock_status' => 'is_in_stock'))
    ->addAttributeToSelect('stock_status')
    ->addFieldToFilter('stock_status', ['eq' => 1])
->load();
6
  • @Davidas I tried this but not working.
    – Amit Saini
    Jun 9, 2021 at 11:28
  • update your question with code. so we will check
    – Devidas
    Jun 9, 2021 at 11:39
  • @Davidas please check updaed question
    – Amit Saini
    Jun 9, 2021 at 11:50
  • change this code in your template file $_productCollection->getSelect()->joinField and check
    – Devidas
    Jun 9, 2021 at 11:57
  • Error filtering template: Unrecognized method 'joinField()' this error is showing.
    – Amit Saini
    Jun 9, 2021 at 12:25

Your Answer

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

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