1

In a basic block I'm getting the featured products, however I only get the products that are in stock:

public function getProducts()
{
    $collection = $this->_productCollectionFactory->create();
    $collection->addAttributeToSelect('*')
        ->addAttributeToFilter('featured', '1');

    $collection->getSelect()
        ->limit(8);

    return $collection;
}

How can I get the out of stock products (I guess those which stock_status=0 in cataloginventory_stock_status) as well? without changing "anything" in the backend configuration.

6
  • I tried this, it actually returns all products to me, either in stock or out of stock, then in phtml you can filter by $_product->isSaleable(). Are you sure it returns only in stock products to you?
    – Ekta Puri
    Aug 4, 2016 at 0:11
  • I don't know if it's because of something in my backend config, but yes, it definitely looks like I only get in stock products.
    – Shaz
    Aug 4, 2016 at 1:21
  • is this your custom module?
    – Ekta Puri
    Aug 4, 2016 at 1:22
  • there is setting in admin configuration -> catalog -> inventory -> stock options -> Display Out of Stock Products . try that
    – Ekta Puri
    Aug 4, 2016 at 1:26
  • You're right, it would probably work, but what about if that parameter has to be always NO, how can I still see those out of stock products in my module?
    – Shaz
    Aug 4, 2016 at 1:36

1 Answer 1

0

Yes,you can be get out of product in your collection whenever Stock options -> Display Out of Stock Products = no

Add below code to $collection.

    $cond = [
        '{{table}}.use_config_manage_stock = 0 AND {{table}}.manage_stock=1 AND {{table}}.is_in_stock=1',
        '{{table}}.use_config_manage_stock = 0 AND {{table}}.manage_stock=0'
    ];

        $cond[] = '{{table}}.use_config_manage_stock = 1';

    $collection->joinField(
        'inventory_in_stock',
        'cataloginventory_stock_item',
        'is_in_stock',
        'product_id=entity_id',
        '(' . join(') OR (', $cond) . ')'
    );

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.