gpt4 book ai didi

php - 以编程方式更新 bundle 产品 magento?

转载 作者:搜寻专家 更新时间:2023-10-31 21:04:53 24 4
gpt4 key购买 nike

我在 magento 中创建了一个脚本,用于创建 bundle 产品并且运行良好。但我还想用新的精选产品更新创建的 bundle 产品。这是我的代码,但无法正常工作:

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).'
';导出; $bundleProduct->保存();}

但是我没有添加产品项目,而是删除了我之前的选项。

最佳答案

研究了一段时间,我发现它是certainly common构建一些产品信息数组并从这些数组“手动构建” bundle 产品,就像您所做的那样。

这很令人惊讶,而且看起来并不复杂,所以是时候检查一下 Magento 在使用标准 Magento 管理面板时如何处理更新了。事实上,Magento 实际上使用了相同的技术来构建数据数组。您可以编辑 bundle 产品,启动 Web 控制台,并在保存时查看发布的数据。


Magento bundle 产品由称为选项的组组成,其中包含称为选择的产品。

要使用新选择更新一些 bundle 产品选项,您可以像这样创建一个数组:

$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 管理面板中更新 bundle 产品时发布的内容的副本。

然后您可以使用该数据构建新的选择,如 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');

也很有可能对其进行调整以添加 bundle 选项。我建议查看上面评论中提到的文件。

关于php - 以编程方式更新 bundle 产品 magento?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34283345/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com