Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
quote_with_configurable_product.php
Go to the documentation of this file.
1 <?php
9 
10 require 'product_configurable.php';
14 $productRepository = Bootstrap::getObjectManager()->create(ProductRepositoryInterface::class);
15 $product = $productRepository->get('configurable');
16 /* Create simple products per each option */
18 $options = Bootstrap::getObjectManager()->create(
19  \Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\Collection::class
20 );
21 $option = $options->setAttributeFilter($attribute->getId())->getFirstItem();
22 
23 $requestInfo = new \Magento\Framework\DataObject(
24  [
25  'product' => 1,
26  'selected_configurable_option' => 1,
27  'qty' => 1,
28  'super_attribute' => [
29  $attribute->getId() => $option->getId()
30  ]
31  ]
32 );
33 
35 $cart = Bootstrap::getObjectManager()->create(\Magento\Checkout\Model\Cart::class);
36 $cart->addProduct($product, $requestInfo);
37 $cart->getQuote()->setReservedOrderId('test_cart_with_configurable');
38 $cart->save();
39 
41 $objectManager = Bootstrap::getObjectManager();
42 $objectManager->removeSharedInstance(\Magento\Checkout\Model\Session::class);