Question

I am doing a project which is add Magento products programmatically. Here is the code segment

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;
}

Here comes the problem: after executing the code, I went back to magento manage products, the product has been created, but some of the "store view" attributes are empty! I went into the database and find out that all the attributes have values.

Does anybody have any idea how to make the attributs to show? Thanks a lot!

Was it helpful?

Solution

Set your Store to Admin before adding products.

Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top