Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
quote_with_new_customer.php
Go to the documentation of this file.
1 <?php
17 
19 $objectManager = Bootstrap::getObjectManager();
20 
22 $product = $objectManager->create(Product::class);
23 $product->setTypeId('simple')
24  ->setAttributeSetId(4)
25  ->setName('Simple Product')
26  ->setSku('simple001')
27  ->setPrice(10)
28  ->setQty(100)
29  ->setVisibility(Visibility::VISIBILITY_BOTH)
30  ->setStatus(Status::STATUS_ENABLED);
31 
33 $stockItem = $objectManager->create(StockItemInterface::class);
34 $stockItem->setQty(100)
35  ->setIsInStock(true);
36 $extensionAttributes = $product->getExtensionAttributes();
37 $extensionAttributes->setStockItem($stockItem);
38 
40 $productRepository = $objectManager->get(ProductRepositoryInterface::class);
42 
43 $addressData = include __DIR__ . '/address_data.php';
44 $billingAddress = $objectManager->create(Address::class, ['data' => $addressData]);
45 $billingAddress->setAddressType('billing');
46 
48 $shippingAddress->setId(null)
49  ->setAddressType('shipping');
50 
52 $store = $objectManager->get(StoreManagerInterface::class)
53  ->getStore();
54 
56 $quote = $objectManager->create(Quote::class);
57 $quote->setCustomerIsGuest(false)
58  ->setCustomerEmail('[email protected]')
59  ->setStoreId($store->getId())
60  ->setReservedOrderId('2000000001')
61  ->setBillingAddress($billingAddress)
62  ->setShippingAddress($shippingAddress)
63  ->addProduct($product);
64 
65 $quote->getPayment()
66  ->setMethod('checkmo');
67 $quote->getShippingAddress()
68  ->setShippingMethod('flatrate_flatrate')
69  ->setCollectShippingRates(true);
70 $quote->collectTotals();
71 
73 $quoteRepository = $objectManager->get(CartRepositoryInterface::class);
defined('TESTS_BP')||define('TESTS_BP' __DIR__
Definition: _bootstrap.php:60