Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
product_1.php
Go to the documentation of this file.
1 <?php
7 /*
8  * Since the bundle product creation GUI doesn't allow to choose values for bundled products' custom options,
9  * bundled items should not contain products with required custom options.
10  * However, if to create such a bundle product, it will be always out of stock.
11  */
12 require __DIR__ . '/../../../Magento/Catalog/_files/products.php';
13 
17 $productRepository = $objectManager->create(\Magento\Catalog\Api\ProductRepositoryInterface::class);
19 
21 $product = $objectManager->create(\Magento\Catalog\Model\Product::class);
22 $product->setTypeId('bundle')
23  ->setId(3)
24  ->setAttributeSetId(4)
25  ->setWeight(2)
26  ->setWebsiteIds([1])
27  ->setName('Bundle Product')
28  ->setSku('bundle-product')
29  ->setVisibility(\Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH)
30  ->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED)
31  ->setStockData(['use_config_manage_stock' => 1, 'qty' => 100, 'is_qty_decimal' => 0, 'is_in_stock' => 1])
32  ->setPriceView(1)
33  ->setSkuType(1)
34  ->setWeightType(1)
35  ->setPriceType(1)
36  ->setShipmentType(0)
37  ->setPrice(10.0)
38  ->setBundleOptionsData(
39  [
40  [
41  'title' => 'Bundle Product Items',
42  'default_title' => 'Bundle Product Items',
43  'type' => 'select', 'required' => 1,
44  'delete' => '',
45  ],
46  ]
47  )
48  ->setBundleSelectionsData(
49  [
50  [
51  [
52  'product_id' => $sampleProduct->getId(),
53  'selection_price_value' => 2.75,
54  'selection_qty' => 1,
55  'selection_can_change_qty' => 1,
56  'delete' => '',
57 
58  ],
59  ],
60  ]
61  );
62 
63 if ($product->getBundleOptionsData()) {
64  $options = [];
65  foreach ($product->getBundleOptionsData() as $key => $optionData) {
66  if (!(bool)$optionData['delete']) {
67  $option = $objectManager->create(\Magento\Bundle\Api\Data\OptionInterfaceFactory::class)
68  ->create(['data' => $optionData]);
69  $option->setSku($product->getSku());
70  $option->setOptionId(null);
71 
72  $links = [];
73  $bundleLinks = $product->getBundleSelectionsData();
74  if (!empty($bundleLinks[$key])) {
75  foreach ($bundleLinks[$key] as $linkData) {
76  if (!(bool)$linkData['delete']) {
78  $link = $objectManager->create(\Magento\Bundle\Api\Data\LinkInterfaceFactory::class)
79  ->create(['data' => $linkData]);
80  $linkProduct = $productRepository->getById($linkData['product_id']);
81  $link->setSku($linkProduct->getSku());
82  $link->setQty($linkData['selection_qty']);
83  $link->setPrice($linkData['selection_price_value']);
84  if (isset($linkData['selection_can_change_qty'])) {
85  $link->setCanChangeQuantity($linkData['selection_can_change_qty']);
86  }
87  $links[] = $link;
88  }
89  }
90  $option->setProductLinks($links);
91  $options[] = $option;
92  }
93  }
94  }
95  $extension = $product->getExtensionAttributes();
96  $extension->setBundleProductOptions($options);
97  $product->setExtensionAttributes($extension);
98 }
99 //$product->save();
100 $productRepository->save($product, true);
$objectManager
Definition: product_1.php:15
$product
Definition: product_1.php:21
$optionData
defined('TESTS_BP')||define('TESTS_BP' __DIR__
Definition: _bootstrap.php:60
$productRepository
Definition: product_1.php:17
$sampleProduct
Definition: product_1.php:18