我在magento中创建了一个脚本,用于创建捆绑产品,它运行良好。但我也想用新的精选产品更新创建的捆绑产品。这是我的代码,它不起作用:
公共函数updateBundleProduct($pro_id,$cPrdcts){$bundleProduct=Mage::getModel('catalog/product');$bundleProduct->load($pro_id);$bundleProduct->setName("测试产品捆绑包bundlea");$bundleSelections=array();$bundleSelections=数组("0"=>数组(//选项ID"0"=>数组('product_id'=>'70','delete'=>'','selection_price_value'=>'10','selection_price_type'=>0,'选择数量'=>1,'selection_can_change_qty'=>0,'位置'=>0,'is_default'=>1,'selection_id'=>71,'option_id'=>14),'1'=>数组('product_id'=>'84','delete'=>'','selection_price_value'=>'10','selection_price_type'=>0,'选择数量'=>1,'selection_can_change_qty'=>0,'位置'=>0,'is_default'=>1,'selection_id'=>72,'option_id'=>14))//获取所有选定的产品列表和数据);$bundleOptions=array();$bundleOptions=数组("0"=>数组('title'=>'所有项目2','option_id'=>14,'delete'=>'','type'=>'multi',"必需"=>"1","位置"=>"1"));$bundleProduct->setData('_edit_mode',true);//用于保存自定义选项/选择的标志$bundleProduct->setCanSaveCustomOptions(true);$bundleProduct->setCanSaveBundleSelections(true);$bundleProduct->setAffectBundleProductSelections(true);//由于Mage_Bundle_Model_Selection::_beforeSave而注册产品Mage::register('product',$bundleProduct);//设置捆绑选项和选择数据$bundleProduct->setBundleOptionsData($bundleOptions);$bundleProduct->setBundleSelectionsData($bundleSelections);//echo''.print_r($bundleProduct,true).'';出口$bundleProduct->save();}
但它并没有添加产品项目,而是删除了我以前的选项。
研究了一段时间后,我发现构建一些产品信息数组并从这些数组中"手动构建"捆绑产品是很常见的,就像你所做的那样
这很令人惊讶,而且似乎很简单,所以是时候检查一下Magento在使用标准Magento管理面板时如何处理更新了。事实上,Magento实际上使用了同样的技术来构建一个数据数组。您可以编辑捆绑产品,启动Web控制台,并在保存时查看张贴的数据。
Magento捆绑产品由称为选项的组组成,这些组包含称为选择的产品。
要用新的选择更新一些捆绑产品选项,您可以创建这样的数组:
$selectionData = array(
//'selection_id' => 'not set, so Magento will create a new one',
'option_id' => $option->getId(),
'product_id' => $product->getId(),
'delete' => '',
'selection_price_value' => '0.00',
'selection_price_type' => '0',
'selection_qty' => '1.0000',
'selection_can_change_qty' => '1',
'position' => '0',
);
阵列结构是在Magento管理面板中更新捆绑产品时张贴的内容的副本。
然后,您可以使用该数据来构建新的选择,如app/code/core/Mage/Bundle/Model/Product/Type.php
中所做的那样
$resource = Mage::getResourceModel('bundle/bundle');
$bundleProduct = YOUR_BUNDLE_PRODUCT;
// app/code/core/Mage/Bundle/Block/Adminhtml/Catalog/Product/Edit/Tab/Bundle/Option.php
$optionCollection = $bundleProduct->getTypeInstance(true)->getOptionsCollection($bundleProduct);
$selectionCollection = $bundleProduct->getTypeInstance(true)->getSelectionsCollection(
$bundleProduct->getTypeInstance(true)->getOptionsIds($bundleProduct),
$bundleProduct
);
$options = $optionCollection->appendSelections($selectionCollection);
// needed because of app/code/core/Mage/Bundle/Model/Selection.php:73
Mage::register('product', $bundleProduct);
// process each option
foreach ($options as $option) {
$selections = $option->getSelections();
// process each selection
foreach ($selections as $selection) {
$usedProductIds = array();
$productCollection = YOUR_PRODUCT_COLLECTION;
foreach ($yourProductCollection as $product) {
$selectionData = array(
//'selection_id' => 'not set, so Magento will create a new one',
'option_id' => $option->getId(),
'product_id' => $product->getId(),
'delete' => '',
'selection_price_value' => '0.00',
'selection_price_type' => '0',
'selection_qty' => '1.0000',
'selection_can_change_qty' => '1',
'position' => '0',
);
// app/code/core/Mage/Bundle/Model/Product/Type.php:315
$selectionModel = Mage::getModel('bundle/selection')
->setData($selectionData)
->setOptionId($option->getId())
->setParentProductId($bundleProduct->getId());
$selectionModel->isDeleted((bool)$selectionData['delete']);
$selectionModel->save();
$selectionData['selection_id'] = $selectionModel->getSelectionId();
if ($selectionModel->getSelectionId()) {
$excludeSelectionIds[] = $selectionModel->getSelectionId();
$usedProductIds[] = $selectionModel->getProductId();
}
}
$resource->dropAllUnneededSelections($bundleProduct->getId(), $excludeSelectionIds);
$resource->saveProductRelations($bundleProduct->getId(), array_unique($usedProductIds));
}
}
Mage::unregister('product');
这是非常有可能的调整,以添加捆绑包选项。我建议查看上面评论中提到的文件。