2015-06-10 47 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(); 
    }