1

I need to get applied coupon details in cart form.phtml

/app/design/frontend/Vendor/Theme/Magento_Checkout/templates/cart

In this path we have shopping cart files are there like coupon.html, form.html

In a coupon.html file we have code to get coupon

 $block->escapeHtml($block->getCouponCode()) 

How to get this coupon detail in other phtml file form.phtml

3
  • Provide detail on this provide the full path of file where & What you want
    – Brad Hodge
    Commented Sep 27, 2016 at 12:11
  • @AnkitJaiswal Please check my updated question Commented Sep 27, 2016 at 12:18
  • please let me know if you have any issue. Commented Sep 27, 2016 at 12:45

3 Answers 3

3

All you need to do is add

<?php

    $quote = $block->getQuote();
    $couponCode = $quote->getCouponCode();

    // to get discount amount, we need to loop the quote items
    $discountAmount = 0;
    foreach($quote->getAllItems() as $item){
       if($discount = $item->getDiscountAmount()){
           $discountAmount += $discount;
       }
    }

    // we can also get the discount more simply
    $discountAmount = $quote->getSubtotal() - $quote->getSubtotalWithDiscount();

?>

to form.phtml.

Form.phtml uses the class \Magento\Checkout\Block\Cart for it's $block variable. \Magento\Checkout\Block\Cart extends \Magento\Quote\Model\Quote\AbstractCart which has a getQuote() method.

With the quote in hand, all that's left is to get the coupon code and whatever other info you need.

1

Rakesh Code Provided will Give you Coupon Code which you are applied to cart Page what else you required please specify will accordingly on that meanwhile try the:

$couponappiledToCartPage = $block->getLayout()->createBlock('Magento\Checkout\Block\Cart\Coupon');
print_r($couponappiledToCartPage->getCouponCode());
0

you need to define below method inside your form.phtml file,

$getCoupon = $block->getLayout()->createBlock('Magento\Checkout\Block\Cart\Coupon');
echo $getCoupon->getCouponCode();
4
  • I need to get coupon amount Commented Sep 28, 2016 at 5:33
  • you need to fetch quote object and may be its getDiscountAmount() is worked for your case Commented Sep 28, 2016 at 5:44
  • Please explain how to get discount amount in same page Commented Sep 28, 2016 at 10:27
  • How to get available coupon code on cart page which is valid
    – Mahendra
    Commented Apr 5, 2019 at 10:03

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.