以编程方式创建Magento产品

3

我正在进行一个项目,需要通过编程的方式添加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;
}

问题来了:执行代码后,我回到Magento管理产品页面,产品已经创建了,但是一些“商店视图”属性为空!我进入数据库发现所有属性都有值。

请问有人知道如何让属性显示出来吗?非常感谢!


默认视图上的属性是否已填充?还是所有视图/网站的属性都为空? - Mandy Schoep
它们是默认商店视图为空的。 - CharlesDou
1个回答

8

在添加产品之前,请将您的商店设置为管理员。

Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);

嘿,那个可行!那让我整天都很烦恼!非常感谢。 - CharlesDou

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接