在Magento中以编程方式更新产品 [英] Update products programmatically in Magento

查看:86
本文介绍了在Magento中以编程方式更新产品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个脚本来创建或更新目录中的产品.
当需要创建产品时,脚本可以正常工作,但是当数据库中已经存在该产品时,脚本会失败(多次),并显示以下消息:

I'm working on a script that will create or update products in my catalog.
The script works fine when the product needs to be created, but it fails when the product already exists in the database giving me (many times) the following messages :

2011-09-30T08:00:53 + 00:00 ERR(3):可恢复的错误:参数3 传递给 Mage_Catalog_Model_Resource_Eav_Mysql4_Abstract :: _ canUpdateAttribute() 必须是一个数组,给定为null,在...中调用.
2011-09-30T08:00:53 + 00:00 ERR(3):可恢复的错误:参数3传递给 Mage_Eav_Model_Entity_Abstract :: _ canUpdateAttribute()必须为 数组,给定null,在...中调用.
2011-09-30T08:00:53 + 00:00 ERR(3): 警告:array_key_exists()[function.array-key-exists]: 第二个参数应该是数组或...中的对象.

2011-09-30T08:00:53+00:00 ERR (3): Recoverable Error: Argument 3 passed to Mage_Catalog_Model_Resource_Eav_Mysql4_Abstract::_canUpdateAttribute() must be an array, null given, called in ...
2011-09-30T08:00:53+00:00 ERR (3): Recoverable Error: Argument 3 passed to Mage_Eav_Model_Entity_Abstract::_canUpdateAttribute() must be an array, null given, called in ...
2011-09-30T08:00:53+00:00 ERR (3): Warning: array_key_exists() [function.array-key-exists]: The second argument should be either an array or an object in ...

我一直在查看消息中引用的方法,但是找不到脚本失败的任何原因. 该脚本首先尝试使用:

I've been looking at the method quoted in the message, but I can't find any reason why the script fails.
The script first try to load a product using :

$product = Mage::getModel('catalog/product')->loadByAttribute('sku', $sku);

,然后测试是否使用简单的if(!$product) { //creation }检索了产品.
共享if语句之后的所有代码以进行创建或更新,这些代码由对产品对象的setter调用组成.

and then test if the product was retrieved using a simple if(!$product) { //creation }.
All the code that follow the if statement is shared for creation or update and consists of setter calls on product object.

这是我使用的代码:

$product = Mage::getModel('catalog/product')->loadByAttribute('sku', $sku);
if(!$product) {
    // the product doesn't exist yet
    $product = new Mage_Catalog_Model_Product();
    $product->setSku($sku);
    $product->setTypeId(Mage_Catalog_Model_Product_Type::TYPE_SIMPLE);
    $product->setCreatedAt(strtotime('now'));
}
// setters calls
$product->setTeinte(trim((string)$record->web_teinte));
// ...
// finally save the product
$product->save();

也许有人已经遇到了同样的问题.
欢迎任何帮助!谢谢.

Maybe someone has already faced the same problem.
Any help is welcome ! Thank you.

推荐答案

在您的"setter调用"中,您试图设置一些不能直接在$ product上设置的内容.甚至可能是"setTeinte",因为我不确定要设置什么.但是,因为我们看不到您的所有代码,所以很难说,因此,在我指导下,请看下面的代码,该代码直接在产品和库存水平上设置了一些信息.因此,它确实说明了如何设置某些数据.希望对您有帮助.

Chances are, in your "setter calls" you are trying to set something that cannot be directly set on $product. It could even be the "setTeinte" as I am not sure what that is trying to set. But as we cannot see all your code, it is a little difficult to say, so as I guide, take a look at the code below, which sets some information directly on the product and then stock levels. It does therefore, illustrate how certain data has to be set. I hope it helps.

$SKU = (string)$XMLproduct->Sku;
$product = Mage::getModel('catalog/product')->loadByAttribute('sku',$SKU);

if ($product) {
    //Product found, so we need to update it in Magento.

    $product->setName((string)$XMLproduct->Name);
    $product->setPrice((real)$XMLproduct->SalePrice);
    //$product->setDescription((string)$XMLproduct->LongDescription);
    //$product->setShortDescription((string)$XMLproduct->Description);

    $product->save();

    $productId = $product->getId();
    $stockItem =Mage::getModel('cataloginventory/stock_item')->loadByProduct($productId);
    $stockItemId = $stockItem->getId();

    $stockItem->setData('manage_stock', 1);
    $stockItem->setData('qty', (integer)$XMLproduct->QtyInStock);

    $stockItem->save();

    echo $SKU," Updated: Name: '",(string)$XMLproduct->Name,"', Price: ",(real)$XMLproduct->SalePrice,", Stock level: ",$XMLproduct->QtyInStock,PHP_EOL;

    $updated++;
} 

这篇关于在Magento中以编程方式更新产品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆