I am trying to create a bundle product with Magento 2. But somehow its not linking simple products with the bundle product (i.e. its not saving
bundle options & data, also linking same with product). Can anyone have a look at below code snippet and let me know what I'm doing wrong?
//handle sub-items or sizes
foreach($item->Size as $size){
//$item_name = $name."-".$size->SizeLabel;
$sku = strtolower($size->SizeLabel)."_".$pID;
$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); // instance of object manager
$product = $objectManager->create('\Magento\Catalog\Model\Product');
$product->setSku($sku); // Set your sku here
$product->setName($size->SizeLabel); // Name of Product
$product->setStoreId(1);
$product->setAttributeSetId(4);
$product->setStatus(1); // Status on product enabled/ disabled 1/0
$product->setWeight($size->Weight); // weight of product
$product->setVisibility(1); // visibilty of product (catalog / search / catalog, search / Not visible individually)
$product->setTaxClassId(0); // Tax class id
$product->setTypeId(\Magento\Catalog\Model\Product\Type::TYPE_SIMPLE); // type of product (simple/virtual/downloadable/configurable)
$product->setPrice($size->UnitPrice); // price of product
$product->setWebsiteIds(array(1)); // set website Id
$product->setStockData(array('use_config_manage_stock' => 0, 'manage_stock' => 1, 'is_in_stock' => 1, 'qty' => 9999));
$product->save();
$options[] = array(
'product_id' => $product->getId(), //if of a product in selection
'delete' => '',
'selection_price_value' => 0,
'selection_price_type' => 0,
'selection_qty' => 1,
'selection_can_change_qty' => 0,
'position' => 0,
'is_default' => 0,
'name' => $size->SizeLabel,
'sku' => $sku,
'price' => $size->UnitPrice
);
}
$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); // instance of object manager
$product = $objectManager->create('\Magento\Catalog\Model\Product');
$product->setSku('sku'); // Set your sku here
//Map product with category
$category = $this->_saveCategory($item->WebCategory);
if ($category->getId())
{
$product->setCategoryIds([$category->getId()]);
}
$product->setName($name); // Name of Product
$product->setStoreId(1);
$product->setAttributeSetId(4);
$product->setStatus(1); // Status on product enabled/ disabled 1/0
$product->setWeight($item->Weight); // weight of product
$product->setVisibility(4); // visibilty of product (catalog / search / catalog, search / Not visible individually)
$product->setTaxClassId(0); // Tax class id
$product->setTypeId(\Magento\Catalog\Model\Product\Type::TYPE_BUNDLE); // type of product (simple/virtual/downloadable/configurable)
$product->setPrice($item->UnitPrice); // price of product
$product->setDescription($item->WebDescription); // description of product
$product->setWebsiteIds(array(1)); // set website Id
$product->setStockData(array('use_config_manage_stock' => 1, 'manage_stock' => 1, 'is_in_stock' => 1, 'qty' => 9999));
$bundleOptions = array(
'0' => array(//option id (0, 1, 2, etc)
'title' => 'Size', //option title
'option_id' => '',
'delete' => '',
'type' => 'radio', //option type
'required' => '1', //is option required
'position' => '1' //option position
)
);
$bundleSelections = array(
'0' => array(//option ID
$options
)
);
$this->registry->register('product', $product);
$this->registry->register('productCheck', $product);
$this->registry->register('current_product', $product);
$product->setCanSaveConfigurableAttributes(false);
$product->setCanSaveCustomOptions(true);
$product->setCanSaveBundleSelections(true);
$product->setAffectBundleProductSelections(true);
// Set the Bundle Options & Selection Data
$product->setBundleOptionsData($bundleOptions);
$product->setBundleSelectionsData($bundleSelections);
$product->save();