Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // 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');
- // Load existing bundled product
- $productCheck = Mage::getModel('catalog/product')->load($bundled_product_id);
- // Set raw option data
- $bundleOptions = array();
- $bundleOptions = array(
- 0 => array(
- 'title' => $data['bundled_title'],
- 'default_title' => $data['bundled_title'],
- 'option_id' => '',
- 'delete' => '',
- 'type' => 'select',
- 'required' => '1',
- 'position' => '1'
- )
- );
- // Load bundle item ids
- $bundled_item_ids = array();
- $selectionCollection = $productCheck->getTypeInstance(true)->getSelectionsCollection(
- $productCheck->getTypeInstance(true)->getOptionsIds($productCheck), $productCheck
- );
- foreach($selectionCollection as $option)
- $bundled_item_ids[] = (int)$option->product_id;
- foreach ($bundled_item_ids as $bundled_item_id)
- if ($new_bundled_item_id !== $bundled_item_id)
- $bundled_item_ids[] = $new_bundled_item_id;
- // Set raw selection data
- $bundleSelections = array();
- foreach ($bundleOptions as $option_id => $bundleOption) {
- foreach ($bundled_item_ids as $selection_id => $bundled_item_id) {
- $bundleSelections[$option_id][$selection_id] = array(
- 'product_id' => $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
- );
- }
- }
- // 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();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement