Magento 2 – How to Create a Bundle Product Programmatically

bundled-productmagento2productprogrammatically

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();

Best Answer

You code should like:

/** @var $objectManager \Magento\TestFramework\ObjectManager */
$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); // instance of object manager

/** @var \Magento\Catalog\Api\ProductRepositoryInterface $productRepository */
$productRepository = $objectManager->create(Magento\Catalog\Api\ProductRepositoryInterface::class);

//handle sub-items or sizes
foreach($item->Size as $size){
    //$item_name = $name."-".$size->SizeLabel;
    $sku = strtolower($size->SizeLabel) . "_" . $pID;

    /** @var \Magento\Catalog\Api\Data\ProductInterface $product */
    $product = $objectManager->create(\Magento\Catalog\Api\Data\ProductInterface::class);

    $product->setSku($sku); // Set your sku here
    $product->setName($size->SizeLabel); // Name of Product
    $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->setCustomAttribute('tax_class_id', 0);
    $product->setTypeId(\Magento\Catalog\Model\Product\Type::TYPE_SIMPLE); // type of product (simple/virtual/downloadable/configurable)
    $product->setPrice($size->UnitPrice); // price of product


    /** @var \Magento\CatalogInventory\Api\Data\StockItemInterface $stockItem */
    $stockItem = $objectManager->create(\Magento\CatalogInventory\Api\Data\StockItemInterface::class);
    $stockItem->setUseConfigManageStock(false);
    $stockItem->setManageStock(true);
    $stockItem->getIsInStock(true);
    $stockItem->setQty(9999);
    $productExtension->setStockItem($stockItem);

    /** @var \Magento\Catalog\Api\Data\ProductExtensionInterface $productExtension */
    $productExtension = $objectManager->create(\Magento\Catalog\Api\Data\ProductExtensionInterface::class);

    $product->setExtensionAttributes($productExtension);
    $product = $productRepository->save($product, true);



    /** @var \Magento\Bundle\Api\Data\LinkInterface $link */
    $link = $objectManager->create(\Magento\Bundle\Api\Data\LinkInterface::class);
    $link->setPosition(0);
    $link->setSku($product->getSku());
    $link->setIsDefault(false);
    $link->getQty(1);
    $link->setPrice($size->UnitPrice);
    $link->setPriceType(\Magento\Bundle\Api\Data\LinkInterface::PRICE_TYPE_FIXED);

    $links[] = $link;
}

/** @var \Magento\Catalog\Api\Data\ProductInterface $product */
$product = $objectManager->create(\Magento\Catalog\Api\Data\ProductInterface::class);

$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->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->setCustomAttribute('tax_class_id', 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->setCustomAttribute('description', $item->WebDescription); // description of product

/** @var \Magento\Catalog\Api\Data\ProductExtensionInterface $productExtension */
$productExtension = $objectManager->create(\Magento\Catalog\Api\Data\ProductExtensionInterface::class);


/** @var \Magento\Bundle\Api\Data\OptionInterface $option */
$option = $objectManager->create(\Magento\Bundle\Api\Data\OptionInterface::class);
$option->setTitle('Size');
$option->setType('radio');
$option->setRequired(true);
$option->setPosition(1);
$option->setProductLinks($links);
$productExtension->setBundleOptions([$option]);

/** @var \Magento\CatalogInventory\Api\Data\StockItemInterface $stockItem */
$stockItem = $objectManager->create(\Magento\CatalogInventory\Api\Data\StockItemInterface::class);
$stockItem->setUseConfigManageStock(false);
$stockItem->setManageStock(true);
$stockItem->getIsInStock(true);
$stockItem->setQty(9999);
$productExtension->setStockItem($stockItem);

$product->setExtensionAttributes($productExtension);
$productRepository->save($product, true);
Related Topic