Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
product_bundle.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 use Magento\Bundle\Api\Data\LinkInterfaceFactory;
11 use Magento\Bundle\Api\Data\OptionInterfaceFactory;
23 
25 $productRepository = Bootstrap::getObjectManager()->create(ProductRepositoryInterface::class);
26 
28 $installer = Bootstrap::getObjectManager()->create(CategorySetup::class);
29 
31 $website = Bootstrap::getObjectManager()->create(Website::class);
32 $website->load('us_website', 'code');
33 $websiteIds = [$website->getId()];
34 
35 $attributeSetId = $installer->getAttributeSetId('catalog_product', 'Default');
37 
39 $product = Bootstrap::getObjectManager()->create(Product::class);
40 $product->setTypeId(Type::TYPE_SIMPLE)
41  ->setId($productId)
42  ->setAttributeSetId($attributeSetId)
43  ->setWebsiteIds($websiteIds)
44  ->setName('Simple product ' . $productId)
45  ->setSku('simple_' . $productId)
46  ->setPrice($productId)
47  ->setVisibility(Visibility::VISIBILITY_BOTH)
48  ->setStatus(Status::STATUS_ENABLED)
49  ->setStockData(['use_config_manage_stock' => 1, 'qty' => 100, 'is_qty_decimal' => 0, 'is_in_stock' => 1]);
50 
52 
53 // Remove any previously created product with the same id.
55 $registry = Bootstrap::getObjectManager()->get(Registry::class);
56 $registry->unregister('isSecureArea');
57 $registry->register('isSecureArea', true);
58 try {
59  $productToDelete = $productRepository->getById(1);
60  $productRepository->delete($productToDelete);
61 
63  $itemResource = Bootstrap::getObjectManager()->get(QuoteItem::class);
64  $itemResource->getConnection()->delete(
65  $itemResource->getMainTable(),
66  'product_id = ' . $productToDelete->getId()
67  );
68 } catch (\Exception $e) {
69  // Nothing to remove
70 }
71 $registry->unregister('isSecureArea');
72 $registry->register('isSecureArea', false);
73 
75 
77 $bundleProduct = Bootstrap::getObjectManager()->create(Product::class);
78 $bundleProduct->setTypeId(Type::TYPE_BUNDLE)
79  ->setId($bundleProductId)
80  ->setWebsiteIds($websiteIds)
81  ->setAttributeSetId(4)
82  ->setName('Bundle Product')
83  ->setSku('bundle')
84  ->setVisibility(Visibility::VISIBILITY_BOTH)
85  ->setStatus(Status::STATUS_ENABLED)
86  ->setStockData(['use_config_manage_stock' => 1, 'qty' => 100, 'is_qty_decimal' => 0, 'is_in_stock' => 1])
87  ->setPriceView(1)
88  ->setPriceType(1)
89  ->setShipmentType(1)
90  ->setPrice(10.0)
91  ->setBundleOptionsData(
92  [
93  [
94  'title' => 'Bundle Product Items',
95  'default_title' => 'Bundle Product Items',
96  'type' => 'select',
97  'required' => 1,
98  'delete' => '',
99  ],
100  ]
101  )
102  ->setBundleSelectionsData(
103  [
104  [
105  [
106  'product_id' => $product->getId(),
107  'selection_qty' => 1,
108  'selection_can_change_qty' => 1,
109  'delete' => '',
110  ],
111  ],
112  ]
113  );
114 if ($bundleProduct->getBundleOptionsData()) {
115  $options = [];
116  foreach ($bundleProduct->getBundleOptionsData() as $key => $optionData) {
117  if (!(bool)$optionData['delete']) {
119  $option = Bootstrap::getObjectManager()->create(OptionInterfaceFactory::class)
120  ->create(['data' => $optionData]);
121  $option->setSku($bundleProduct->getSku());
122  $option->setOptionId(null);
123  $links = [];
124  $bundleLinks = $bundleProduct->getBundleSelectionsData();
125  if (!empty($bundleLinks[$key])) {
126  foreach ($bundleLinks[$key] as $linkData) {
127  if (!(bool)$linkData['delete']) {
129  $link = Bootstrap::getObjectManager()->create(LinkInterfaceFactory::class)
130  ->create(['data' => $linkData]);
131  $linkProduct = $productRepository->getById($linkData['product_id']);
132  $link->setSku($linkProduct->getSku());
133  $link->setQty($linkData['selection_qty']);
134  if (isset($linkData['selection_can_change_qty'])) {
135  $link->setCanChangeQuantity($linkData['selection_can_change_qty']);
136  }
137  $links[] = $link;
138  }
139  }
140  $option->setProductLinks($links);
141  $options[] = $option;
142  }
143  }
144  }
145  $extension = $bundleProduct->getExtensionAttributes();
146  $extension->setBundleProductOptions($options);
147  $bundleProduct->setExtensionAttributes($extension);
148 }
149 $bundleProduct->save();
150 
152 $categoryLinkManagement = Bootstrap::getObjectManager()
153  ->create(CategoryLinkManagementInterface::class);
154 
155 $categoryLinkManagement->assignProductToCategories(
156  $bundleProduct->getSku(),
157  [2]
158 );
$bundleProductId
$productId
$attributeSetId
$optionData
$website
$categoryLinkManagement
$websiteIds
$productRepository
$installer
$product
$itemResource
$bundleProduct
$registry