Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
default_stock_bundle_products.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 
15 
17 
18 require __DIR__ . '/../../../../../../dev/tests/integration/testsuite/Magento/Catalog/_files/products.php';
19 require __DIR__ . '/product_simple_out_of_stock.php';
20 
24 $productRepository = $objectManager->create(\Magento\Catalog\Api\ProductRepositoryInterface::class);
27 $productBundleInStock = $objectManager->create(\Magento\Catalog\Model\Product::class);
28 $productBundleInStock->setTypeId(Type::TYPE_BUNDLE)
29  ->setId(3)
30  ->setAttributeSetId(4)
31  ->setWebsiteIds([1])
32  ->setName('Bundle Product In Stock')
33  ->setSku('bundle-product-in-stock')
34  ->setVisibility(\Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH)
35  ->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED)
36  ->setStockData(['use_config_manage_stock' => 1, 'qty' => 100, 'is_qty_decimal' => 0, 'is_in_stock' => 1])
37  ->setPriceView(1)
38  ->setPriceType(1)
39  ->setShipmentType(1)
40  ->setPrice(10.0)
41  ->setBundleOptionsData(
42  [
43  [
44  'title' => 'Bundle Product Items',
45  'default_title' => 'Bundle Product Items',
46  'type' => 'select', 'required' => 1,
47  'delete' => '',
48  ],
49  ]
50  )
51  ->setBundleSelectionsData(
52  [
53  [
54  [
55  'product_id' => $sampleProduct->getId(),
56  'selection_qty' => 1,
57  'selection_can_change_qty' => 1,
58  'delete' => '',
59  ],
60  ],
61  ]
62  );
63 if ($productBundleInStock->getBundleOptionsData()) {
64  $options = [];
65  foreach ($productBundleInStock->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($productBundleInStock->getSku());
70  $option->setOptionId(null);
71  $links = [];
72  $bundleLinks = $productBundleInStock->getBundleSelectionsData();
73  if (!empty($bundleLinks[$key])) {
74  foreach ($bundleLinks[$key] as $linkData) {
75  if (!(bool)$linkData['delete']) {
77  $link = $objectManager->create(\Magento\Bundle\Api\Data\LinkInterfaceFactory::class)
78  ->create(['data' => $linkData]);
79  $linkProduct = $productRepository->getById($linkData['product_id']);
80  $link->setSku($linkProduct->getSku());
81  $link->setQty($linkData['selection_qty']);
82  if (isset($linkData['selection_can_change_qty'])) {
83  $link->setCanChangeQuantity($linkData['selection_can_change_qty']);
84  }
85  $links[] = $link;
86  }
87  }
88  $option->setProductLinks($links);
89  $options[] = $option;
90  }
91  }
92  }
93  $extension = $productBundleInStock->getExtensionAttributes();
94  $extension->setBundleProductOptions($options);
95  $productBundleInStock->setExtensionAttributes($extension);
96 }
97 $productBundleInStock->save();
98 
99 $sampleProductOutOfStock = $productRepository->get('simple-out-of-stock');
101 $productBundleOutOfStock = $objectManager->create(\Magento\Catalog\Model\Product::class);
102 $productBundleOutOfStock->setTypeId(Type::TYPE_BUNDLE)
103  ->setId(4)
104  ->setAttributeSetId(4)
105  ->setWebsiteIds([1])
106  ->setName('Bundle Product Out Of Stock')
107  ->setSku('bundle-product-out-of-stock')
108  ->setVisibility(\Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH)
109  ->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED)
110  ->setStockData([
111  'use_config_manage_stock' => 1,
112  'use_config_backorders' => 1,
113  'qty' => 100,
114  'is_qty_decimal' => 0,
115  'is_in_stock' => 1
116  ])
117  ->setPriceView(1)
118  ->setPriceType(1)
119  ->setShipmentType(1)
120  ->setPrice(10.0)
121  ->setBundleOptionsData(
122  [
123  [
124  'title' => 'Bundle Product Items',
125  'default_title' => 'Bundle Product Items',
126  'type' => 'select', 'required' => 1,
127  'delete' => '',
128  ],
129  ]
130  )
131  ->setBundleSelectionsData(
132  [
133  [
134  [
135  'product_id' => $sampleProductOutOfStock->getId(),
136  'selection_qty' => 1,
137  'selection_can_change_qty' => 1,
138  'delete' => '',
139  ],
140  ],
141  ]
142  );
143 if ($productBundleOutOfStock->getBundleOptionsData()) {
144  $options = [];
145  foreach ($productBundleOutOfStock->getBundleOptionsData() as $key => $optionData) {
146  if (!(bool)$optionData['delete']) {
147  $option = $objectManager->create(\Magento\Bundle\Api\Data\OptionInterfaceFactory::class)
148  ->create(['data' => $optionData]);
149  $option->setSku($productBundleOutOfStock->getSku());
150  $option->setOptionId(null);
151  $links = [];
152  $bundleLinks = $productBundleOutOfStock->getBundleSelectionsData();
153  if (!empty($bundleLinks[$key])) {
154  foreach ($bundleLinks[$key] as $linkData) {
155  if (!(bool)$linkData['delete']) {
157  $link = $objectManager->create(\Magento\Bundle\Api\Data\LinkInterfaceFactory::class)
158  ->create(['data' => $linkData]);
159  $linkProduct = $productRepository->getById($linkData['product_id']);
160  $link->setSku($linkProduct->getSku());
161  $link->setQty($linkData['selection_qty']);
162  if (isset($linkData['selection_can_change_qty'])) {
163  $link->setCanChangeQuantity($linkData['selection_can_change_qty']);
164  }
165  $links[] = $link;
166  }
167  }
168  $option->setProductLinks($links);
169  $options[] = $option;
170  }
171  }
172  }
173  $extension = $productBundleOutOfStock->getExtensionAttributes();
174  $extension->setBundleProductOptions($options);
175  $productBundleOutOfStock->setExtensionAttributes($extension);
176 }
$optionData
defined('TESTS_BP')||define('TESTS_BP' __DIR__
Definition: _bootstrap.php:60