Magento – Magento2 – include possible tier price in getProductPrice() product list

bundlemagento2priceproduct-listtierprice

Our customer would like to display lowest possible price in product list pages. All visible products in website are bundle products

This is the function which displays the price

Magento\Catalog\Block\Product\ListProduct

public function getProductPrice(\Magento\Catalog\Model\Product $product)
{
    $priceRender = $this->getPriceRender();

    $price = '';
    if ($priceRender) {
        $price = $priceRender->render(
            \Magento\Catalog\Pricing\Price\FinalPrice::PRICE_CODE,
            $product,
            [
                'include_container' => true,
                'display_minimal_price' => true,
                'zone' => \Magento\Framework\Pricing\Render::ZONE_ITEM_LIST,
                'list_category_page' => true
            ]
        );
    }

    return $price;
}

The fast try (changing \Magento\Catalog\Pricing\Price\FinalPrice::PRICE_CODE to \Magento\Catalog\Pricing\Price\TierPrice::PRICE_CODE) obviously doesn't work, as TierPrice should return an array or some complex data and not a price amount

How to achieve this? Any tips?

update

Missed to comment that products in catalog are bundle

Best Answer

 'display_minimal_price' => true

Already forces the lowest price to be displayed in your list (as a link), you don't have to change the price code, if tierprice is the lowest possible price.

Example:

enter image description here


If you only want the minimal price to be displayed in your list, you have to create a new Magento\Catalog\templates\product\price\final_price.phtml in your custom theme and only display the final_price if

!$block->hasSpecialPrice()) and !$block->showMinimalPrice()


Edit: possible solution for bundled products/ just an idea, no time at the moment to check it but maybe it helps you

  1. Define a new custom price renderer in your di.xml, so you will only change the behavior in list and don't change bundle product pricing core function

    <virtualType name="Magento\Bundle\Pricing\Price\Pool" type="Magento\Framework\Pricing\Price\Pool">
    <arguments>
        <argument name="prices" xsi:type="array">
            <item name="bundle_lowest_price" xsi:type="string">Company\CustomPrices\Pricing\Price\BundleLowestPrice</item>
        </argument>
    </arguments>
    

  2. Define render template for your price renderer in catalog_product_prices.xml

    <referenceBlock name="render.product.prices"> <arguments> <argument name="bundle" xsi:type="array"> <item name="prices" xsi:type="array"> <item name="bundle_lowest_price" xsi:type="array"> <item name="render_template" xsi:type="string">Company_CustomerPrices::bundle_lowest_price.phtml</item> </item> </item> </argument> </arguments> </referenceBlock> >

  3. In your BundleLowestPrice.php get the final price for each bundle option (as simpleproduct) and compare them, return the lowest possible price to your bundle/final_price.phtml

4.final_price.phtml:

 $bundleLowestPriceModel= $block->getPriceType('bundle_lowest_price');
 $lowestPrice = $bundleLowestPriceModel->getLowestPrice();
 ...........
 if ($lowestPrice < $minimalPrice < $minimalRegularPrice)
 .... output lowestPrice
Related Topic