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
- 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
- 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:-
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.
\Magento\Quote\Api\CouponManagementInterface::set