我有一个捆绑产品,我想将一个新的捆绑项添加到特定选项中,所以我尝试了以下内容:
// Load existing bundled product
$productCheck = Mage::getModel('catalog/product')->load($bundled_product_id);
// Get option data
$options = $productCheck->getTypeInstance(true)->getOptionsCollection($productCheck);
// Set selection data
foreach ($options as $option) {
if ($option->getTitle() == $data['bundled_title']) {
$selection = new Mage_Bundle_Model_Selection();
$selection->addData(array(
'entity_id' => $productCheck->getId(),
'product_id' => $new_bundled_item_id,
'selection_qty' => 1,
'selection_can_change_qty' => 0,
'position' => 0,
'is_default' => 1,
'selection_id' => '',
'selection_price_type' => 0,
'selection_price_value' => 0.0,
'option_id' => $option->getId(),
'delete' => ''
));
$option->setStoreId($data['store_id']);
$option->addSelection($selection);
$option->save();
$selection->save();
}
}
$productCheck->save();
这不会引发错误,同时,捆绑产品似乎没有任何变化。新项目未被添加。
有什么想法吗?我已经检查过; $new_bundled_item_id
正确且选项标题匹配。
我尝试解决这个问题:http://pastebin.com/hQbxQHak - 当代码运行时,它似乎创建了一个新的空选项,但我的第二个选择确实似乎已添加。它看起来像这样:
这不太对,但如果我再次重新运行脚本,就会发生这种情况:
我不确定发生了什么,有人能看到我的代码有什么问题吗?或者我完全错了吗?
答案 0 :(得分:0)
我已经解决了,就像这样:
public function syncBundledProduct($data)
{
// Check if bundled product already exists
$bundled_product_id = Mage::getModel("catalog/product")->getIdBySku($data['sku']);
// Load new bundled item
$new_bundled_item_id = (int)Mage::getModel("catalog/product")->getIdBySku($data['bundled_sku']);
if (!$new_bundled_item_id)
throw new Exception('Product with sku '. $data['bundled_sku'] .' does not exists');
// Bundled product already exists
if ($bundled_product_id)
{
// Load existing bundled product
$productCheck = Mage::getModel('catalog/product')->load($bundled_product_id);
$productCheck->setName($data['name']);
$productCheck->setDescription($data['description']);
$productCheck->setShortDescription($data['short_description']);
$productCheck->setMetaDescription($data['meta_description']);
$productCheck->setPrice($data['price']);
}
else
{
// Create new bundled product
$productCheck = Mage::getModel('catalog/product');
$productCheck->setData(array(
'sku_type' => 0,
'sku' => $data['sku'],
'name' => $data['name'],
'description' => $data['description'],
'short_description' => $data['short_description'],
'meta_description' => $data['meta_description'],
'type_id' => 'bundle',
'tax_class_id' => 2,
'attribute_set_id' => 4,
'weight_type' => 0,
'visibility' => 4,
'price_type' => 1,
'price' => $data['price'],
'price_view' => 0,
'status' => 1,
'created_at' => strtotime('now'),
'store_id' => $data['store_id'],
'website_ids' => Mage::getModel('core/store')->load($data['store_id'])->getWebsiteId()
));
}
// Load option & selection data
$productCheck->getTypeInstance(true)->setStoreFilter($productCheck->getStoreId(), $productCheck);
$optionCollection = $productCheck->getTypeInstance(true)->getOptionsCollection($productCheck);
$selectionCollection = $productCheck->getTypeInstance(true)->getSelectionsCollection(
$productCheck->getTypeInstance(true)->getOptionsIds($productCheck), $productCheck
);
$optionCollection->appendSelections($selectionCollection);
// Init raw option & selection data
$bundleOptions = array();
$bundleSelections = array();
// Set raw option & selection data
if (!$optionCollection->count()) {
$bundleOptions = array(
0 => array(
'title' => $data['bundled_title'],
'default_title' => $data['bundled_title'],
'option_id' => '',
'delete' => '',
'type' => 'checkbox',
'required' => '1',
'position' => '1'
)
);
$bundleSelections[0][0] = array(
'product_id' => $new_bundled_item_id,
'delete' => '',
'selection_price_value' => 0.00,
'selection_price_type' => 0,
'selection_qty' => 1,
'selection_can_change_qty' => 0,
'position' => 0,
'is_default' => 1
);
} else {
$new_bundled_item_found = false;
$i = 0;
$option_index = null;
foreach ($optionCollection as $option) {
if ($option->getData('title') && $option->getData('title') == $data['bundled_title']) {
$option_index = $i;
$bundleOptions[$option_index] = array(
'option_id' => $option->getOptionId(),
'required' => $option->getData('required'),
'position' => $option->getData('position'),
'type' => $option->getData('type'),
'title' => $option->getData('title'),
'default_title' => $option->getData('default_title'),
'delete' => ''
);
foreach ($option->getSelections() as $selection) {
$new_bundled_item_found = (!$new_bundled_item_found && $new_bundled_item_id == (int)$selection->getProductId());
$bundleSelections[$option_index][] = array(
'product_id' => $selection->getProductId(),
'position' => $selection->getPosition(),
'is_default' => $selection->getIsDefault(),
'selection_price_type' => $selection->getSelectionPriceType(),
'selection_price_value' => $selection->getSelectionPriceValue(),
'selection_qty' => $selection->getSelectionQty(),
'selection_can_change_qty' => $selection->getSelectionCanChangeQty(),
'delete' => ''
);
}
}
$i++;
}
if (!$new_bundled_item_found && $option_index >= 0) {
$bundleSelections[$option_index][] = array(
'product_id' => $new_bundled_item_id,
'position' => 0,
'is_default' => 1,
'selection_price_type' => 0,
'selection_price_value' => 0.00,
'selection_qty' => 1,
'selection_can_change_qty' => 0,
'delete' => ''
);
}
}
// Set flags
$productCheck->setCanSaveCustomOptions(true);
$productCheck->setCanSaveBundleSelections(true);
$productCheck->setAffectBundleProductSelections(true);
// Register flag
Mage::register('product', $productCheck);
// Set option & selection data
$productCheck->setBundleOptionsData($bundleOptions);
$productCheck->setBundleSelectionsData($bundleSelections);
// Save changes
$productCheck->save();
// Success
return true;
}