I am creating order pro-grammatically.

I have referred below code


My requirement is to set discount amount for each cart item.

The discount amount is 5. This can be varied based the product id which i am fetching from external api

I tried below code for that.


after the line


It is not working. Can anyone help me to achieve this functionality.

Thanks for your help


As from Amit suggestion i used below code.


<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Sales:etc/sales.xsd">
<section name="quote">
    <group name="totals">
        <item name="testdiscount" instance="Vendor\Module\Model\Quote\Discount" sort_order="500"/>


namespace X247commerce\CustomerService\Model\Quote;

 class Discount extends 
 protected $_priceCurrency;

  public function __construct(
   \Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency
   $this->_priceCurrency = $priceCurrency;

public function collect(
   \Magento\Quote\Model\Quote $quote,
   \Magento\Quote\Api\Data\ShippingAssignmentInterface $shippingAssignment,
   \Magento\Quote\Model\Quote\Address\Total $total
   parent::collect($quote, $shippingAssignment, $total);
       $baseDiscount = 10;
       $discount =  $this->_priceCurrency->convert($baseDiscount);
       $total->addTotalAmount('customdiscount', -$discount);
       $total->addBaseTotalAmount('customdiscount', -$baseDiscount);
       $total->setBaseGrandTotal($total->getBaseGrandTotal() - $baseDiscount);
       $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
   return $this;


1 Answer 1


You cannot give discount amount using a simple setter function setDiscountAmount().

At Magento,discount amount calculation on Cart Price rule. So, it means you have to applied coupon on to your code /apply Cart price rules programmatically to this Quote object.

Create a Cart Price rule at admin and apply coupon code at quote programmatically to this Quote object.



If you want to apply the custom discount as per as your desire then you have to implement custom discount , and this discount does not depend on Magento cart price rules.

Check the below aricles, how you can implement custom discount

https://www.mageplaza.com/devdocs/how-add-custom-discount-magento-2.html https://www.magestore.com/magento-2-tutorial/how-to-add-magento-2-custom-discount/ Magento 2 - How to add custom discount in cart programmatically?

  • I don't like to create coupon code. can we set discount amount to the total, that value will be varied based on the api I am calling.
    – Manjunath
    Commented Jul 9, 2019 at 12:53
  • The value 5 can be varied because the amount value i am reading from external api, how to set discount amount to total in that way
    – Manjunath
    Commented Jul 9, 2019 at 12:57
  • Each and every time i can't create the coupon code to set, so is there anyway to set discount amount only please
    – Manjunath
    Commented Jul 9, 2019 at 13:08
  • hello @Amit, I still have some questions, I need to set custom discount amount for each cart item not for whole cart. in that case how can i use those snippets Also will it show in admin sales order grid, that custom discount
    – Manjunath
    Commented Jul 9, 2019 at 13:33
  • You will get quote object at collect( from ` $quote` ,So you can get quote items and calculate the total.
    – Amit Bera
    Commented Jul 9, 2019 at 13:37

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.