2015-06-10 89 views
0

我有一個循環以編程方式創建多個捆綁產品。新捆綁包的創建工作正常,但如果我有現有的捆綁包並嘗試添加/刪除選項,則會失敗。在循環中創建捆綁產品

如果我第一次運行我的導入代碼,則會創建捆綁包。第二次,所有選項都將被刪除,只有最後一個數組的數據包有其選項設置。

$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(); 
    } 
} 

那麼,我該如何獲得這項工作?如果我刪除選項被刪除的部分,那麼每次執行代碼時都會創建新的空白選項(這是錯誤的)。

更新:我發現,如果我運行腳本三次,每個都與另一個產品ID正確創建/更新捆綁包。所以問題必須是在單個請求中循環執行。

回答

0

最後,我自己發現了。發帖的這一部分:

// 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(); 
    } 

似乎只刪除選項的選擇部分。要刪除完整的選項,我必須這樣做:

// 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(); 
    }