我在magento中创建了一个脚本,它创建了捆绑产品并且工作正常 . 但我想用新的选择产品更新创建的捆绑产品 . 这是我的代码,它不起作用:
public function updateBundleProduct($pro_id,$cPrdcts){
$bundleProduct = Mage::getModel('catalog/product');
$bundleProduct->load($pro_id);
$bundleProduct->setName('test product bundle bundlea');
$bundleSelections = array();
$bundleSelections = array(
'0' => array( //option ID
'0' => array(
'product_id' => '70',
'delete' => '',
'selection_price_value' => '10',
'selection_price_type' => 0,
'selection_qty' => 1,
'selection_can_change_qty' => 0,
'position' => 0,
'is_default' => 1,
'selection_id' => 71,
'option_id' => 14
),
'1' => array(
'product_id' => '84',
'delete' => '',
'selection_price_value' => '10',
'selection_price_type' => 0,
'selection_qty' => 1,
'selection_can_change_qty' => 0,
'position' => 0,
'is_default' => 1,
'selection_id' => 72,
'option_id' => 14
)
) //get all selected products list and data
);
$bundleOptions = array();
$bundleOptions = array(
'0' => array(
'title' => 'All Items2',
'option_id' => 14,
'delete' => '',
'type' => 'multi',
'required' => '1',
'position' => '1'
)
);
$bundleProduct->setData('_edit_mode', true);
//flags for saving custom options/selections
$bundleProduct->setCanSaveCustomOptions(true);
$bundleProduct->setCanSaveBundleSelections(true);
$bundleProduct->setAffectBundleProductSelections(true);
//registering a product because of Mage_Bundle_Model_Selection::_beforeSave
Mage::register('product', $bundleProduct);
//setting the bundle options and selection data
$bundleProduct->setBundleOptionsData($bundleOptions);
$bundleProduct->setBundleSelectionsData($bundleSelections);
// echo ' '.print_r($bundleProduct,true).' '; exit;
$bundleProduct->save();
}
但是,不是添加产品项目,而是删除我以前的选项 .
1 回答
研究了一段时间后,我发现构建一些产品信息数组和"manually build"这些数组的捆绑产品就像certainly common一样,就像你一样 .
这是令人惊讶的,而且看起来并不复杂,所以是时候检查Magento在使用标准Magento管理面板时如何处理更新了 . 事实上,Magento实际上使用了构建数据阵列的相同技术 . 您可以编辑捆绑产品,启动Web控制台,并查看保存时发布的数据 .
Magento捆绑产品由称为选项的组组成,其中包含称为选择的产品 .
要使用新选择更新某些捆绑产品选项,您可以创建如下数组:
阵列结构是在Magento管理面板中更新捆绑产品时POST的内容的副本 .
然后,您可以使用该数据构建新选择,如
app/code/core/Mage/Bundle/Model/Product/Type.php
中所述很有可能调整它以添加捆绑选项 . 我建议您查看上面评论中提到的文件 .