以编程方式添加的捆绑产品未显示在前端
我正在尝试从 PHP 脚本将捆绑产品插入 Magento 数据库。有问题的版本是社区 1.5.1.0。
我尝试了问题“使用简单商品的 SKU/ID 以编程方式在 Magento 中添加捆绑产品"。插入的产品在管理部分中显示得很好 - 我可以编辑它们,添加新选项和选择等。但是,无论我尝试什么,它们根本不会显示在 Magento 前端中 - 例如重建索引或重新保存他们来自后端。通过管理界面添加捆绑包效果很好。
经过一番数据库挖掘后,我注意到使用我的脚本时,catalog_product_index_price
和 catalog_product_index_price_bundle_idx
表中没有必要的条目,而通过后端添加捆绑包会更新索引正常。就这些表而言,重新索引只是忽略添加的捆绑产品。
我仔细研究了 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();
*/
?>
I am trying to insert bundled products to the Magento database from a PHP script. The version in question is Community 1.5.1.0.
I tried the method described in the question "Programmatically add Bundle Products in Magento, using the SKU / ID of Simple Items". The inserted products show up nicely in the administration section -- I can edit them, add new options and selections etc. However, they are not showing up at all in the Magento frontend no matter what I try - e.g. rebuilding indexes or re-saving them from the back-end. Adding bundles through the administration interface works fine.
After some digging through the database, I noticed there are no necessary entries in the catalog_product_index_price
and catalog_product_index_price_bundle_idx
tables when using my script, while adding the bundle through the back-end updates the indexes normally. Re-indexing simply ignores the added bundle product as far as those tables are concerned.
I dug through the Magento source files and can't find any hints on what I'm doing wrong. All caches are disabled, selections are in stock, and I tried to include all data I dug up while studying the POST request Magento sends while inserting the product in the back-end.
Here's the complete script I use for testing, along with some desperate attempts commented out at the bottom:
$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();
*/
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请尝试使用以下代码&看看会发生什么:-
希望有帮助。
Please try using the following code & see what happens:-
Hope it helps.
我尝试过使用你的代码,但它似乎在 Magento 1.7.0.2 中不起作用。显然该产品无法保存。
我所做的是添加以下几行:
就在这些行之前:
这似乎解决了无法保存文件的问题。
I have tried using your code, but it did not seem to work in Magento 1.7.0.2. Apparently the product could not be saved.
What I did was added the following lines:
Just before the lines:
This seemed to fix the issue of not being able to save the file.