【问题标题】:Programmatically create Magento product以编程方式创建 Magento 产品
【发布时间】:2014-01-23 19:05:30
【问题描述】:

我正在做一个以编程方式添加 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管理产品,产品已经创建,但是“商店视图”的一些属性是空的!我进入数据库,发现所有属性都有值。

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

【问题讨论】:

  • 属性是否填充在默认视图上?还是所有视图/网站的属性都为空?
  • 默认商店视图为空。

标签: magento magento-1.6


【解决方案1】:

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

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

【讨论】:

  • 嘿,这行得通!这困扰了我一整天!非常感谢。
猜你喜欢
  • 2012-11-15
  • 1970-01-01
  • 2017-01-08
  • 2023-04-04
  • 2012-05-18
  • 2013-05-05
  • 2014-03-04
  • 2011-12-11
  • 1970-01-01
相关资源
最近更新 更多