Guest User

Untitled

a guest
Nov 24th, 2017
381
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.22 KB | None | 0 0
  1. <?php
  2. namespace yourmodulenamespaceHelper;
  3. class Create extends MagentoFrameworkAppHelperAbstractHelper
  4. {
  5. /**
  6. * @param MagentoFrameworkAppHelperContext $context
  7. * @param MagentoStoreModelStoreManagerInterface $storeManager
  8. * @param MagentoCatalogModelProduct $product
  9. * @param MagentoFrameworkDataFormFormKey $formKey $formkey,
  10. * @param MagentoQuoteModelQuote $quote,
  11. * @param MagentoCustomerModelCustomerFactory $customerFactory,
  12. * @param MagentoSalesModelServiceOrderService $orderService,
  13. */
  14. public function __construct(
  15. MagentoFrameworkAppHelperContext $context,
  16. MagentoStoreModelStoreManagerInterface $storeManager,
  17. MagentoCatalogModelProduct $product,
  18. MagentoFrameworkDataFormFormKey $formkey,
  19. MagentoQuoteModelQuoteFactory $quote,
  20. MagentoQuoteModelQuoteManagement $quoteManagement,
  21. MagentoCustomerModelCustomerFactory $customerFactory,
  22. MagentoCustomerApiCustomerRepositoryInterface $customerRepository,
  23. MagentoSalesModelServiceOrderService $orderService,
  24. MagentoQuoteModelQuoteAddressRate $shippingRate
  25. ) {
  26. $this->_storeManager = $storeManager;
  27. $this->_product = $product;
  28. $this->_formkey = $formkey;
  29. $this->quote = $quote;
  30. $this->quoteManagement = $quoteManagement;
  31. $this->customerFactory = $customerFactory;
  32. $this->customerRepository = $customerRepository;
  33. $this->orderService = $orderService;
  34. $this->shippingRate = $shippingRate;
  35. parent::__construct($context);
  36. }
  37.  
  38. /**
  39. * Create Order On Your Store
  40. *
  41. * @param array $orderData
  42. * @return array
  43. *
  44. */
  45. public function createMageOrder($orderData) {
  46. $store=$this->_storeManager->getStore();
  47. $websiteId = $this->_storeManager->getStore()->getWebsiteId();
  48. $customer=$this->customerFactory->create();
  49. $customer->setWebsiteId($websiteId);
  50. $customer->loadByEmail($orderData['email']);// load customet by email address
  51. if(!$customer->getEntityId()){
  52. //If not avilable then create this customer
  53. $customer->setWebsiteId($websiteId)
  54. ->setStore($store)
  55. ->setFirstname($orderData['shipping_address']['firstname'])
  56. ->setLastname($orderData['shipping_address']['lastname'])
  57. ->setEmail($orderData['email'])
  58. ->setPassword($orderData['email']);
  59. $customer->save();
  60. }
  61. $quote=$this->quote->create(); //Create object of quote
  62. $quote->setStore($store); //set store for which you create quote
  63. // if you have allready buyer id then you can load customer directly
  64. $customer= $this->customerRepository->getById($customer->getEntityId());
  65. $quote->setCurrency();
  66. $quote->assignCustomer($customer); //Assign quote to customer
  67.  
  68. //add items in quote
  69. foreach($orderData['items'] as $item){
  70. $product=$this->_product->load($item['product_id']);
  71. //$product->setPrice($item['price']);
  72. $product->setWeight(20);
  73. $quote->addProduct(
  74. $product,
  75. intval($item['qty'])
  76. );
  77. }
  78.  
  79. //Set Address to quote
  80. $quote->getBillingAddress()->addData($orderData['shipping_address']);
  81. $quote->getShippingAddress()->addData($orderData['shipping_address']);
  82.  
  83. // Collect Rates and Set Shipping & Payment Method
  84.  
  85. $shippingAddress=$quote->getShippingAddress();
  86.  
  87. /*$shippingAddress->setCollectShippingRates(true)
  88. ->collectShippingRates()
  89. ->setShippingMethod('fedex_FEDEX_GROUND'); //shipping method
  90.  
  91. */
  92. //$this->shippingRate
  93. // ->setCode('fedex_FEDEX_GROUND');
  94. //->getPrice(1);
  95. //$shippingAddress = $cart->getShippingAddress();
  96. //@todo set in order data
  97. $shippingAddress->setCollectShippingRates(true)
  98. ->collectShippingRates()
  99. ->setShippingMethod('fedex_FEDEX_GROUND'); //shipping method
  100. //$quote->getShippingAddress()->addShippingRate($this->shippingRate);
  101. $quote->setPaymentMethod('checkmo'); //payment method
  102. $quote->setInventoryProcessed(false); //not effetc inventory
  103. $quote->save(); //Now Save quote and your quote is ready
  104.  
  105. // Set Sales Order Payment
  106. $quote->getPayment()->importData(['method' => 'checkmo']);
  107.  
  108. // Collect Totals & Save Quote
  109. $quote->collectTotals()->save();
  110.  
  111. // Create Order From Quote
  112. $order = $this->quoteManagement->submit($quote);
  113.  
  114. $order->setEmailSent(0);
  115. $increment_id = $order->getRealOrderId();
  116. if($order->getEntityId()){
  117. $result['order_id']= $order->getRealOrderId();
  118. }else{
  119. $result=['error'=>1,'msg'=>'Your custom message'];
  120. }
  121. return $result;
  122. }
  123. }
  124.  
  125. array (
  126. 'request' =>
  127. array (
  128. 'WebAuthenticationDetail' =>
  129. array (
  130. 'UserCredential' =>
  131. array (
  132. 'Key' => '****',
  133. 'Password' => '****',
  134. ),
  135. ),
  136. 'ClientDetail' =>
  137. array (
  138. 'AccountNumber' => 'XXXXXX',
  139. 'MeterNumber' => '****',
  140. ),
  141. 'Version' =>
  142. array (
  143. 'ServiceId' => 'crs',
  144. 'Major' => '10',
  145. 'Intermediate' => '0',
  146. 'Minor' => '0',
  147. ),
  148. 'RequestedShipment' =>
  149. array (
  150. 'DropoffType' => 'REGULAR_PICKUP',
  151. 'ShipTimestamp' => '2017-06-27T06:09:23+00:00',
  152. 'PackagingType' => 'YOUR_PACKAGING',
  153. 'TotalInsuredValue' =>
  154. array (
  155. 'Amount' => 0,
  156. 'Currency' => 'USD',
  157. ),
  158. 'Shipper' =>
  159. array (
  160. 'Address' =>
  161. array (
  162. 'PostalCode' => '90034',
  163. 'CountryCode' => 'US',
  164. ),
  165. ),
  166. 'Recipient' =>
  167. array (
  168. 'Address' =>
  169. array (
  170. 'PostalCode' => '11701',
  171. 'CountryCode' => 'US',
  172. 'Residential' => false,
  173. 'City' => 'Ave Forest Hills',
  174. ),
  175. ),
  176. 'ShippingChargesPayment' =>
  177. array (
  178. 'PaymentType' => 'SENDER',
  179. 'Payor' =>
  180. array (
  181. 'AccountNumber' => 'XXXXX',
  182. 'CountryCode' => 'US',
  183. ),
  184. ),
  185. 'CustomsClearanceDetail' =>
  186. array (
  187. 'CustomsValue' =>
  188. array (
  189. 'Amount' => 0,
  190. 'Currency' => 'USD',
  191. ),
  192. ),
  193. 'RateRequestTypes' => 'LIST',
  194. 'PackageCount' => '1',
  195. 'PackageDetail' => 'INDIVIDUAL_PACKAGES',
  196. 'RequestedPackageLineItems' =>
  197. array (
  198. 0 =>
  199. array (
  200. 'Weight' =>
  201. array (
  202. 'Value' => 0.0,
  203. 'Units' => 'LB',
  204. ),
  205. 'GroupPackageCount' => 1,
  206. ),
  207. ),
  208. 'ServiceType' => 'SMART_POST',
  209. 'SmartPostDetail' =>
  210. array (
  211. 'Indicia' => 'PRESORTED_STANDARD',
  212. 'HubId' => NULL,
  213. ),
  214. ),
  215. ),
  216. 'result' =>
  217. stdClass::__set_state(array(
  218. 'HighestSeverity' => 'ERROR',
  219. 'Notifications' =>
  220. stdClass::__set_state(array(
  221. 'Severity' => 'ERROR',
  222. 'Source' => 'crs',
  223. 'Code' => '809',
  224. 'Message' => 'Package 1 - Weight is missing or invalid. ',
  225. 'LocalizedMessage' => 'Package 1 - Weight is missing or invalid. ',
  226. 'MessageParameters' =>
  227. stdClass::__set_state(array(
  228. 'Id' => 'PACKAGE_INDEX',
  229. 'Value' => '1',
  230. )),
  231. )),
  232. 'Version' =>
  233. stdClass::__set_state(array(
  234. 'ServiceId' => 'crs',
  235. 'Major' => 10,
  236. 'Intermediate' => 0,
  237. 'Minor' => 0,
  238. )),
  239. )),
  240. ) {"is_exception":false} []
  241.  
  242. $orderData = [
  243. 'email' => 'ram10test@gmail.com', //buyer email id
  244. 'shipping_address' =>[
  245. 'firstname' => 'Ramki ', //address Details
  246. 'lastname' => 'ram',
  247. 'street' => '10119 Ascan Ave Forest Hills',
  248. 'city' => 'Ave Forest Hills',
  249. 'country_id' => 'US',
  250. 'region' => '43',
  251. 'postcode' => '11701',
  252. 'telephone' => 'XXXX',
  253. 'fax' => '32423',
  254. 'save_in_address_book' => 1
  255. ],
  256. 'items'=> [ //array of product which order you want to create
  257. ['product_id'=>'2','qty'=>1]
  258. ]
  259. ];
Add Comment
Please, Sign In to add comment