0

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?

OrderCreate.php

    /**
     *
     * @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();
        $quote->setStore($store);

        $customerId = 228386;
        $customer = $this->customerRepository->getById($customerId);
        $quote->setCurrency();
        $quote->assignCustomer($customer);
        
        $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
        $quote->getShippingAddress()->setFirstName($firstName);
        $quote->getShippingAddress()->setLastName($lastName);
        $quote->getShippingAddress()->setRegionId($regionId);
        $quote->getShippingAddress()->setRegion($region);
        $quote->getShippingAddress()->setCity($city); 
        $quote->getShippingAddress()->setCountryId($countryId);
        $quote->getShippingAddress()->setPostcode($postcode);
        $quote->getShippingAddress()->setStreet($street);
        $quote->getShippingAddress()->setTelephone($telephone);
        $quote->getShippingAddress()->setFax($fax);
        $quote->getShippingAddress()->setSaveInAddressBook(1);

        // Billing Address to quote
        $quote->getBillingAddress()->setFirstName($firstName);
        $quote->getBillingAddress()->setLastName($lastName);
        $quote->getBillingAddress()->setRegionId($regionId);
        $quote->getBillingAddress()->setRegion($region);
        $quote->getBillingAddress()->setCity($city); 
        $quote->getBillingAddress()->setCountryId($countryId);
        $quote->getBillingAddress()->setPostcode($postcode);
        $quote->getBillingAddress()->setStreet($street);
        $quote->getBillingAddress()->setTelephone($telephone);
        $quote->getBillingAddress()->setFax($fax);
        $quote->getBillingAddress()->setSaveInAddressBook(1);

        // Set Shipping Method
        $shippingAddress = $quote->getShippingAddress();
        $quote->collectTotals();
        $shippingAddress->setCollectShippingRates(true)
                    ->collectShippingRates()
                    ->setShippingMethod($shippingMethod);
        $quote->setTotalsCollectedFlag(false)->collectTotals();
        $quote->setPaymentMethod($paymentMethod);
        $quote->setInventoryProcessed(false);
        $quote->save();
        $quote->getPayment()->importData(['method' => $paymentMethod]);
        $quote->collectTotals()->save();

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

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

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.

UPDATE

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.

2
  • 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
    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
    Feb 28, 2022 at 6:39

2 Answers 2

1

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

1

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.