我正在使用magento 2.1.5,而我实际创建订单比仅创建1个产品我需要按顺序保存两个产品。
我调试订单项的foreach循环,我得到两个产品都正确加载。
$orderData =[
'currency_id' => 'USD',
'email' => 'test@example.com',
'shipping_address' =>[
'firstname' => 'Fake', //address Details
'lastname' => 'Name',
'street' => 'xxxxx',
'city' => 'xxxxx',
'country_id' => 'IN',
'region' => 'xxx',
'postcode' => '43244',
'telephone' => '52332',
'fax' => '32423',
'save_in_address_book' => 1
],
'items'=> [
['product_id'=>'1','qty'=>1],
['product_id'=>'2','qty'=>1]
]
];
<?php
namespace Icecube\Measurements\Helper;
class Data extends \Magento\Framework\App\Helper\AbstractHelper
{
/**
* @param Magento\Framework\App\Helper\Context $context
* @param Magento\Store\Model\StoreManagerInterface $storeManager
* @param Magento\Catalog\Model\Product $product
* @param Magento\Framework\Data\Form\FormKey $formKey $formkey,
* @param Magento\Quote\Model\Quote $quote,
* @param Magento\Customer\Model\CustomerFactory $customerFactory,
* @param Magento\Sales\Model\Service\OrderService $orderService,
*/
public function __construct(
\Magento\Framework\App\Helper\Context $context,
\Magento\Store\Model\StoreManagerInterface $storeManager,
\Magento\Catalog\Model\ProductFactory $product,
\Magento\Framework\Data\Form\FormKey $formkey,
\Magento\Quote\Model\QuoteFactory $quote,
\Magento\Quote\Model\QuoteManagement $quoteManagement,
\Magento\Customer\Model\CustomerFactory $customerFactory,
\Magento\Customer\Api\CustomerRepositoryInterface $customerRepository,
\Magento\Sales\Model\Service\OrderService $orderService,
\Magento\Sales\Api\OrderRepositoryInterface $orderRepository,
\Magento\Sales\Model\Service\InvoiceService $invoiceService,
\Magento\Framework\DB\Transaction $transaction,
\Magento\Sales\Api\Data\OrderInterface $order,
\Magento\Framework\ObjectManagerInterface $objectmanager,
\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productFactory,
\Magento\Quote\Api\CartRepositoryInterface $cartRepositoryInterface,
\Magento\Quote\Api\CartManagementInterface $cartManagementInterface,
\Magento\Quote\Model\Quote\Address\Rate $shippingRate
) {
$this->_storeManager = $storeManager;
$this->_productFactory = $product;
$this->_formkey = $formkey;
$this->quote = $quote;
$this->quoteManagement = $quoteManagement;
$this->customerFactory = $customerFactory;
$this->customerRepository = $customerRepository;
$this->orderService = $orderService;
$this->_orderRepository = $orderRepository;
$this->_invoiceService = $invoiceService;
$this->_transaction = $transaction;
$this->order = $order;
$this->_objectManager = $objectmanager;
$this->productFactory = $productFactory;
$this->cartRepositoryInterface = $cartRepositoryInterface;
$this->cartManagementInterface = $cartManagementInterface;
$this->shippingRate = $shippingRate;
parent::__construct($context);
}
public function loadProduct() {
$collection = NULL;
return $collection = $this->productFactory->create()
->addAttributeToSelect('*')
->load();
}
/**
* Create Order On Your Store
*
* @param array $orderData
* @return array
*
*/
public function createOrderNew($orderData) {
//init the store id and website id @todo pass from array
$store = $this->_storeManager->getStore();
$websiteId = $this->_storeManager->getStore()->getWebsiteId();
//init the customer
$customer=$this->customerFactory->create();
$customer->setWebsiteId($websiteId);
$customer->loadByEmail($orderData['email']);// load customet by email address
//check the customer
if(!$customer->getEntityId()){
//If not avilable then create this customer
$customer->setWebsiteId($websiteId)
->setStore($store)
->setFirstname($orderData['shipping_address']['firstname'])
->setLastname($orderData['shipping_address']['lastname'])
->setEmail($orderData['email'])
->setPassword($orderData['email']);
$customer->save();
}
//init the quote
$cart_id = $this->cartManagementInterface->createEmptyCart();
$cart = $this->cartRepositoryInterface->get($cart_id);
$cart->setStore($store);
// if you have already buyer id then you can load customer directly
$customer= $this->customerRepository->getById($customer->getEntityId());
$cart->setCurrency();
$cart->assignCustomer($customer); //Assign quote to customer
//add items in quote
foreach($orderData['items'] as $item){
$product = $this->_productFactory->create()->load($item['product_id']);
$cart->addProduct(
$product,
intval($item['qty'])
);
}
//Set Address to quote @todo add section in order data for seperate billing and handle it
$cart->getBillingAddress()->addData($orderData['shipping_address']);
$cart->getShippingAddress()->addData($orderData['shipping_address']);
// Collect Rates and Set Shipping & Payment Method
$this->shippingRate
->setCode('freeshipping_freeshipping')
->getPrice(1);
$shippingAddress = $cart->getShippingAddress();
//@todo set in order data
$shippingAddress->setCollectShippingRates(true)
->collectShippingRates()
->setShippingMethod('flatrate_flatrate'); //shipping method
//$cart->getShippingAddress()->addShippingRate($this->rate);
$cart->setPaymentMethod('checkmo'); //payment method
//@todo insert a variable to affect the invetory
$cart->setInventoryProcessed(false);
// Set sales order payment
$cart->getPayment()->importData(['method' => 'checkmo']);
// Collect total and saeve
$cart->collectTotals();
// Submit the quote and create the order
$cart->save();
$cart = $this->cartRepositoryInterface->get($cart->getId());
$order_id = $this->cartManagementInterface->placeOrder($cart->getId());
return $order_id;
}
}
?>