我有一个循环来以编程方式创建多个捆绑产品。新捆绑包的创建工作正常,但如果我有现有捆绑包并尝试添加/删除选项,则会失败。
如果我第一次运行导入代码,则会创建捆绑包。第二次,所有选项都将被删除,只有数组的最后一个包有其选项设置。
$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 :(得分: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();
}