0

I need to have the discounted, base, and percentage off prices in the summary checkout. I found the file that prints and it is this checkout/summary/item/price/row_excl_tax.html but I can't retrieve the base price or even calculate the discount percentage on the item

Location

1 Answer 1

0

In Magento 2, you can add the base price, discounted price, and percentage discount to the summary checkout by modifying the template files and creating a new block class. Here is an example of how you can do this:

  1. Override the "checkout_cart_item_renderers.xml" layout file in your custom module. You can create a new directory called "Magento_Checkout" in the "view/frontend" directory of your custom module and create a new file called "layout/checkout_cart_item_renderers.xml" within it.

  2. In the "checkout_cart_item_renderers.xml" file, add the following code to add a new block to the summary checkout template:

Here is the code:-

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceBlock name="checkout.cart.item.renderers">
            <block class="YourVendor\YourModule\Block\Checkout\Discount" name="checkout.cart.item.renderers.discount" template="YourVendor_YourModule::checkout/discount.phtml" after="checkout.cart.item.renderers.name"/>
        </referenceBlock>
    </body>
</page>
  1. Create a new block class called "Discount.php" in the "Block/Checkout" directory of your custom module.

  2. In the "Discount.php" file, add the following code to define your new block class and retrieve the base price, discounted price, and percentage discount:

Here is the code:-

<?php

namespace YourVendor\YourModule\Block\Checkout;

use Magento\Framework\View\Element\Template;

class Discount extends Template
{
    public function getDiscount()
    {
        $item = $this->getItem();
        $basePrice = $item->getPrice();
        $discountedPrice = $item->getDiscountAmount();
        $discountPercent = 0;
        if ($basePrice > 0) {
            $discountPercent = ($discountedPrice / $basePrice) * 100;
        }
        return [
            'base_price' => $basePrice,
            'discounted_price' => $discountedPrice,
            'discount_percent' => $discountPercent
        ];
    }
}
  1. Create a new template file called "discount.phtml" in the "view/frontend/templates/checkout" directory of your custom module.

  2. In the "discount.phtml" file, add the following code to display the base price, discounted price, and percentage discount in the summary checkout:

Here is the code:-

<?php
$discount = $block->getDiscount();
$basePrice = $discount['base_price'];
$discountedPrice = $discount['discounted_price'];
$discountPercent = $discount['discount_percent'];
?>
<div class="discount-info">
    <span>Base Price: <?php echo $basePrice ?></span>
    <span>Discounted Price: <?php echo $discountedPrice ?></span>
    <span>Discount Percent: <?php echo $discountPercent ?></span>
</div>

Let me know if any query.

THANKS.

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.