我试图从
PHP脚本将捆绑的产品插入Magento数据库.有问题的版本是Community 1.5.1.0.
我尝试了问题“Programmatically add Bundle Products in Magento, using the SKU / ID of Simple Items”中描述的方法.插入的产品很好地显示在管理部分 – 我可以编辑它们,添加新选项和选择等.但是,无论我尝试什么,它们都不会出现在Magento前端 – 例如重建索引或从后端重新保存它们.通过管理界面添加捆绑工作正常.
在对数据库进行一些挖掘之后,我注意到在使用我的脚本时,catalog_product_index_price和catalog_product_index_price_bundle_idx表中没有必要的条目,而通过后端添加bundle通常会更新索引.就这些表而言,重新索引只是忽略添加的捆绑产品.
我挖掘了Magento源文件,找不到任何关于我做错的提示.所有缓存都被禁用,选择有库存,我试图包括我在研究Magento在后端插入产品时发送的POST请求时挖出的所有数据.
这是我用于测试的完整脚本,以及在底部注释的一些绝望尝试:
$magentoPath = '/home/nikola/bin/magento-1.5/';
require_once($magentoPath . 'includes/config.php');
require_once($magentoPath . 'app/Mage.php');
$storeID = 1;
$websiteIDs = array(1);
$mageObj = Mage::app()->setCurrentStore($storeID);
$product = Mage::getModel('catalog/product');
$cats = array("210");
$p = array(
'sku_type' => 0,
'sku' => 687,
'name' => "BarProduct",
'description' => 'Foo',
'short_description' => 'Bar',
'type_id' => 'bundle',
'attribute_set_id' => 4,
'weight_type' => 0,
'visibility' => 4,
'price_type' => 0,
'price_view' => 0,
'status' => 1,
'created_at' => strtotime('now'),
'category_ids' => $cats,
'store_id' => $storeID,
'website_ids' => $websiteIDs
);
$product->setData($p);
$product->setCanSaveBundleSelections(true);
$product->setCanSaveCustomOptions(true);
Mage::register('product', $product);
Mage::register('current_product', $product);
$optionRawData = array();
$selectionRawData = array();
$optionRawData[0] = array(
'required' => 1,
'option_id' => '',
'position' => 0,
'type' => 'select',
'title' => 'FooOption',
'default_title' => 'FooOption',
'delete' => ''
);
$selectionRawData[0] = array();
$selectionRawData[0][] = array(
'product_id' => 1810,
'position' => 0,
'is_default' => true,
'selection_id' => '',
'option_id' => '',
'selection_price_type' => 0,
'selection_price_value' => 0.0,
'selection_qty' => 1,
'selection_can_change_qty' => 1,
'delete' => ''
);
$product->setBundleOptionsData($optionRawData);
$product->setBundleSelectionsData($selectionRawData);
$product->save();
/*
$stockItem = Mage::getModel('cataloginventory/stock_item');
$stockItem->loadByProduct($product->getId());
if (!$stockItem->getId()) {
$stockItem->setProductId($product->getId())->setStockId(1);
}
$stockItem->setData('is_in_stock', 1);
$stockItem->save();
$pi = Mage::getSingleton('bundle/price_index');
$pi->addPriceIndexToProduct($product);
$pi->save();
*/
?>