Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2015
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.26 KB | None | 0 0
  1. // Check if bundled product already exists
  2. $bundled_product_id = Mage::getModel("catalog/product")->getIdBySku($data['sku']);
  3.  
  4. // Load new bundled item
  5. $new_bundled_item_id = (int)Mage::getModel("catalog/product")->getIdBySku($data['bundled_sku']);
  6. if (!$new_bundled_item_id)
  7.     throw new Exception('Product with sku '. $data['bundled_sku'] .' does not exists');
  8.  
  9. // Load existing bundled product
  10. $productCheck = Mage::getModel('catalog/product')->load($bundled_product_id);
  11.  
  12. // Set raw option data
  13. $bundleOptions = array();
  14. $bundleOptions = array(
  15.     0 => array(
  16.         'title' => $data['bundled_title'],
  17.         'default_title' => $data['bundled_title'],
  18.         'option_id' => '',
  19.         'delete' => '',
  20.         'type' => 'select',
  21.         'required' => '1',
  22.         'position' => '1'
  23.     )
  24. );
  25.  
  26. // Load bundle item ids
  27. $bundled_item_ids = array();
  28. $selectionCollection = $productCheck->getTypeInstance(true)->getSelectionsCollection(
  29.     $productCheck->getTypeInstance(true)->getOptionsIds($productCheck), $productCheck
  30. );
  31. foreach($selectionCollection as $option)
  32.     $bundled_item_ids[] = (int)$option->product_id;
  33. foreach ($bundled_item_ids as $bundled_item_id)
  34.     if ($new_bundled_item_id !== $bundled_item_id)
  35.         $bundled_item_ids[] = $new_bundled_item_id;
  36.  
  37. // Set raw selection data
  38. $bundleSelections = array();
  39. foreach ($bundleOptions as $option_id => $bundleOption) {
  40.     foreach ($bundled_item_ids as $selection_id => $bundled_item_id) {
  41.         $bundleSelections[$option_id][$selection_id] = array(
  42.             'product_id' => $bundled_item_id,
  43.             'delete' => '',
  44.             'selection_price_value' => 0.00,
  45.             'selection_price_type' => 0,
  46.             'selection_qty' => 1,
  47.             'selection_can_change_qty' => 0,
  48.             'position' => 0,
  49.             'is_default' => 1
  50.         );
  51.     }
  52. }
  53.  
  54. // Set flags
  55. $productCheck->setCanSaveCustomOptions(true);
  56. $productCheck->setCanSaveBundleSelections(true);
  57. $productCheck->setAffectBundleProductSelections(true);
  58.  
  59. // Register flag
  60. Mage::register('product', $productCheck);
  61.  
  62. // Set option & selection data
  63. $productCheck->setBundleOptionsData($bundleOptions);
  64. $productCheck->setBundleSelectionsData($bundleSelections);
  65.  
  66. // Save changes
  67. $productCheck->save();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement