Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
product_with_multiple_options_1.php
Go to the documentation of this file.
1 <?php
7 require __DIR__ . '/../../../Magento/Catalog/_files/multiple_products.php';
8 
10 
11 $productIds = range(10, 12, 1);
12 foreach ($productIds as $productId) {
14  $stockItem = $objectManager->create(\Magento\CatalogInventory\Model\Stock\Item::class);
15  $stockItem->load($productId, 'product_id');
16 
17  if (!$stockItem->getProductId()) {
18  $stockItem->setProductId($productId);
19  }
20  $stockItem->setUseConfigManageStock(1);
21  $stockItem->setQty(1000);
22  $stockItem->setIsQtyDecimal(0);
23  $stockItem->setIsInStock(1);
24  $stockItem->save();
25 }
26 
28 $product = $objectManager->create(\Magento\Catalog\Model\Product::class);
29 $product->setTypeId(\Magento\Catalog\Model\Product\Type::TYPE_BUNDLE)
30  ->setId(3)
31  ->setAttributeSetId(4)
32  ->setWebsiteIds([1])
33  ->setName('Bundle Product')
34  ->setSku('bundle-product')
35  ->setVisibility(\Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH)
36  ->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED)
37  ->setStockData(['use_config_manage_stock' => 1, 'qty' => 100, 'is_qty_decimal' => 0, 'is_in_stock' => 1])
38  ->setPriceView(1)
39  ->setSkuType(1)
40  ->setWeightType(1)
41  ->setPriceType(1)
42  ->setPrice(10.0)
43  ->setShipmentType(0)
44  ->setBundleOptionsData(
45  [
46  // Required "Drop-down" option
47  [
48  'title' => 'Option 1',
49  'default_title' => 'Option 1',
50  'type' => 'select',
51  'required' => 1,
52  'delete' => '',
53  ],
54 
55  ]
56  )->setBundleSelectionsData(
57  [
58  [
59  [
60  'product_id' => 10,
61  'selection_qty' => 1,
62  'selection_price_value' => 2.75,
63  'selection_can_change_qty' => 1,
64  'delete' => '',
65  'option_id' => 1
66  ],
67  [
68  'product_id' => 11,
69  'selection_qty' => 1,
70  'selection_price_value' => 6.75,
71  'selection_can_change_qty' => 1,
72  'delete' => '',
73  'option_id' => 1
74  ]
75  ]
76 
77  ]
78  );
79 $productRepository = $objectManager->create(\Magento\Catalog\Api\ProductRepositoryInterface::class);
80 
81 if ($product->getBundleOptionsData()) {
82  $options = [];
83  foreach ($product->getBundleOptionsData() as $key => $optionData) {
84  if (!(bool)$optionData['delete']) {
85  $option = $objectManager->create(\Magento\Bundle\Api\Data\OptionInterfaceFactory::class)
86  ->create(['data' => $optionData]);
87  $option->setSku($product->getSku());
88  $option->setOptionId(null);
89 
90  $links = [];
91  $bundleLinks = $product->getBundleSelectionsData();
92  if (!empty($bundleLinks[$key])) {
93  foreach ($bundleLinks[$key] as $linkData) {
94  if (!(bool)$linkData['delete']) {
95  $link = $objectManager->create(\Magento\Bundle\Api\Data\LinkInterfaceFactory::class)
96  ->create(['data' => $linkData]);
97  $linkProduct = $productRepository->getById($linkData['product_id']);
98  $link->setSku($linkProduct->getSku());
99  $link->setQty($linkData['selection_qty']);
100  $link->setPrice($linkData['selection_price_value']);
101  $links[] = $link;
102  }
103  }
104  $option->setProductLinks($links);
105  $options[] = $option;
106  }
107  }
108  }
109  $extension = $product->getExtensionAttributes();
110  $extension->setBundleProductOptions($options);
111  $product->setExtensionAttributes($extension);
112 }
113 $product->save();
$optionData
defined('TESTS_BP')||define('TESTS_BP' __DIR__
Definition: _bootstrap.php:60