cancel
Showing results for 
Search instead for 
Did you mean: 

Show applied cart rule on each product in cart/minicart

SOLVED

Show applied cart rule on each product in cart/minicart

I have a cart rule that goes like this: if you have an item from category A and an item from category B, the item from B gets a 50% discount.
 
So the way it works now is, the discount is displayed in 'Totals' section only.
screenshot.jpg
 
I would like to let the customers know which particular item is getting discounted
e.g. with crossed-out price & new price next to (with the label of the rule that was applied next to it if possible).
 
How can i do this? I would appreciate your help as i'm a beginner and i have no idea where to even begin.
 
Thanks
 
 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions

Re: Show applied cart rule on each product in cart/minicart

Hello @stojanovicef91 

 

Please follow the steps below to show the applied cart rules on each product in the cart/minicart in Magento 2:

  • Please create a custom module or use an existing one to override the vendor/magento/module-checkout/view/frontend/templates/cart/item/default.phtml template file.
  • Please copy the default.phtml file to your module's template directory, which should be located at app/code/[Vendor]/[Module]/view/frontend/templates/cart/item/default.phtml.
  • Please find the following code in the default.phtml file:
    <span class="price">
        <?= /* @escapeNotVerified */ $block->getItemPrice($_item) ?>
    </span>
  • Please replace it with the following code to display the applied cart rules for the item:
    <?php
    $quote = $block->getQuote();
    $cartRules = $quote->getAppliedCartRules($_item);
    ?>
    <span class="price">
        <?= /* @escapeNotVerified */ $block->getItemPrice($_item) ?>
    </span>
    <?php if (!empty($cartRules)): ?>
        <div class="applied-rules">
            <ul>
                <?php foreach ($cartRules as $rule): ?>
                    <li><?= $rule->getName() ?></li>
                <?php endforeach; ?>
            </ul>
        </div>
    <?php endif; ?>
  • Please save the file and clear the Magento cache.

This code retrieves the quote object, which contains the applied cart rules, and displays them in a list below the item price. If there are no cart rules applied to the item, the list will not be displayed.

 

If you find our reply helpful, please give us kudos.

 

A Leading Magento Development Agency That Delivers Powerful Results, Innovation, and Secure Digital Transformation.

 

WebDesk Solution Support Team

Get a Free Quote | | Adobe Commerce Partner | Hire Us | Call Us 877.536.3789

Thank You,


WebDesk Solution Support Team
Get a Free Quote | Email | Adobe Commerce Partner | Hire Us | Call Us 877.536.3789


Location: 150 King St. W. Toronto, ON M5H 1J9

View solution in original post

1 REPLY 1

Re: Show applied cart rule on each product in cart/minicart

Hello @stojanovicef91 

 

Please follow the steps below to show the applied cart rules on each product in the cart/minicart in Magento 2:

  • Please create a custom module or use an existing one to override the vendor/magento/module-checkout/view/frontend/templates/cart/item/default.phtml template file.
  • Please copy the default.phtml file to your module's template directory, which should be located at app/code/[Vendor]/[Module]/view/frontend/templates/cart/item/default.phtml.
  • Please find the following code in the default.phtml file:
    <span class="price">
        <?= /* @escapeNotVerified */ $block->getItemPrice($_item) ?>
    </span>
  • Please replace it with the following code to display the applied cart rules for the item:
    <?php
    $quote = $block->getQuote();
    $cartRules = $quote->getAppliedCartRules($_item);
    ?>
    <span class="price">
        <?= /* @escapeNotVerified */ $block->getItemPrice($_item) ?>
    </span>
    <?php if (!empty($cartRules)): ?>
        <div class="applied-rules">
            <ul>
                <?php foreach ($cartRules as $rule): ?>
                    <li><?= $rule->getName() ?></li>
                <?php endforeach; ?>
            </ul>
        </div>
    <?php endif; ?>
  • Please save the file and clear the Magento cache.

This code retrieves the quote object, which contains the applied cart rules, and displays them in a list below the item price. If there are no cart rules applied to the item, the list will not be displayed.

 

If you find our reply helpful, please give us kudos.

 

A Leading Magento Development Agency That Delivers Powerful Results, Innovation, and Secure Digital Transformation.

 

WebDesk Solution Support Team

Get a Free Quote | | Adobe Commerce Partner | Hire Us | Call Us 877.536.3789

Thank You,


WebDesk Solution Support Team
Get a Free Quote | Email | Adobe Commerce Partner | Hire Us | Call Us 877.536.3789


Location: 150 King St. W. Toronto, ON M5H 1J9