我已经在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(); }
但不是添加产品项目,而是删除我以前的选项。
答案 0 :(得分:2)
研究了一段时间后,我发现构建一些产品信息数组certainly common并且"手动构建"来自这些数组的捆绑产品,就像你一样。
这是令人惊讶的,而且看起来并不复杂,所以是时候检查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管理面板中更新捆绑产品时POST的内容的副本。
然后,您可以使用该数据构建新选择,如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');
很有可能将其调整为添加捆绑选项。我建议您查看上面评论中提到的文件。