0

I am using Magento 2.3.* and I want to create a Bundle Product and Associate the simple products using code.

2
1

After creating a product with type bundled we have to add a link and option information for the product.

These are modified attribute as compare to simple product creation

 $product->setTypeId(\Magento\Catalog\Model\Product\Type::TYPE_BUNDLE); 
 $product->setPriceType(1); 
 $product->setPriceView(1);

To add a link use the below code.

$link = $objectManager->create(\Magento\Bundle\Api\Data\LinkInterface::class);
$link->setPosition(0);
$link->setSku('MH01-XS-Orange');
$link->setIsDefault(false);
$link->getQty(1);
$link->setPrice(12);
$link->setPriceType(\Magento\Bundle\Api\Data\LinkInterface::PRICE_TYPE_FIXED);

To add link to option add below code:

$optionRepository = $objectManager->create(\Magento\Bundle\Api\ProductOptionRepositoryInterface::class);
$option = $objectManager->create(\Magento\Bundle\Api\Data\OptionInterface::class);
$option->setTitle('Size');
$option->setType('radio');
$option->setRequired(true);
$option->setPosition(1);
$option->setProductLinks(array($link));
$optionRepository->save($product, $option);

Reference: http://www.w3solver.com/magento-2-add-a-bundled-product-programmatically/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.