Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
create_quote_on_us_website.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
11 use Magento\Quote\Api\Data\AddressInterfaceFactory;
15 
17 $cartRepository = Bootstrap::getObjectManager()->get(CartRepositoryInterface::class);
19 $cartManagement = Bootstrap::getObjectManager()->get(CartManagementInterface::class);
21 $addressFactory = Bootstrap::getObjectManager()->get(AddressInterfaceFactory::class);
23 $storeRepository = Bootstrap::getObjectManager()->get(StoreRepositoryInterface::class);
25 $storeManager = Bootstrap::getObjectManager()->get(StoreManagerInterface::class);
26 
27 $cartId = $cartManagement->createEmptyCart();
29 $cart->setCustomerEmail('[email protected]');
30 $cart->setCustomerIsGuest(true);
31 $store = $storeRepository->get('store_for_us_website');
32 $cart->setStoreId($store->getId());
33 $storeManager->setCurrentStore($store->getCode());
34 
37  [
38  'data' => [
39  AddressInterface::KEY_COUNTRY_ID => 'US',
40  AddressInterface::KEY_REGION_ID => 15,
41  AddressInterface::KEY_LASTNAME => 'Doe',
42  AddressInterface::KEY_FIRSTNAME => 'John',
43  AddressInterface::KEY_STREET => 'example street',
44  AddressInterface::KEY_EMAIL => '[email protected]',
45  AddressInterface::KEY_CITY => 'example city',
46  AddressInterface::KEY_TELEPHONE => '000 0000',
47  AddressInterface::KEY_POSTCODE => 12345
48  ]
49  ]
50 );
51 $cart->setReservedOrderId('created_order_for_test');
52 $cart->setBillingAddress($address);
53 $cart->setShippingAddress($address);
54 $cart->getPayment()->setMethod('checkmo');
55 $cart->getShippingAddress()->setShippingMethod('flatrate_flatrate');
56 $cart->getShippingAddress()->setCollectShippingRates(true);
57 $cart->getShippingAddress()->collectShippingRates();
58 $cartRepository->save($cart);