Is there any way to get all Applied Discount (for cart rules and coupons) line items in cart/order in Magento 2?

I need to show all cart/order discounts in the following manner:

Subtotal                              ----------- 100.00

CHRISTMAS10 (Christmas Sale 10 % off) ----------- -10.00
CHDLL1242DK (Coupon Redeemed)         ----------- -20.00

Grand Total                           -----------  70.00

Magento 2 store only the coupon_code and the applied_rule_ids, but not the discount amount for specific rule and discount description.

What would the best way to achieve this? (also if there is any module available, please suggest)

Solutions

You can achieve this by using below code:

namespace Vendor\ModuleName\Controller\Index;

use Magento\Sales\Model\ResourceModel\Order\CollectionFactory;
use Magento\SalesRule\Model\RuleFactory;

class YOURClass extends \Magento\Framework\App\Action\Action
{
     /**
     * @var OrderCollection
     */
    private $orderCollection;
    /**
     * @var RuleFactory
     */
    private $rule;

    public function __construct(
        Context $context,
        CollectionFactory $orderCollection,
        RuleFactory $rule,
        \Psr\Log\LoggerInterface $logger
    )
    {
        parent::__construct($context);
        $this->orderCollection = $orderCollection;
        $this->rule = $rule;
        $this->logger = $logger;
    }


    public function execute()
    {
        $collection = $this->orderCollection->create()
            ->addAttributeToSelect('*')
            ->addFieldToFilter('increment_id', '000000005');

        foreach ($collection->getItems() as $order) {

            foreach($order->getAllItems() as $item) {

                $appliedRule = $item->getAppliedRuleIds();

                if (!empty($appliedRule)) {

                    $appliedRuleArr = explode(",", $appliedRule);

                    foreach ($appliedRuleArr as $ruleId) {

                        $couponCodeData  = $this->rule->create()->load($ruleId);

                        $this->logger->log('600', print_r($couponCodeData->getData(), true));

                    }

                }

            }

        }

    }

}

Similar questions

Is there any way to add module block inside any other module's phtml file in magento 2?
I have a layered navigation module inside my Magento 2 custom created theme. Now, I have to use this layered nav module's phtml file to another module just like catalog module's phtml file. Can anyone please tell me how to do this? Thanks in advanced.
Getting an order item unit price, after any discount has been applied in Magento
Is it possible to get the unit price of an item from an order that has had a discount applied to it? For example order number #12345 contains 4 products each with a normal unit price of £3.00. A discount is applied which takes off 25% each item. How can I then access that cost from each order from $order->getAllItems() ?
How to get total amount with discounts (all rules applied) before Order Review?
I have cart rules applied by payment methods. And my custom Credit Card payment send authorization request in step five (before Order Review). But Cart Rules are applied at Order Review only (step 6). I'm trying like below but $amount is wrong (with no discounts). How to get price discounts in this case?
Catalog Price Rules discount applied to Parent Category, not reflecting on Children
Note: I've asked this on StackOverflow, and will update my question there with credit (or delete) if I get an answer here. I have have nested products which are not getting a discount if that product does not have the parent category selected within the product page. For example: Within the Magento Admin, I've created a Catalog Price Rule "If ANY o...
Deleting a large number of Magento coupons (shopping cart price rules) programatically
I've been on here before asking about writing code to get Magento to generate a random coupon code for a new e-mail subscriber - Creating a single random Magento coupon This code worked perfectly and to those who helped I am eternally grateful. I am now looking for a way to delete these coupons as they expire in a bulk fashion because deleting them...
Cart Price Rules: Minimum Purchase of 2 of the same items to get 15% discount
I am trying to set up the following rule in Price Cart Rules: Promotion: 15% off selected products with minimum purchase of 2 items and above. Criteria: No combined purchased allowed, meaning, discount will only be given if two or more of the same item is purchased. Example: This promotion code must work if i purchase 2 or more of SKU1-1. How do i ...

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.