Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
products_bundle.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
8 use Magento\Catalog\Api\Data\ProductInterfaceFactory;
15 use Magento\Bundle\Api\Data\OptionInterfaceFactory;
16 use Magento\Bundle\Api\Data\LinkInterfaceFactory;
19 
20 $objectManager = Bootstrap::getObjectManager();
22 $productFactory = $objectManager->get(ProductInterfaceFactory::class);
23 
24 $extensionAttributesFactory = $objectManager->get(ExtensionAttributesFactory::class);
25 $bundleOptionFactory = $objectManager->get(OptionInterfaceFactory::class);
26 $productLinkFactory = $objectManager->get(LinkInterfaceFactory::class);
27 
29 $productRepository = $objectManager->get(ProductRepositoryInterface::class);
30 $productRepository->cleanCache();
31 
33  [
34  'attributes' => [
35  'attribute_set_id' => 4,
36  'type_id' => Type::TYPE_BUNDLE,
37  'sku' => 'SKU-BUNDLE-1',
38  'name' => 'Bundle Product Blue',
39  'status' => Status::STATUS_ENABLED,
40  'stock_data' => ['is_in_stock' => true]
41  ],
42  'custom_attributes' => [
43  'price_type' => Price::PRICE_TYPE_DYNAMIC,
44  'shipment_type' => AbstractType::SHIPMENT_SEPARATELY,
45  'sku_type' => 0,
46  'price_view' => 1
47  ],
48  'simple_links' => [
49  [
50  'sku' => 'SKU-1',
51  'qty' => 2,
52  'title' => 'Simple Product Orange'
53  ],
54  [
55  'sku' => 'SKU-3',
56  'qty' => 3,
57  'title' => 'Simple Product Blue'
58  ]
59  ]
60  ],
61  [
62  'attributes' => [
63  'attribute_set_id' => 4,
64  'type_id' => Type::TYPE_BUNDLE,
65  'sku' => 'SKU-BUNDLE-2',
66  'name' => 'Bundle Product White',
67  'status' => Status::STATUS_ENABLED,
68  'stock_data' => ['is_in_stock' => true]
69  ],
70  'custom_attributes' => [
71  'price_type' => Price::PRICE_TYPE_DYNAMIC,
72  'shipment_type' => AbstractType::SHIPMENT_TOGETHER,
73  'sku_type' => 0,
74  'price_view' => 1
75  ],
76  'simple_links' => [
77  [
78  'sku' => 'SKU-2',
79  'qty' => 3,
80  'title' => 'Simple Product White'
81  ],
82  [
83  'sku' => 'SKU-3',
84  'qty' => 4,
85  'title' => 'Simple Product Blue'
86  ]
87  ]
88  ]
89 ];
90 
91 foreach ($productsData as $productData) {
93  $product = $productFactory->create();
94  foreach ($productData['attributes'] as $code => $value) {
95  $product->setDataUsingMethod($code, $value);
96  }
97  $product->setCustomAttributes($productData['custom_attributes']);
98 
99  $options = [];
100  foreach ($productData['simple_links'] as $linkData) {
102  $link = $productLinkFactory->create();
103  $link->setSku($linkData['sku']);
104  $link->setQty($linkData['qty']);
105  $link->setCanChangeQuantity(1);
106 
108  $option = $bundleOptionFactory->create();
109  $option->setTitle($linkData['title']);
110  $option->setRequired(true);
111  $option->setType('select');
112  $option->setProductLinks([$link]);
113  $options[] = $option;
114  }
115 
117  $extensionAttributes = $extensionAttributesFactory->create(ProductInterface::class);
118  $extensionAttributes->setBundleProductOptions($options);
119 
120  $product->setExtensionAttributes($extensionAttributes);
122 }
$bundleOptionFactory
$productRepository
$productFactory
$value
Definition: gender.phtml:16
$objectManager
$extensionAttributes
Definition: payment.php:22
$productLinkFactory
$productData
$productsData
$extensionAttributesFactory
$code
Definition: info.phtml:12