Magento API:添加新产品后重建索引

发布于 2024-10-05 09:42:45 字数 378 浏览 4 评论 0原文

我目前正在编写一个脚本,可以让我在 magento 中导入多个产品。

$product = Mage::getModel('catalog/product');
$product->setSku($data['sku']);
//etc etc
$product->save();

该产品被完美创建,但它不会显示在我的前端,直到我将其保存在后端(不更改任何内容!)或者我在后端重建索引。

我对相关数据库表进行了比较,以查看保存产品并将这些字段添加到导入脚本时发生的变化,但它没有任何效果。导入的产品必须没问题,因为当我通过后端手动重建索引时它会显示出来。

缓存已完全禁用。

现在我的问题是:导入产品后如何重建索引?

I am currently writing a script that lets me import multiple products in magento.

$product = Mage::getModel('catalog/product');
$product->setSku($data['sku']);
//etc etc
$product->save();

The product gets created perfectly but it won't show up in my frontend until I either save it in the backend (without changing anything!) OR I rebuild the indexes in the backend.

I did a diff on the relevant database tables to see what's changing when I save the product and added those fields to my import script, but it did not have any effect. The imported product has to be OK since it shows up when I rebuild the indexes via the backend manually.

Caching is completely disabled.

Now my question is: How can I rebuild the indexes after importing my products?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

裂开嘴轻声笑有多痛 2024-10-12 09:42:45

您可以在 Index 模块中使用这样的模型。

$processes = Mage::getSingleton('index/indexer')->getProcessesCollection();
$processes->walk('reindexAll');

由于您需要重建所有索引,因此没有应用于集合的过滤器。但是您可以通过 addFieldToFilter($field, $condition) 方法按参数集(代码、上次重新索引等)过滤索引进程列表。

小建议

在导入产品时将索引设置为手动模式会很好,这将帮助您加快导入过程,因为其中一些会观察产品保存事件,因此需要一些时间。您可以通过以下方式进行操作:

$processes = Mage::getSingleton('index/indexer')->getProcessesCollection();
$processes->walk('setMode', array(Mage_Index_Model_Process::MODE_MANUAL));
$processes->walk('save');
// Here goes your
// Importing process
// ................
$processes->walk('reindexAll');
$processes->walk('setMode', array(Mage_Index_Model_Process::MODE_REAL_TIME));
$processes->walk('save');

You can use such a model in Index module.

$processes = Mage::getSingleton('index/indexer')->getProcessesCollection();
$processes->walk('reindexAll');

Since you need to rebuild all the indexes, there is no filters aplied to collection. But you can filter index processes list by set of parameters (code, last time re-indexed, etc) via addFieldToFilter($field, $condition) method.

Small Suggestion

Would be great to set indexes to manual mode while you importing the products, it will help you to speed up the import process, because some of them observe product saving event , so it takes some time. You can do it in the following way:

$processes = Mage::getSingleton('index/indexer')->getProcessesCollection();
$processes->walk('setMode', array(Mage_Index_Model_Process::MODE_MANUAL));
$processes->walk('save');
// Here goes your
// Importing process
// ................
$processes->walk('reindexAll');
$processes->walk('setMode', array(Mage_Index_Model_Process::MODE_REAL_TIME));
$processes->walk('save');
薄荷梦 2024-10-12 09:42:45

至少有两种情况会阻止索引器在保存时重新索引产品。

一:索引属性中的“手动更新”设置,您可以在“系统”、“索引管理”下找到。如果您希望在保存时对产品建立索引,则应将其设置为“保存时更新”。

二:setIsMassupdate 产品标志,例如在 DataFlow 批量导入过程中使用,以防止在每次产品保存方法调用时触发索引器。

希望这有帮助。
问候, 亚历山德罗

There are at least two circumstances that prevent indexer to reindex a product on save.

One: the "Manual update" setting in the Indexes properties you find under System, Index Management. You should set it to "Update on Save" if you want a product to be indexed upon a save.

Two: the setIsMassupdate product flag that is used, for example, in DataFlow batch import procedures in order to prevent indexer to be triggered upon each product save method call.

Hope this helps.
Regards, Alessandro

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文