2

Following this guide Magento 2 : How To Show Price of "out of stock" Products

I successfully managed to show price for out of stock products. But this applies only to simple products. For configurable out of stock products the price printed is always 0,00 while I would like to show the lowest price of simple products composing its parent.

How can I do it?

  • Did you find a solution for this issue? – rtk0407 Feb 23 '17 at 12:59
  • No, unfortunenately – gianis6 Feb 23 '17 at 13:05
  • Did you figure it out ? – ahsan Dec 5 '18 at 22:42
3

Not sure if this is applicable, but I'm on Magento 2.1.9 and there is an

issue with configurable products showing price of $0.00 when child products are out of stock on Github.

The hack, Albeit a core hack, is the only available remedy it seems.

I added the code & commented below, into

vendor/magento/module-configurable-product/Pricing/Price/ConfigurablePriceResolver.php

and it fixed my issue.

    /**
 * @param \Magento\Framework\Pricing\SaleableInterface|\Magento\Catalog\Model\Product $product
 * @return float|null
 */
public function resolvePrice(\Magento\Framework\Pricing\SaleableInterface $product)
{
    $price = null;

    foreach ($this->lowestPriceOptionsProvider->getProducts($product) as $subProduct) {
        $productPrice = $this->priceResolver->resolvePrice($subProduct);
        $price = $price ? min($price, $productPrice) : $productPrice;
    }

    /* fix >>> */
    $price = $price ?: $product->getData('price');
    /* <<< fix */

    return $price === null ? null : (float)$price;
}
  • I have tried this solution and its also working for me as well. – Aasim Goriya Nov 17 '17 at 5:27
0

@gianis6 We have the same issue and tracked it back to the LinkedProductSelectBuilderComposite which builds a bunch of SELECT statements. Each of the linkedProductSelectBuilder objects in that class get the StockStatusBaseSelectProcessor class injected into it (module-catalog-inventory/etc/di.xml). That stock status base select processor adds the table and where clause needed to restrict products that are out of stock.

So, my solution is to remove the StockStatusBaseSelectProcessor from the CompositeBaseSelectProcessor when building the select statements.

WARNING: This code may be used by several components in Magento 2. We're still testing to see if it has any adverse effects. If you use this, please run a full UAT to make sure it doesn't break other code.

In the {Vendor}/{Module}/etc/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">
    <preference for="Magento\Catalog\Model\ResourceModel\Product\CompositeBaseSelectProcessor" type="{Vendor}\{Module}\Model\ResourceModel\Product\CompositeBaseSelectProcessor" />

</config>

In the {Vendor}/{Module}/Model/ResourceModel/Product/CompositeBaseSelectProcessor.php

namespace {Vendor}\{Module}\Model\ResourceModel\Product;

use Magento\CatalogInventory\Model\ResourceModel\Product\StockStatusBaseSelectProcessor;

class CompositeBaseSelectProcessor extends \Magento\Catalog\Model\ResourceModel\Product\CompositeBaseSelectProcessor
{
    public function __construct(
        array $baseSelectProcessors
    ) {


        // REMOVE THE STOCK STATUS PROCESSOR
        //...................................
        $finalProcessors = array();
        foreach ($baseSelectProcessors as $baseSelectProcessor) {
            if(!is_a($baseSelectProcessor, StockStatusBaseSelectProcessor::class)) {
                $finalProcessors[] = $baseSelectProcessor;
            }
        }

        parent::__construct($finalProcessors);
    }

}

Any other ideas on how to selectively remove the StockStatusBaseSelectProcessor only when we need to would be greatly appreciated.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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