Creation of bundle products in a loop

142 views Asked by At

I have a loop to create several bundle products programmatically. The creation of new bundles works fine, but if I have an existing bundle and try to add/remove options, it fails.

If I run my import code the first time, the bundles are created. The second time, all options will be removed and only the last bundle of the array has its options set.

$ids = [8663,8665,8664];
foreach ($ids as $id) {
    createBundle($id);
}

function createBundle($id)
{
    Mage::unregister('product');

    try {
        $bundleProduct = Mage::getModel('catalog/product')->load($id);
        Mage::register('product', $bundleProduct);

        // try to get already assigned options and remove them all
        $selectionCollection = $bundleProduct->getTypeInstance(true)->getSelectionsCollection(
            $bundleProduct->getTypeInstance(true)->getOptionsIds($bundleProduct), $bundleProduct
        );

        // remove assigned options
        // works fine
        foreach ($selectionCollection as $option) 
        {
            $optionModel = Mage::getModel('bundle/option');
            $optionModel->setId($option->option_id);
            $optionModel->delete();
        }

        $bundleOptions = array(
            '0' => array( //option id (0, 1, 2, etc)
                'title' => 'item01', //option title
                'option_id' => '',
                'delete' => '',
                'type' => 'select', //option type
                'required' => '1', //is option required
                'position' => '0' //option position
            ),
            '1' => array(
                'title' => 'item02',
                'option_id' => '',
                'delete' => '',
                'type' => 'multi',
                'required' => '1',
                'position' => '0'
            )
        );

        $bundleSelections = array();
        $bundleSelections = array(
            '0' => array( //option ID
                '0' => array( //selection ID of the option (first product under this option (option ID) would have ID of 0, second an ID of 1, etc)
                    'product_id' => 8631, //id of a product in selection
                    'delete' => '',
                    'selection_price_value' => '0',
                    'selection_price_type' => 0,
                    'selection_qty' => 10,
                    'selection_can_change_qty' => 0,
                    'position' => 0,
                    'is_default' => 1
                )
            ),
            '1' => array( //option ID
                '0' => array(
                    'product_id' => 8630,
                    'delete' => '',
                    'selection_price_value' => '0',
                    'selection_price_type' => 0,
                    'selection_qty' => 1,
                    'selection_can_change_qty' => 0,
                    'position' => 0,
                    'is_default' => 1
                )
            )
        );

        // flags for saving custom options/selections
        $bundleProduct->setCanSaveCustomOptions(true);
        $bundleProduct->setCanSaveBundleSelections(true);
        $bundleProduct->setAffectBundleProductSelections(true);

        // setting the bundle options and selection data
        $bundleProduct->setBundleOptionsData($bundleOptions);
        $bundleProduct->setBundleSelectionsData($bundleSelections);

        $bundleProduct->save();
        $bundleProduct->setData(array());
        return $bundleProduct->getId();

    } catch (Exception $e) {
        Mage::log($e->getMessage());
        echo $e->getMessage();
    }
}

So, how can I get this work? If I remove the part where the options are deleted, then every time the code is executed there are new empty options created (which is wrong).

Update: I found out, that the bundles are created/updated correctly, if I run the script thrice, each with another product id. So the problem must be the execution in a loop in a single request.

1

There are 1 answers

0
webster On BEST ANSWER

Finally, I found out by myself. This part of the posted code:

    // try to get already assigned options and remove them all
    $selectionCollection = $bundleProduct->getTypeInstance(true)->getSelectionsCollection(
        $bundleProduct->getTypeInstance(true)->getOptionsIds($bundleProduct), $bundleProduct
    );

    // remove assigned options
    // works fine
    foreach ($selectionCollection as $option) 
    {
        $optionModel = Mage::getModel('bundle/option');
        $optionModel->setId($option->option_id);
        $optionModel->delete();
    }

seems to only remove the selection part of the options. To remove the full options, I had to do it like this:

    // try to get already assigned options and remove them all
    $optionCollection = $_product->getTypeInstance(true)->getOptionsCollection($_product);

    // remove assigned options
    foreach ($optionCollection as $option) 
    {
        $option->delete();
    }