JSweete
JSweete

Reputation: 269

Magento Programatically add products and set stock at the same time

Hi i hope someone can help me. Ive created a automatic product importer script, and it works adding the products and categories programatically. The only issue I'm having is it isn't setting the stock , when you go into the Backend of the CMS the products are listed with manage stock = no and so its not setting a quantity. Im using magento 1.9.0.1

Can someone point out what I'm missing here please, this is the code i have...

$newProduct->setAttributeSetId(4)
           ->setTypeId('simple')
           ->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH)
           ->setTaxClassId(2)
           ->setCreatedAt(strtotime('now'))
           ->setName($product["ItemTitle"])
           ->setEannumber($product["EANNumber"])
           ->setWeight($product["Weight"])
           ->setStatus(1)
           ->setPrice($product["Price"])
           ->setCategoryIds($CategoryIDs)
           ->setWebsiteIds(array(1))
           ->setSku($product["ItemCode"])
           ->setDistributor('entatech')
           ->setDescription($productDescription2)
           ->setShortDescription($productDescriptionone)
           ->setMetaTitle($product["ItemTitle"])
           ->setMetaKeyword($prodKeywords)
           ->setMetaDescription($product["description2"]);

  $newProduct->setCreatedAt(strtotime('now'));
  $newProduct->setStockData(array( 
        'use_config_manage_stock' => 1,
        'is_in_stock' => 1, 
        'qty' => $product["Stock"],
        'manage_stock' => 1,
        'use_config_notify_stock_qty' => 0
    )); 


  $newProduct->getResource()->save($newProduct);  

Upvotes: 3

Views: 9066

Answers (2)

Deepak Mankotia
Deepak Mankotia

Reputation: 4564

You can also do this by following method :

$newProduct->setStockData(array(
                       'use_config_manage_stock' => 0, //'Use config settings' checkbox
                       'manage_stock'=>1, //manage stock
                       'min_sale_qty'=>1, //Minimum Qty Allowed in Shopping Cart
                       'max_sale_qty'=>2, //Maximum Qty Allowed in Shopping Cart
                       'is_in_stock' => 1, //Stock Availability
                       'qty' => 999 //qty
                   )
    );

Upvotes: 1

MagePal Extensions
MagePal Extensions

Reputation: 17656

Take a look @ Set default product values when adding new product in Magento 1.7

$stockItem = Mage::getModel('cataloginventory/stock_item');
$stockItem->assignProduct($newProduct);
$stockItem->setData('is_in_stock', 1);
$stockItem->setData('qty', 1);
$product->setStockItem($stockItem);

Upvotes: 7

Related Questions