I'm creating an order programmatically. I have added cart price rule in backend for subtotal above 1000 will get 30% product price discount on product

  1. When generating order(programmatically) as a customer, Discount is not added in order totals in the backend as shown in the screenshot:- https://nimb.ws/YD5Exj
  1. When generating an order on the frontend with the same customer, the discount is added to the cart can also see in the order totals in the backend as shown in the screenshot:- https://nimb.ws/uwVET1

How can I add discounts while generating order programmatically?


     * @return void
    public function execute() 
        $firstName = "test1";
        $lastName = "test12345";
        $regionId = "285";
        $region = "Brasov";
        $postcode = "50001";
        $street = "Test Street";
        $city = "Acris";
        $countryId = "RO";
        $telephone = "0555555555";
        $fax = "";
        $lockerId = ""; 
        $courierId = "";
        $courierFixedLocationId = ""; 
        $shippingMethod = "flatrateone_flatrate";
        $paymentMethod = "cashondelivery";
        $nextOrderDate = "";

        $store = $this->storeManager->getStore();
        $storeId = $store->getStoreId();
        $websiteId = $this->storeManager->getStore()->getWebsiteId();
        $quote = $this->quote->create();

        $customerId = 228386;
        $customer = $this->customerRepository->getById($customerId);
        $allskus = 'BFSITE03';
        $allskus = explode(",", $allskus);

        foreach($allskus as $sku){
            $qty = 1;
            $product = $this->productRepository->get($sku););

            $params = new DataObject([
                'product' => $productId,
                'qty' => $qty
            $quote->addProduct($product, $params);

        // Shipping Address to quote

        // Billing Address to quote

        // Set Shipping Method
        $shippingAddress = $quote->getShippingAddress();
        $quote->getPayment()->importData(['method' => $paymentMethod]);

        // Create a order from quote
        $order = $this->quoteManagement->submit($quote);
        $orderId = $order->getIncrementId();

            $result['success'] = $orderId;
        } else {
            $result = [ 'error' => true,'msg' => 'Error occurs for Order placed'];

Note:- All classes are mentioned in the construct so don't worry about it.

I have tried following way, but I'm getting in $quote->getAppliedRuleIds().

if($quote->getAppliedRuleIds()) {
    echo "rule applied";
} else {
    echo " no rule applied";

I'm having the same issue with Shipping charges as well on the front it's showing 0 but while generating an order(programmatically) shipping charges are calculated(14.9).

Let me know If anyone has any idea regarding this.


Discount is working after some changes, but flat rate shipping is not calculated. While placing an order from the front end shipping method is calculated but programmatically it's not calculating. I have updated the code above.

Other shipping method is working fine but for flatrate_flatrateone shipping method is showing an error as shown in the screenshot:- Screenshot

I have debugged the code and found out the issue is in $order = $this->quoteManagement->submit($quote); so i think there is some issue with quote object.

  • Did you try to add the coupon code programmatically? I don't see adding it to the code. \Magento\Quote\Api\CouponManagementInterface::set
    – Dan Staver
    Commented Feb 27, 2022 at 5:37
  • No, I have not added a coupon code prog. . I'm adding a discount without a coupon code. If you could provide a reference code I can look for it.
    – Mohit Rane
    Commented Feb 28, 2022 at 6:39

2 Answers 2


Can you please check the shipping method code as it is carrier.code + method.code i.e carrier.code = "flaterate" method.code = "flaterate" then it would be flaterate_flaterate


It was a silly mistake by me I thought I was using the correct code for the shipping method, but it was different. My collegue suggested me to look in quote_shipping_rate table i realised that shipping code i'm using is wrong. so correct one is

$shippingMethod = "flatrateone_flatrate";

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.