2

I have a bundled product that I would like to add a new bundled item into a specific selection, so I tried the following:

// Load existing bundled product
$productCheck = Mage::getModel('catalog/product')->load($bundled_product_id);

// Get option data
$options = $productCheck->getTypeInstance(true)->getOptionsCollection($productCheck);

// Set selection data
foreach ($options as $option) {
    if ($option->getTitle() == $data['bundled_title']) {
        $selection = new Mage_Bundle_Model_Selection();
        $selection->addData(array(
            'entity_id' => $productCheck->getId(),
            'product_id' => $new_bundled_item_id,
            'selection_qty' => 1,
            'selection_can_change_qty' => 0,
            'position' => 0,
            'is_default' => 1,
            'selection_id' => '',
            'selection_price_type' => 0,
            'selection_price_value' => 0.0,
            'option_id' => $option->getId(),
            'delete' => ''
        ));
        $option->setStoreId($data['store_id']);
        $option->addSelection($selection);
        $option->save();
        $selection->save();
    }
}
$productCheck->save();

This does not apear to be throwing an error, at the same time, nothing seems to be changing with the bundled product. The new item is not getting added.

Any ideas? I have already checked; $new_bundled_item_id is correct and the option title does match.


Edit

I tried to solve it like this: http://pastebin.com/hQbxQHak - when the code runs, it appears to be creating a new empty option but my 2nd selection does appear to be added. it looks like this:

enter image description here

This is not quite right, but if I re-ran the script again, this is what happens:

enter image description here

I am not sure what's happening, can anyone see anything wrong with my code? Or am I doing this completely wrong?

2

I have solved it, like this:

public function syncBundledProduct($data)
{
    // Check if bundled product already exists
    $bundled_product_id = Mage::getModel("catalog/product")->getIdBySku($data['sku']);

    // Load new bundled item
    $new_bundled_item_id = (int)Mage::getModel("catalog/product")->getIdBySku($data['bundled_sku']);
    if (!$new_bundled_item_id)
        throw new Exception('Product with sku '. $data['bundled_sku'] .' does not exists');

    // Bundled product already exists
    if ($bundled_product_id)
    {
        // Load existing bundled product
        $productCheck = Mage::getModel('catalog/product')->load($bundled_product_id);
        $productCheck->setName($data['name']);
        $productCheck->setDescription($data['description']);
        $productCheck->setShortDescription($data['short_description']);
        $productCheck->setMetaDescription($data['meta_description']);
        $productCheck->setPrice($data['price']);
    }
    else
    {
        // Create new bundled product
        $productCheck = Mage::getModel('catalog/product');
        $productCheck->setData(array(
            'sku_type' => 0,
            'sku' => $data['sku'],
            'name' => $data['name'],
            'description' => $data['description'],
            'short_description' => $data['short_description'],
            'meta_description' => $data['meta_description'],
            'type_id' => 'bundle',
            'tax_class_id' => 2,
            'attribute_set_id' => 4,
            'weight_type' => 0,
            'visibility' => 4,
            'price_type' => 1,
            'price' => $data['price'],
            'price_view' => 0,
            'status' => 1,
            'created_at' => strtotime('now'),
            'store_id' => $data['store_id'],
            'website_ids' => Mage::getModel('core/store')->load($data['store_id'])->getWebsiteId()
        ));
    }

    // Load option & selection data
    $productCheck->getTypeInstance(true)->setStoreFilter($productCheck->getStoreId(), $productCheck);
    $optionCollection = $productCheck->getTypeInstance(true)->getOptionsCollection($productCheck);
    $selectionCollection = $productCheck->getTypeInstance(true)->getSelectionsCollection(
        $productCheck->getTypeInstance(true)->getOptionsIds($productCheck), $productCheck
    );
    $optionCollection->appendSelections($selectionCollection);

    // Init raw option & selection data
    $bundleOptions = array();
    $bundleSelections = array();

    // Set raw option & selection data
    if (!$optionCollection->count()) {
        $bundleOptions = array(
            0 => array(
                'title' => $data['bundled_title'],
                'default_title' => $data['bundled_title'],
                'option_id' => '',
                'delete' => '',
                'type' => 'checkbox',
                'required' => '1',
                'position' => '1'
            )
        );
        $bundleSelections[0][0] = array(
            'product_id' => $new_bundled_item_id,
            'delete' => '',
            'selection_price_value' => 0.00,
            'selection_price_type' => 0,
            'selection_qty' => 1,
            'selection_can_change_qty' => 0,
            'position' => 0,
            'is_default' => 1
        );
    } else {
        $new_bundled_item_found = false;
        $i = 0;
        $option_index = null;
        foreach ($optionCollection as $option) {
            if ($option->getData('title') && $option->getData('title') == $data['bundled_title']) {
                $option_index = $i;
                $bundleOptions[$option_index] = array(
                    'option_id' => $option->getOptionId(),
                    'required' => $option->getData('required'),
                    'position' => $option->getData('position'),
                    'type' => $option->getData('type'),
                    'title' => $option->getData('title'),
                    'default_title' => $option->getData('default_title'),
                    'delete' => ''
                );
                foreach ($option->getSelections() as $selection) {
                    $new_bundled_item_found = (!$new_bundled_item_found && $new_bundled_item_id == (int)$selection->getProductId());
                    $bundleSelections[$option_index][] = array(
                        'product_id' => $selection->getProductId(),
                        'position' => $selection->getPosition(),
                        'is_default' => $selection->getIsDefault(),
                        'selection_price_type' => $selection->getSelectionPriceType(),
                        'selection_price_value' => $selection->getSelectionPriceValue(),
                        'selection_qty' => $selection->getSelectionQty(),
                        'selection_can_change_qty' => $selection->getSelectionCanChangeQty(),
                        'delete' => ''
                    );
                }
            }
            $i++;
        }
        if (!$new_bundled_item_found && $option_index >= 0) {
            $bundleSelections[$option_index][] = array(
                'product_id' => $new_bundled_item_id,
                'position' => 0,
                'is_default' => 1,
                'selection_price_type' => 0,
                'selection_price_value' => 0.00,
                'selection_qty' => 1,
                'selection_can_change_qty' => 0,
                'delete' => ''
            );
        }
    }

    // Set flags
    $productCheck->setCanSaveCustomOptions(true);
    $productCheck->setCanSaveBundleSelections(true);
    $productCheck->setAffectBundleProductSelections(true);

    // Register flag
    Mage::register('product', $productCheck);

    // Set option & selection data
    $productCheck->setBundleOptionsData($bundleOptions);
    $productCheck->setBundleSelectionsData($bundleSelections);

    // Save changes
    $productCheck->save();

    // Success
    return true;
}
0

I reckon the bundle selection data needs an option_id as well. Check the following code, it may help you: https://gist.github.com/tim-bezhashvyly/4245048 Also another one from stack: https://stackoverflow.com/questions/7763296/magento-how-to-load-product-along-its-all-data-as-it-is-used-in-admin

  • I tried specifying the option_id in the $bundleSelections array and it did not help. See my updated question with what i have tried to do. – Latheesan Sep 22 '15 at 12:33
  • Could you try adding a try / catch to your code as well as set the PHP display errors to yes for everything (including warnings). Another good thing would be to add Mage::log between each lines of your code to see if your code ever fail. – Raphael at Digital Pianism Sep 22 '15 at 12:46

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.