0

I am trying to get results of products by stock quantity filter

i use this inside the constructor of my helper class.

 \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $product_lib

The question is how do i add stock quantity from and to in this?

$this->product_lib->create()->addAttributeToSelect('*')->
addCategoriesFilter(array('in' => array(1,2,3))->
addFieldToFilter('price', ['from' => $min_price, 'to' => $max_price])
->load();

When i try to additional addFieldToFilter with quantity it says not found. What i am looking for is to achieve the same results as in catalog->products filter.

Any suggestion is helpful. Also if possible can you suggest where to do i look for these kinds of information? Is there any book that would be helpful?

2
  • you means product filter using in stock quantity??? If yes check this link ---magento.stackexchange.com/questions/215617/… May 23, 2020 at 12:05
  • Thank you for answer. I want to get all products where stock_quantity >= $min_quantity and stock_quantity <= $max_quantity. But the above link seems to check if product is in status or not. May 23, 2020 at 12:27

1 Answer 1

1
/** @var ProductCollection $collection */
$collection = $this->productCollectionFactory->create()
    ->addAttributeToSelect('status')
    ->addFieldToFilter('status', Status::STATUS_ENABLED)
    ->joinField(
        'qty',
        'cataloginventory_stock_item',
        'qty',
        'product_id=entity_id',
        'qty>0'
    );

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.