create('Magento\Catalog\Api\ProductRepositoryInterface'); $sampleProduct = $productRepository->get('simple'); /** @var $product \Magento\Catalog\Model\Product */ $product = $objectManager->create('Magento\Catalog\Model\Product'); $product->setTypeId('bundle') ->setId(3) ->setAttributeSetId(4) ->setWebsiteIds([1]) ->setName('Bundle Product') ->setSku('bundle-product') ->setVisibility(\Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH) ->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED) ->setStockData(['use_config_manage_stock' => 1, 'qty' => 100, 'is_qty_decimal' => 0, 'is_in_stock' => 1]) ->setPriceView(1) ->setPriceType(1) ->setShipmentType(1) ->setPrice(10.0) ->setBundleOptionsData( [ [ 'title' => 'Bundle Product Items', 'default_title' => 'Bundle Product Items', 'type' => 'select', 'required' => 1, 'delete' => '', ], ] ) ->setBundleSelectionsData( [ [ [ 'product_id' => $sampleProduct->getId(), 'selection_qty' => 1, 'selection_can_change_qty' => 1, 'delete' => '', ], ], ] ); if ($product->getBundleOptionsData()) { $options = []; foreach ($product->getBundleOptionsData() as $key => $optionData) { if (!(bool)$optionData['delete']) { $option = $objectManager->create('Magento\Bundle\Api\Data\OptionInterfaceFactory') ->create(['data' => $optionData]); $option->setSku($product->getSku()); $option->setOptionId(null); $links = []; $bundleLinks = $product->getBundleSelectionsData(); if (!empty($bundleLinks[$key])) { foreach ($bundleLinks[$key] as $linkData) { if (!(bool)$linkData['delete']) { /** @var \Magento\Bundle\Api\Data\LinkInterface$link */ $link = $objectManager->create('Magento\Bundle\Api\Data\LinkInterfaceFactory') ->create(['data' => $linkData]); $linkProduct = $productRepository->getById($linkData['product_id']); $link->setSku($linkProduct->getSku()); $link->setQty($linkData['selection_qty']); if (isset($linkData['selection_can_change_qty'])) { $link->setCanChangeQuantity($linkData['selection_can_change_qty']); } $links[] = $link; } } $option->setProductLinks($links); $options[] = $option; } } } $extension = $product->getExtensionAttributes(); $extension->setBundleProductOptions($options); $product->setExtensionAttributes($extension); } $product->save();