我正在编写一个脚本,用于在我的目录中创建或更新产品 当需要创建产品时,该脚本可以正常工作,但当数据库中已存在产品时,它会失败(给我多次)以下消息:
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 错误(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] :. 第二个参数应该是...中的数组或对象。
我一直在查看邮件中引用的方法,但我找不到脚本失败的原因。
该脚本首先尝试使用以下命令加载产品:
$product = Mage::getModel('catalog/product')->loadByAttribute('sku', $sku);
然后测试是否使用简单的if(!$product) { //creation }
检索产品
if语句后面的所有代码都被共享用于创建或更新,并且包含对产品对象的setter调用。
以下是我使用的代码:
$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();
也许有人已经面临同样的问题 欢迎任何帮助!谢谢。
答案 0 :(得分:23)
有可能在您的“设置者调用”中,您尝试设置无法直接在$ product上设置的内容。它甚至可能是“setTeinte”,因为我不确定那是什么设置。但由于我们无法看到您的所有代码,因此有点难以说明,因此在我指导时,请查看下面的代码,该代码直接在产品上设置一些信息,然后是库存水平。因此,它说明了如何设置某些数据。我希望它有所帮助。
$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++;
}
答案 1 :(得分:20)
在保存产品之前添加Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
可解决错误。下面的示例代码更新了产品的成本。
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$productId = 160;
$newCost = 80;
$product = Mage::getModel('catalog/product')->load($productId);
$product->setCost($newCost)->save();
答案 2 :(得分:0)
使用Magento API轻松实现 也可以使用方法......
例如
$data = array('qty'=>1, 'is_in_stock'=>1)
$stockModel = new Mage_CatalogInventory_Model_Stock_Item_Api;
$stockModel->update($product_id, $data);
也可以设置管理模式
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
答案 3 :(得分:0)
//here what i use in codeigniter
function updateProducts(){
$params = array('name' => 'adminhtml'); // frontend or adminhtml
$this->load->library('magento', $params);
error_reporting(E_ALL | E_STRICT);
//$mageFilename = 'app/Mage.php';
//require_once $mageFilename;
Mage::setIsDeveloperMode(true);
umask(0);
Mage::app();
Mage::app()->setCurrentStore(Mage::getModel('core/store')->load(Mage_Core_Model_App::ADMIN_STORE_ID));
$obj = new stdClass();
$obj->Sku = '25484684';
$obj->Name = 'test product 2';
$obj->SalePrice = 55;
$obj->LongDescription = 'test product long decription.test product long decription.test product long decription.';
$obj->Description = 'short descrption';
$res = $this->updateMagentoProduct($obj);
//dumb($res);
}
function updateMagentoProduct($XMLproduct){
$SKU = (string)$XMLproduct->Sku;
$product = Mage::getModel('catalog/product')->loadByAttribute('sku',$SKU);
if (!$product) {//insert new product
$product = Mage::getModel('catalog/product');
$product->setSku($SKU);
}
//$product = new Mage_Catalog_Model_Product();
// Build the product
$product->setAttributeSetId(4); // 4 means Default AttributeSet
$product->setTypeId('simple');
$product->setName((string)$XMLproduct->Name);
$product->setCategoryIds(array(2,3,4,5,6,7));
$product->setWebsiteIDs(array(1)); # Website id, 1 is default
//$product->setWebsiteIds(array(Mage::app()->getStore(true)->getWebsite()->getId()));
//$product->setWebsiteIDs(array(1)); # Website id, my is 1 (default frontend)
$product->setDescription((string)$XMLproduct->LongDescription);
$product->setShortDescription((string)$XMLproduct->Description);
$product->setPrice((real)$XMLproduct->SalePrice);
# Custom created and assigned attributes
//$product->setHeight('my_custom_attribute1_val');
//$product->setWidth('my_custom_attribute2_val');
//$product->setDepth('my_custom_attribute3_val');
//$product->setType('my_custom_attribute4_val');
//Default Magento attribute
$product->setWeight(1.0);
$product->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH);
$product->setStatus(1);
$product->setTaxClassId(0); # My default tax class
/*$product->setStockData(array(
'is_in_stock' => 1,
'qty' => 99999
));*/
$product->setCreatedAt(strtotime('now'));
try {
$product->save();
$productId = $product->getId();
$stockItem =Mage::getModel('cataloginventory/stock_item')->loadByProduct($productId);
$stockItemId = $stockItem->getId();
$stockItem->setData('manage_stock', 1);
$stockItem->setData('qty', 99999);//(integer)$XMLproduct->QtyInStock
$stockItem->save();
echo '<h5>'.$SKU," Updated: Name: '",(string)$XMLproduct->Name,"', Price: ",(real)$XMLproduct->SalePrice,", Stock level: ",PHP_EOL.'</h5>';
}
catch (Exception $ex) {
//Handle the error
echo '<h5>'.$ex->getMessage().'</h5>';
}
// assign product to the default website
return $product->save();
}
答案 4 :(得分:0)
<?php
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$product = Mage::getModel('catalog/product');
$product
// ->setStoreId(1) //you can set data in store scope
->setWebsiteIds(array(1)) //website ID the product is assigned to, as an array
->setAttributeSetId(9) //ID of a attribute set named 'default'
->setTypeId('simple') //product type
->setCreatedAt(strtotime('now')) //product creation time
// ->setUpdatedAt(strtotime('now')) //product update time
->setSku('testsku61') //SKU
->setName('test product21') //product name
->setWeight(4.0000)
->setStatus(1) //product status (1 - enabled, 2 - disabled)
->setTaxClassId(4) //tax class (0 - none, 1 - default, 2 - taxable, 4 - shipping)
->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH) //catalog and search visibility
->setManufacturer(28) //manufacturer id
->setColor(24)
->setNewsFromDate('06/26/2014') //product set as new from
->setNewsToDate('06/30/2014') //product set as new to
->setCountryOfManufacture('AF') //country of manufacture (2-letter country code)
->setPrice(11.22) //price in form 11.22
->setCost(22.33) //price in form 11.22
->setSpecialPrice(00.44) //special price in form 11.22
->setSpecialFromDate('06/1/2014') //special price from (MM-DD-YYYY)
->setSpecialToDate('06/30/2014') //special price to (MM-DD-YYYY)
->setMsrpEnabled(1) //enable MAP
->setMsrpDisplayActualPriceType(1) //display actual price (1 - on gesture, 2 - in cart, 3 - before order confirmation, 4 - use config)
->setMsrp(99.99) //Manufacturer's Suggested Retail Price
->setMetaTitle('test meta title 2')
->setMetaKeyword('test meta keyword 2')
->setMetaDescription('test meta description 2')
->setDescription('This is a long description')
->setShortDescription('This is a short description')
->setMediaGallery (array('images'=>array (), 'values'=>array ())) //media gallery initialization
->addImageToMediaGallery('media/catalog/product/1/0/10243-1.png', array('image','thumbnail','small_image'), false, false) //assigning image, thumb and small image to media gallery
->setStockData(array(
'use_config_manage_stock' => 0, //'Use config settings' checkbox
'manage_stock'=>1, //manage stock
'min_sale_qty'=>1, //Minimum Qty Allowed in Shopping Cart
'max_sale_qty'=>2, //Maximum Qty Allowed in Shopping Cart
'is_in_stock' => 1, //Stock Availability
'qty' => 999 //qty
)
)
->setCategoryIds(array(3, 10)); //assign product to categories
$product->save();