以编程方式创建Magento产品

Cha*_*Dou 3 magento magento-1.6

我正在做一个以编程方式添加Magento产品的项目.这是代码段

try{
    //create new product
    $newProduct = new Mage_Catalog_Model_Product();
    $newProduct->setAttributeSetId(9)
               ->setTypeId('simple')
               ->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH)
               ->setTaxClassId(2)
               ->setCreatedAt(strtotime('now'))
               ->setName($data[0])
               ->setSku($data[1])
               ->setWeight($data[2])
               ->setStatus($data[3])
               ->setPrice($data[4])
               ->setCategoryIds(explode(',',$data[5]))
               ->setWebsiteIds(explode(',',$data[6]))
               ->setDescription($data[7])
               ->setShortDescription($data[8])
                               ....
               ->setFreeGroundShipping($data[18])
               ->setMetaTitle($data[19])
               ->setMetaKeyword($data[20])
               ->setMetaDescription($data[21])
               ->setStockData(array(
                                     'manage_stock'=>0,
                                     'min_sale_qty'=>$data[22],
                                     'max_sale_qty'=>$data[23]))
               ->setSetupFee($data[24])
               ->setsetupCost($data[25]);
    $newProduct->save();                
}catch(Exception $e){
     $result['status'] = 3;
     $result['message'] = 'There is an ERROR happened! NOT ALL products are created! Error:'.$e->getMessage();
     echo json_encode($result);
     return;
}
Run Code Online (Sandbox Code Playgroud)

问题出在这里:执行代码后,我回到magento管理产品,产品已经创建,但是一些"商店视图"属性是空的!我进入数据库,发现所有属性都有值.

有没有人知道如何制作出场景?非常感谢!

Ben*_*uby 8

在添加产品之前将商店设置为管理员.

Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
Run Code Online (Sandbox Code Playgroud)