0

I'm trying to programmatically create a bundled product using the following link as a reference: https://github.com/magento/magento2/blob/develop/dev/tests/api-functional/testsuite/Magento/Bundle/Api/ProductServiceTest.php

I'm adding the bundle product first, without any options, then loading it out of the database in order to add the linked products.

Here's an example of the array I'm using:

Array
(
    [0] => Array
        (
            [title] => Test option
            [required] => 1
            [type] => radio
            [position] => 0
            [product_links] => Array
                (
                    [0] => Array
                        (
                            [sku] => test-simple
                            [qty] => 1
                            [position] => 0
                            [is_default] => 
                            [price] => 0
                            [price_type] => 1
                            [can_change_quantity] => 0
                        )
                )
        )
)

And here's my code to add the relation:

    $bundleProduct = $this->objectManager->get('Magento\Catalog\Model\Product')->load($productID);
    $bundleProduct->getExtensionAttributes()->setBundleProductOptions($bundleOptions);
    $bundleProduct->save();

However, this does not add any linked product to the bundle product. Doesn't anyone have an idea what I'm missing? As the documentation for Magento2 is very limited at the moment.

0

You should pass \Magento\Bundle\Api\Data\OptionInterface to setBundleProductOptions().

example code:

$productRepository = $this->objectManager->get('\Magento\Catalog\Api\ProductRepositoryInterface');
$product = $productRepository->get('product_sku', true);
$data = Array
(
[0] => Array
    (
        [title] => Test option
        [required] => 1
        [type] => radio
        [position] => 0
        [product_links] => Array
            (
                [0] => Array
                    (
                        [sku] => test-simple
                        [qty] => 1
                        [position] => 0
                        [is_default] => 
                        [price] => 0
                        [price_type] => 1
                        [can_change_quantity] => 0
                    )
            )
    )
); // like in your example

    $options = [];
    foreach ($data 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);

                foreach ($optionData['product_links'] as $linkData) {
                    if (!(bool)$linkData['delete']) {
                        $link = $objectManager->create('Magento\Bundle\Api\Data\LinkInterfaceFactory')
                            ->create(['data' => $linkData]);
                        $linkProduct = $productRepository->get($linkData['sku']);
                        $link->setSku($linkProduct->getSku());

                        $links[] = $link;
                    }
                }
                $option->setProductLinks($links);
                $options[] = $option;

        }
    }
    $extension = $product->getExtensionAttributes();
    $extension->setBundleProductOptions($options);
 $productRepository->save($product, true);
| improve this answer | |
  • Thanks for replying, but that code doesn't fit with the array/code that I've provided. The bundle product currently only exists on it's own, there are no Bundle Items, these are what I'm trying to add, so getBundleOptionsData() will not return anything, therefore the foreach would not be used? – Karl Jan 26 '16 at 17:07
  • Also, $productRepository is undefined, so I think the answer is missing some code. – Karl Jan 26 '16 at 17:08
  • Thanks for updating your answer, I've tried your example but I'm receiving the following error: Could not save child: "SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '357-132' for key 'PRIMARY', query was: INSERT INTO catalog_product_relation (parent_id,child_ id) VALUES (?, ?)" – Karl Jan 27 '16 at 9:24
  • It appears to throw the above error when there are multiple options, e.g Test Option, Test Option 2, which both have an array of product_links. Any ideas? – Karl Jan 27 '16 at 9:41
  • Any ideas @KAndy? – Karl Jan 29 '16 at 9:42

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