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