循环创建捆绑产品
Creation of bundle products in a loop
我有一个循环以编程方式创建多个捆绑产品。新捆绑包的创建工作正常,但如果我有一个现有的捆绑包并尝试 add/remove 选项,它会失败。
如果我第一次 运行 我的导入代码,就会创建包。第二次,所有选项都将被删除,只有数组的最后一个包有其选项设置。
$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,那么捆绑包是 created/updated 正确的。所以问题一定是在单个请求中循环执行。
最后,我自己发现了。这部分贴出的代码:
// 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();
}
我有一个循环以编程方式创建多个捆绑产品。新捆绑包的创建工作正常,但如果我有一个现有的捆绑包并尝试 add/remove 选项,它会失败。
如果我第一次 运行 我的导入代码,就会创建包。第二次,所有选项都将被删除,只有数组的最后一个包有其选项设置。
$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,那么捆绑包是 created/updated 正确的。所以问题一定是在单个请求中循环执行。
最后,我自己发现了。这部分贴出的代码:
// 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();
}