简体   繁体   English

Magento,以编程方式使用SQL在脚本中向产品添加类别

[英]Magento, programmatically adding category to products in script with SQL

I am in the process of determining the best way to programmatically add Categories to Products. 我正在确定以编程方式将类别添加到产品的最佳方法。 The Categories are obtained from third party data outside of the magento install, I then parse this data and want to update the magento: "catalog_category_product" table, associating found categories to their respective products. 类别是从magento安装之外的第三方数据获得的,然后我解析这些数据并想要更新magento:“catalog_category_product”表,将找到的类别与其各自的产品相关联。

After adding a category to product association in "catalog_category_product" as test, the category did not appear on the magento front-end site. 在“catalog_category_product”中将类别添加到产品关联作为测试后,该类别未显示在magento前端站点上。 After more searching I added an entry to the "catalog_category_product_index" table, the product now shows in the proper category on the front-end site. 在进行了更多搜索之后,我在“catalog_category_product_index”表中添加了一个条目,该产品现在显示在前端站点上的正确类别中。

Is this all that is necessary? 这一切都是必要的吗? I am afraid there are additional magento event hooks tied to the Products and Categories and they may be updating additional tables that I am not aware of. 我担心还有额外的magento事件挂钩绑定到产品和类别,他们可能正在更新我不知道的其他表。

Can I add rows to these two tables with SQL without incident, or do I need to load the magento app and do this through the EAV Product and Category models? 我可以使用SQL向这两个表添加行而不会发生意外,或者我是否需要加载magento应用程序并通过EAV产品和类别模型执行此操作? Am I on the right track? 我是在正确的轨道上吗?

Update 更新

I am not actually 'importing' any new data. 我实际上并没有“导入”任何新数据。 The Products and Categories are in-place in the system already. 产品和类别已在系统中就位。 I have also not added any custom observers and am using the vanilla Products and Categories models. 我还没有添加任何自定义观察者,并且正在使用vanilla Products和Categories模型。

I am parsing the text file to get the proper values and then using plain SQL UPDATE statements. 我正在解析文本文件以获取正确的值,然后使用纯SQL UPDATE语句。

I simply want to make sure that by taking this route, the only tables I need to programmatically update are 'catalog_category_product' and 'catalog_category_product_index'. 我只是想确保通过这条路线,我需要以编程方式更新的唯一表是'catalog_category_product'和'catalog_category_product_index'。

I personally don't advise you to use plain SQL rather use the API like: 我个人不建议您使用纯SQL,而是使用API​​,如:

define('MAGENTO', realpath(dirname(__FILE__)));
require_once MAGENTO . '/app/Mage.php';
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);

// Load products 
$products = Mage::getModel('catalog/product')
            ->setStoreId(Mage_Core_Model_App::ADMIN_STORE_ID)
            ->getAll();

// Load categories
$category = Mage::getModel('catalog/category');
            ->setStoreId(Mage_Core_Model_App::ADMIN_STORE_ID);
$categories = $category->getAll();
foreach($products as $product) {
    // Get relevant category 
    $product->setCategoryIds(array($category->getId()));
    $product->save();
}

The code above is very rough example but with few tweaks should do the job. 上面的代码是非常粗略的例子,但很少有调整应该做的工作。 If you use plain SQL you risk to break your data or relations but using the API Magento should handle it right for you. 如果您使用纯SQL,则可能会破坏您的数据或关系,但使用API​​ Magento应该适合您。

How do you link your categories and products ? 如何链接您的类别和产品? Did you code something specific ? 你有没有具体的代码? If yes : Show some code. 如果是:显示一些代码。

Anyways, magento offers many ways to do what you need in STANDARD. 无论如何,magento提供了许多方法来完成STANDARD所需的操作。 Look in the Backoffice in the menu : System / Imports-Exports 查看菜单中的Backoffice:System / Imports-Exports

There are 2 differents methods in magento : magento中有2种不同的方法:

  • Dataflow profiles : it works with Models and is pretty slow (but by calling ->save() and ->load() on all your models, it ensures that any Observer you added will be called !!) 数据流配置文件:它适用于模型并且非常慢(但是通过在所有模型上调用 - > save()和 - > load(),它确保您调用的任何Observer都将被调用!!)

  • new Imports/Exports profiles : it works with pure SQL queries and is pretty fast (but it's hardcoded so ... say bye-bye to any load/save custom observers you added) 新的Imports / Exports配置文件:它适用于纯SQL查询并且非常快(但它是硬编码的,所以...再见你添加的任何加载/保存自定义观察者)

Like you said, Magento got events hooks linked to product and categories (and all other models). 就像你说的那样,Magento得到了与产品和类别(以及所有其他模型)相关的事件挂钩。 In the after save call back of a product and categories, for example, magento will reindex all the associated INDEXES like the catalog_category_index you tried to add manually.. 例如,在产品和类别的保存后回调中,magento将重新索引所有关联的INDEXES,例如您尝试手动添加的catalog_category_index。

So : 所以:

  • don't do that yourself, use magento Import/Exports profiles, Dataflow profiles or API. 不要自己这样做,使用magento导入/导出配置文件,数据流配置文件或API。
  • depending on the quantity of product/categories you have, choose the right Import to use (see above, the differences). 根据您拥有的产品/类别的数量,选择正确的导入使用(参见上面的差异)。 Dataflow is slower but uses Models .. 数据流速度较慢但使用的是模型..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM