Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
product_with_custom_option.php
Go to the documentation of this file.
1 <?php
7 require __DIR__ . '/../../Checkout/_files/simple_product.php';
10 $product->load(1);
11 
13 $product->setCanSaveCustomOptions(
14  true
15 )->setHasOptions(
16  true
17 );
18 
20  [
21  'id' => 1,
22  'option_id' => 0,
23  'previous_group' => 'text',
24  'title' => 'Test Field',
25  'type' => 'field',
26  'is_require' => 1,
27  'sort_order' => 0,
28  'price' => 1,
29  'price_type' => 'fixed',
30  'sku' => '1-text',
31  'max_characters' => 100,
32  ],
33 ];
34 
36 
38 $customOptionFactory = $objectManager->create(\Magento\Catalog\Api\Data\ProductCustomOptionInterfaceFactory::class);
39 
40 foreach ($oldOptions as $option) {
42  $customOption = $customOptionFactory->create(['data' => $option]);
43  $customOption->setProductSku($product->getSku());
44 
46 }
47 
48 $product->setOptions($customOptions)->save();
49 
51 $product = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(\Magento\Catalog\Model\Product::class);
52 $product->load(1);
53 $optionId = $product->getOptions()[0]->getOptionId();
54 
55 $requestInfo = new \Magento\Framework\DataObject(['qty' => 1, 'options' => [$optionId => 'test']]);
56 
57 require __DIR__ . '/../../Checkout/_files/cart.php';
$customOption
Definition: products.php:73
$objectManager
Definition: bootstrap.php:17
defined('TESTS_BP')||define('TESTS_BP' __DIR__
Definition: _bootstrap.php:60