1

I am creating order pro-grammatically.

I have referred below code

https://www.rakeshjesadiya.com/how-to-create-order-programmatically-in-magento-2/

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.

 $quote->setDiscountAmount(5);

after the line

$quote->addProduct($product,$buyRequest);

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

Thanks for your help

Update

As from Amit suggestion i used below code.

Vendor\Module\etc\sales.xml

<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"/>
    </group>
</section>

Vendor\Module\Model\Quote\Discount.php

namespace X247commerce\CustomerService\Model\Quote;

 class Discount extends 
 \Magento\Quote\Model\Quote\Address\Total\AbstractTotal
 {   
 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);
       $quote->setCustomDiscount(-$discount);
       $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
       $objectManager->get('Psr\Log\LoggerInterface')->info('discount'.$discount);      
      $objectManager->get('Psr\Log\LoggerInterface')->info(json_encode($quote->getData()));
   return $this;
  }

}

0

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.

$quote->setCouponCode($couponCode)->collectTotals()->save();

Update

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 Jul 9 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 Jul 9 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 Jul 9 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 Jul 9 at 13:33
  • You will get quote object at collect( from ` $quote` ,So you can get quote items and calculate the total. – Amit Bera Jul 9 at 13:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.