How to create order for bundle product with rest api Magento2 ?
I create an order using Rest Api for a simple product, but for a bundle product it does not work, any example can help me, thanks.
For this, I use this link
https://devdocs.magento.com/redoc/2.3/admin-rest-api.html#operation/salesOrderRepositoryV1SavePut
/rest/V1/orders/create
"entity"=>
[
"base_currency_code"=>"USD",
"base_discount_amount"=> 0,
"base_grand_total"=> 120,
"base_shipping_amount"=> 0,
"base_subtotal"=> 120,
"base_tax_amount"=>0,
"customer_email"=> "[email protected]",
"customer_firstname"=> "test",
"customer_group_id"=> 1,
"customer_id"=> 134,
"customer_is_guest"=> 0,
"customer_lastname"=> "test",
"customer_note_notify"=> 1,
"discount_amount"=> 0,
"email_sent"=> 1,
"coupon_code"=> "Test1",
"discount_description"=> "Test1",
"grand_total"=> 120,
"is_virtual"=> 0,
"order_currency_code"=> "USD",
"shipping_amount"=> 0,
"shipping_description"=> "Flat Rate - Fixed",
"state"=> "new",
"status"=> "pending",
"store_currency_code"=> "USD",
"store_id"=> 1,
"store_name"=> "Main Website\nMain Website Store\n",
"subtotal"=> 120,
"subtotal_incl_tax"=> 120,
"tax_amount"=> 0,
"total_item_count"=> 1,
"total_qty_ordered"=> 1,
"weight"=> 0,
"items" => [ [
"base_discount_amount"=> 0,
"base_original_price"=> 0,
"base_price"=> 0,
"base_price_incl_tax"=> 0,
"base_row_invoiced"=> 0,
"base_row_total"=> 0,
"base_tax_amount"=> 0,
"base_tax_invoiced"=> 0,
"discount_amount"=> 0,
"discount_percent"=> 0,
"free_shipping"=> 0,
"is_virtual"=> 0,
"name"=> "Mixte tandem",
"original_price"=> 0,
"price"=> 0,
"price_incl_tax"=> 0,
"product_id"=> 8,
"product_type"=> "bundle",
"qty_ordered"=> 1,
"row_total"=> 0,
"row_total_incl_tax"=> 0,
"sku"=> "Mixte Tandem",
"store_id"=> 1,
"product_option"=> [
"extension_attributes"=> [
"bundle_options"=> [
[
"option_id"=> 3,
"option_qty"=> 3,
"option_selections"=> ["12","15","16"]
], [
"option_id"=> 4,
"option_qty"=> 2,
"option_selections"=> ["13","14"]
]
],
]
],
] ],
"billing_address"=> [
"address_type"=> "billing",
"city"=> "Gyumri",
"company"=> "Shooga",
"country_id"=> "AM",
"email"=> "[email protected]",
"firstname"=> "test",
"lastname"=> "test",
"postcode"=> "30332",
"region"=> "Shirak",
"region_code"=> "SK",
"region_id"=> 19,
"street"=> [
"Street 1",
"Street 2"
],
"telephone"=> "12345688"
],
"payment"=> [
"method"=> "checkmo"
],
"extension_attributes"=> [
"shipping_assignments"=> [
[
"shipping"=> [
"address"=> [
"address_type"=> "shipping",
"city"=> "Gyumri",
"company"=> "Shooga",
"country_id"=> "AM",
"email"=> "[email protected]",
"firstname"=> "test",
"lastname"=> "test",
"postcode"=> "30332",
"region"=> "Shirak",
"region_code"=> "SK",
"region_id"=> 19,
"street"=> [
"Street 1",
"Street 2"
],
"telephone"=> "12345688"
],
"method"=> "flatrate_flatrate"
]
]
]
]
]
Now i have it in admin
but i need like this
-
explain "does not work" show us your payload and to what endpoint you are sending itPhilipp Sander– Philipp Sander2019年05月13日 09:55:53 +00:00Commented May 13, 2019 at 9:55
-
Philipp Sander, i updated my questionRobinio– Robinio2019年05月13日 10:05:35 +00:00Commented May 13, 2019 at 10:05
-
I need to show selected products tooRobinio– Robinio2019年05月13日 10:14:16 +00:00Commented May 13, 2019 at 10:14
-
but you are only sending 1 product...why do you expect more to show upPhilipp Sander– Philipp Sander2019年05月13日 10:52:40 +00:00Commented May 13, 2019 at 10:52
-
then what's the point of bundle_options in this code?Robinio– Robinio2019年05月13日 10:54:33 +00:00Commented May 13, 2019 at 10:54
2 Answers 2
@Bharath, I don't know it will help you , but it works for me
namespace venodor\Module\Block;
use Magento\Tax\Model\TaxClass\Source\Product as ProductTaxClassSource;
class CreateCheckout extends \Magento\Framework\View\Element\Template
{
protected $orders;
protected $customerRepository;
protected $_quoteRepository;
protected $_customerRepositoryInterface;
protected $_storeManager;
protected $orderRepository;
protected $_customerFactory;
public function __construct(
\Magento\Framework\View\Element\Template\Context $context,
\Magento\Quote\Api\CartRepositoryInterface $cartRepositoryInterface,
\Magento\Quote\Model\QuoteManagement $quoteManagement,
\Magento\Store\Model\StoreManagerInterface $storeManager,
\Magento\Quote\Model\QuoteRepository $quoteRepository,
\Magento\Customer\Model\CustomerFactory $customerFactory,
\Magento\Customer\Api\CustomerRepositoryInterface $customerRepositoryInterface,
\Magento\Customer\Api\CustomerRepositoryInterfaceFactory $customerRepositoryFactory,
\Magento\Customer\Model\ResourceModel\Customer\CollectionFactory $customerFactory,
\Magento\Quote\Model\QuoteFactory $quoteFactory,
\Magento\Sales\Api\OrderRepositoryInterface $orderRepository,
array $data = []
)
{
$this->cartRepositoryInterface = $cartRepositoryInterface;
$this->quoteManagement = $quoteManagement;
$this->quoteFactory = $quoteFactory;
$this->_quoteRepository = $quoteRepository;
$this->customerFactory = $customerFactory;
$this->_storeManager = $storeManager;
$this->_customerRepositoryInterface = $customerRepositoryInterface;
$this->customerRepository = $customerRepositoryFactory->create();
$this->orderRepository = $orderRepository;
parent::__construct($context, $data);
}
public function getCustomerCollection()
{
return $this->_customerFactory->create();
}
public function createOrder(){
try {
$store = $this->_storeManager->getStore();
$websiteId = $this->_storeManager->getStore()->getWebsiteId();
$customer = $this->customerFactory->create();
$customer->setWebsiteId($websiteId);
$customerCollection = $this->getCustomerCollection();
foreach ($customerCollection as $customerCollection) {
$customerId = $customerCollection->getId();
$customerData = $this->customerRepository->getById($customerId);
$getEmailCustomers = $customerData->getEmail();
$customer->loadByEmail($getEmailCustomers);
$checkCartExist = $this->quoteFactory->create()->loadByCustomer($customerId);
$checkCartExist->getAllVisibleItems();
if ($checkCartExist['entity_id']) {
$quote = $this->cartRepositoryInterface->getForCustomer($customerId);
$assignCustomer= $this->_customerRepositoryInterface->getById($customer->getEntityId());
$quote->setCurrency();
$quote->assignCustomer($assignCustomer);
$shippingAddress = $quote->getShippingAddress();
$shippingAddress->setCollectShippingRates(true)->collectShippingRates()->setShippingMethod('flatrate_flatrate');
$quote->setPaymentMethod('checkmo'); //payment method
$quote->setInventoryProcessed(false); //not effetc inventory
$quote->save(); //Now Save quote and your quote is ready
$this->_quoteRepository->save($quote);
$quote->getPayment()->importData(['method' => 'checkmo']);
$quote->collectTotals();
$order = $this->quoteManagement->submit($quote);
}
if (isset($order)) {
$order->setEmailSent(0);
$increment_id = $order->getRealOrderId();
$order = $this->orderRepository->get($order->getId());
}
}
} catch (\Exception $e) {
echo $e->getMessage();
}
}
}
-
Thank you..but I don't see that you were setting bundle product or its selection items to quote which is the actual problem, can you please add that. Appreciate your help for thisBharath– Bharath2019年08月28日 19:53:17 +00:00Commented Aug 28, 2019 at 19:53
-
@Bharath Do you need to add a bundle product to the cart?Robinio– Robinio2019年08月29日 12:17:32 +00:00Commented Aug 29, 2019 at 12:17
-
Thank you so much for your help..!Bharath– Bharath2019年08月30日 09:45:50 +00:00Commented Aug 30, 2019 at 9:45
@Bharath this step added to the cart, the former was how I created the order.
You must add to this code in the foreach where you can take the product id and the product qty of the place these $products_id = $jsonData['product_id']; $products_count = $jsonData['product_count'];
I hope this helps you
$products_id = $jsonData['product_id'];
$products_count = $jsonData['product_count'];
$_product = $this->_productRepository->getById($products_id);
$options = $objectManager->get('Magento\Bundle\Model\Option')
->getResourceCollection()
->setProductIdFilter($products_id)
->setPositionOrder();
$options->joinValues($store_id);
$typeInstance = $objectManager->get('Magento\Bundle\Model\Product\Type');
$options = $_product->getOptions($products_id);
$selections = $typeInstance->getSelectionsCollection($typeInstance->getOptionsIds($_product), $_product);
$selectionProds = array_reduce($selections->getData(), function ($carry, $selection) {
if (!isset($carry[$selection['option_id']])) {
$carry[$selection['option_id']] = [];
}
$carry[$selection['option_id']][] = $selection['selection_id'];
return $carry;
}, []);
$selectionQty = array_reduce($selections->getData(), function ($carry, $selection) {
if (!isset($carry[$selection['option_id']])) {
$carry[$selection['option_id']] = [];
}
$carry[$selection['option_id']][] = $selection['selection_qty'];
return $carry;
}, []);
$data = [
'qty' => $products_count,
'product' => $products_id,
'bundle_option' => $selectionProds
];
$checkCartExist = $this->quoteFactory->create()->loadByCustomer($customerId);
$checkCartExist->getAllVisibleItems();
$customerData = $this->customerRepository->getById($customerId);
$getEmailCustomers = $customerData->getEmail();
$request->setData($data);
$customer = $this->customerFactory->create();
$customer->setWebsiteId($websiteId);
$customer->loadByEmail($getEmailCustomers);
if (!$checkCartExist['entity_id']) {
$cartId = $this->cartManagementInterface->createEmptyCart(); //Create empty cart
$quote = $this->cartRepositoryInterface->get($cartId); // load empty cart quote
} else {
$quote = $this->cartRepositoryInterface->getForCustomer($customerId);
}
$quote->setStore($store);
$customer = $this->_customerRepositoryInterface->getById($customer->getEntityId());
$quote->setCurrency();
$quote->assignCustomer($customer);
$quote->addProduct($_product, $request);
$quote->collectTotals();
$quote->save(); //Now Save quote and your quote is ready
$this->_quoteRepository->save($quote);
$this->_sendemail->sendTransactionalEmail($getEmailCustomers);
And all the classes that I injected . here, there may be superfluous classes you may not need, you can simply remove them
public function __construct(
\Magento\Framework\View\Element\Template\Context $context,
\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,
\Magento\Store\Model\StoreManagerInterface $storeManager,
\Magento\Directory\Model\Currency $currency,
\Magento\Catalog\Model\ProductRepository $productRepository,
\Magento\Checkout\Model\Cart $cart,
\Magento\Quote\Api\CartRepositoryInterface $cartRepositoryInterface,
\Magento\Quote\Api\CartManagementInterface $cartManagementInterface,
\Magento\Customer\Model\CustomerFactory $customerFactory,
\Magento\Quote\Model\QuoteManagement $quoteManagement,
\Magento\Quote\Model\QuoteRepository $quoteRepository,
\Magento\Customer\Api\CustomerRepositoryInterface $customerRepositoryInterface,
\Magento\Customer\Api\CustomerRepositoryInterfaceFactory $customerRepositoryFactory,
\Magento\Quote\Model\QuoteFactory $quoteFactory,
\Magento\Customer\Model\AddressFactory $addressFactory,
\AutoAddtocart\BundleAddtocart\Helper\Data $sendEmail,
array $data = []
)
{
$this->_productCollectionFactory = $productCollectionFactory;
$this->_storeManager = $storeManager;
$this->_currency = $currency;
$this->_productRepository = $productRepository;
$this->_quoteRepository = $quoteRepository;
$this->_cart = $cart;
$this->cartRepositoryInterface = $cartRepositoryInterface;
$this->cartManagementInterface = $cartManagementInterface;
$this->customerFactory = $customerFactory;
$this->quoteManagement = $quoteManagement;
$this->_customerRepositoryInterface = $customerRepositoryInterface;
$this->quoteFactory = $quoteFactory;
$this->customerRepository = $customerRepositoryFactory->create();
$this->_addressFactory = $addressFactory;
$this->_sendemail = $sendEmail;
parent::__construct($context, $data);
}
Explore related questions
See similar questions with these tags.