2013-02-25 127 views
1

我一直在這個例子中沿着如下:Magento programmaticaly create bundle Product創建捆綁產品,Magento的

,當我創建一個新的產品代碼工作,但是,我不能讓它當我打開一個工作產品捆綁或簡單。 [編輯]我可以加載我通過下面的代碼以編程方式創建的捆綁產品,並將產品添加到捆綁產品中。通過GUI創建的捆綁產品我無法添加產品。

任何想法如何我可以加載產品,然後捆綁與另一個產品?

這裏是我當前的代碼:

$items = array(); 
$items[] = array(
    'title'  => 'Bundle Option', 
    'option_id' => '', 
    'delete' => '', 
    'type'  => 'radio', 
    'required' => 1, 
    'position' => 0, 
); 

$selectionRawData = array(); 
$selectionRawData[0] = array(); 
$selectionRawData[0][] = array(
    'selection_id'    => '', 
    'option_id'    => '', 
    'product_id'    => 3, 
    'delete'     => '', 
    'selection_price_value' => 0, 
    'selection_price_type'  => 0, 
    'selection_qty'   => 1, 
    'selection_can_change_qty' => 0, 
    'position'     => 0, 
    'is_default'    => 1, 
); 

$selections = $selectionRawData; 

$websiteIDs = array(1); 
$cats = array(4); 

Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID); 

// load product 
// NOT WORKING 
$product = Mage::getModel('catalog/product'); 
$product->load(127); 


// new product 
/****** 
* THIS WORKS 

$p = array(
      'sku_type' => 1, 
      'sku' => '123321', 
      'name' => "BarProduct", 
      'description' => 'Foo', 
      'short_description' => 'Bar', 
      'type_id' => 'bundle', 
      'attribute_set_id' => 4, 
      'weight_type' => 0, 
      'visibility' => 4, 
      'price_type' => 1, 
      'price_view' => 0, 
      'price' => 1.99, 
      'has_options' => 1, 
      'required_options' => 1, 
      'status' => 1, 
      'created_at' => strtotime('now'), 
      'category_ids' => $cats, 
      'store_id' => 0, 
      'website_ids' => $websiteIDs, 
      'weight' => 0, 
      'weight_type' => 1, 
      'delivery_time' => '', 
      'generate_meta' => 1, 
      'tax_class_id' => 1, //19% 
    ); 
    $product->setData($p); 

*****/ 

Mage::register('product', $product); 
Mage::register('current_product', $product); 

$product->setCanSaveConfigurableAttributes(false); 
$product->setCanSaveCustomOptions(true); 

$product->setBundleOptionsData($items); 
$product->setBundleSelectionsData($selections); 
$product->setCanSaveCustomOptions(true); 
$product->setCanSaveBundleSelections(true); 
$product->setAffectBundleProductSelections(true); 

$product->save(); 

$result['product_name'] = $product->getId(); 
return $result; 

回答

0

我有一些時髦的東西與我的產品指標回事。我刪除了我的所有產品並修復了我的索引,現在這種方法可行,雖然效果不好。

所以這是我從過程中收集:

如果你想利用兩個簡單的產品和捆綁他們,你需要通過$p = array代碼來創建一個新的捆綁的捆綁產品上面,再加入兩簡單的產品。

否則,您將需要通過magento gui預製的捆綁產品。那麼您將使用$ product-> load(product_id)命令調出該產品,然後將簡單的產品添加到該產品中。

0

只要刪除這樣所有不需要的選項:

$optionsselectionsmap = array(); 
     $options = Mage::getModel("bundle/option")->getCollection()->setProductIdFilter($product->getId()); 
     foreach($options as $option){ 
      $selection = Mage::getModel("bundle/selection")->getCollection()->setOptionIdsFilter($option->getId())->getFirstItem(); 
      $tmp = array(); 
      $tmp['option_id'] = $option->getId(); 
      $tmp['selection_id'] = $selection->getData('selection_id'); 
      $optionsselectionsmap[$selection->getData('sku')] = $tmp; 
     } 

$deleteoptionids = array(); 
     foreach($optionsselectionsmap as $k=>$v) $deleteoptionids[] = $v['option_id']; 
     foreach($product->getTypeInstance(true)->getOptionsCollection($product) as $deleteitem){ 
      $deleteitem = $deleteitem->getData(); 
      $deleteitem['delete'] = 1; 
      $bundleOptions[] = $deleteitem; 
     } 
1

這是捆綁產品的重要:

$product->setData("price_type", 0); 

您必須在保存前將該屬性設置爲0(動態價格)。 當然,Reindexing是必要的。