Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
cart_with_simple_product_and_custom_options.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
15 
16 require __DIR__ . '/../../../Magento/Catalog/_files/product_simple_with_custom_options.php';
17 
19 $productRepository = Bootstrap::getObjectManager()
20  ->create(ProductRepositoryInterface::class);
21 $product = $productRepository->get('simple_with_custom_options');
22 
23 $options = [];
25 foreach ($product->getOptions() as $option) {
26  switch ($option->getGroupByType()) {
27  case ProductCustomOptionInterface::OPTION_GROUP_SELECT:
28  $value = key($option->getValues());
29  break;
30  default:
31  $value = 'test';
32  break;
33  }
34  $options[$option->getId()] = $value;
35 }
36 
37 $requestInfo = new DataObject(['qty' => 1, 'options' => $options]);
38 
40 $quote = Bootstrap::getObjectManager()->create(Quote::class);
41 $quote->setReservedOrderId('test_order_item_with_custom_options');
42 $quote->addProduct($product, $requestInfo);
43 $quote->save();
44 
46 $objectManager = Bootstrap::getObjectManager();
47 $objectManager->removeSharedInstance(Session::class);
defined('TESTS_BP')||define('TESTS_BP' __DIR__
Definition: _bootstrap.php:60
$value
Definition: gender.phtml:16
foreach($product->getOptions() as $option) $requestInfo