爲什麼這種代碼的剪切不會在magento 1.7中創建產品?在1.7版本中以編程方式創建Magento產品
class MyCompany_Catalogimporter_Model_Mysql4_Importer extends Mage_Core_Model_Mysql4_Abstract
{
public function _construct()
{
$this->_init('catalogimporter/catalogimporter', 'catalogimporter_id');
}
public function uploadAndImport(Varien_Object $object){
$product = Mage::getModel('catalog/product');
$product->setSku('pro12');
$product->setAttributeSetId(9);
$product->setTypeId('simple');
$product->setName('Product title');
$product->setCategoryIds(array(7));
$product->setWebsiteIDs(array(1));
$product->setDescription('Product Full description');
$product->setShortDescription('Product Short description');
$product->setPrice(250.00);
$product->setWeight(30.00);
$product->setVisibility(4);
$product->setStatus(1);
$product->setTaxClassId(0);
$product->setStockData(array(
'is_in_stock' => 1,
'qty' => 20
));
$product->setCreatedAt(strtotime('now'));
try {
$product->save();
}
catch (Exception $e) {
Mage::log($e->getMessage()); //check your var/log/system.log for error
}
echo "saved";
die();
}
}
有沒有錯誤!
UPDATE
如果我創建Magento的根一個簡單的文件與此下面的代碼,它的工作原理,以及:
require_once('app/Mage.php');
Mage::app();
$product = Mage::getModel('catalog/product');
$product->setSku('pro111');
$product->setAttributeSetId(9);
$product->setTypeId('simple');
$product->setName('Product title');
$product->setCategoryIds(array(7));
$product->setWebsiteIDs(array(1));
$product->setDescription('Product Full description');
$product->setShortDescription('Product Short description');
$product->setPrice(250.00);
$product->setWeight(30.00);
$product->setVisibility(4);
$product->setStatus(1);
$product->setTaxClassId(0);
$product->setStockData(array(
'is_in_stock' => 1,
'qty' => 20
));
$product->setCreatedAt(strtotime('now'));
$product->save();
爲什麼?!?!?
感謝
看起來很好,你在哪裏執行這個?它是在一個模塊中,還是獨立的腳本?你可以發佈完整的腳本嗎? – Andrew 2013-04-30 08:53:20
嗨安德魯感謝您的回覆。該腳本位於模塊內。該代碼正確執行沒有任何錯誤,我不明白爲什麼它不工作。 – Michelangelo 2013-04-30 09:52:07