Cannot setPrice, setTaxClassId and setWeight when creating a product programmatically

I am using Magento Community Edition ver. 1.6.2.0.

I am trying to add a simple product using Varien data collection model (not Api).

The product is created, but the price, ID, and tax class are not set.

I would be grateful if anyone can advise why these specific product attributes are not set.

Here is my code:

// instatiate Product
    $product = Mage::getModel('catalog/product');

    $product->setWebsiteIds(array(1));
    $product->setSku('99996');
    $product->setPrice(99.0000);
    $product->setAttributeSetId(4); 
    $product->setCategoryIds(array(2));
    $product->setType('Simple Product');
    $product->setName('Product Name6');
    $product->setDescription('The Product Description');
    $product->setShortDescription('Brief Description');
    $product->setStatus(1);    
    $product->setTaxClassId(2);
    $product->setWeight(1.0000);                
    $product->setCreatedAt(strtotime('now'));

    $product->save();

    $stockItem = Mage::getModel('cataloginventory/stock_item');
    $stockItem->loadByProduct($product->getId());    
    if (! $stockItem->getId()) {
        $stockItem->setProductId($product->getId())->setStockId(1);
    }
    $stockItem->setData('inventory_manage_stock_default', 1);
    $stockItem->setData('is_in_stock', 1);
    $stockItem->setData('qty', 10000);

    $stockItem->save();

      

Any help is greatly appreciated!

Respectfully,

James

+3


source to share


3 answers



Mistake in

$product->setType('Simple Product');

      



replace

$product->setTypeId('simple');

      

now the price is isset. Because the price in magento depends on the type of product.

+7


source


Do not use:

$product->setType('Simple Product');

      

it doesn't actually install the product ... it hasn't worked for a while ... as above, use:



  $product->setTypeId('simple')

      

This will actually set the type ... since "simple" was defined as a constant in the magic / directory ... but not "simple product" ...

0


source


As stated above, tweaking $product->setType('Simple');

will solve your problem. How did mine.

0


source







All Articles