I'm creating a new product like this:
$item = array(...); // associative array has attribute values
$product = Mage::getModel('catalog/product');
$product->setTypeId('simple');
$product->setSku($item['sku']);
$product->setName($item['name']);
$product->setPrice($item['price']);
$product->setStatus($item['status']);
etc...
$this->updateStock(product->getId(), $item['quantity']);
$product->save();
....
public function updateStock($id, $qty)
{
$stockItem = Mage::getModel('cataloginventory/stock_item')->loadByProduct($id);
$stockItem->setData('is_in_stock', 1);
$stockItem->setData('manage_stock', 1); // tried a couple of variants like 'inventory_manage_stock'
$stockItem->setData('qty', $qty);
$stockItem->save();
}
I'm having some problems:
[SOLVED] Price doesn't get inserted the first time
save()
is called. If I run this code again (with$product = Mage::getModel('catalog/product')->loadByAttribute('sku', $item['sku'])
), price get set around the second time. (SOLVED: I didn't set the product type. Code updated)[SOLVED]Regarding stock.. I can't get the
Use Config Settings
checkboxes to be checked (by default it is when item is created in admin) in the Inventory tab in the admin when a new product is created.
Upon investigating the database, when I save the product initially, the corresponding row in cataloginventory_stock_item
is not created. So, it looked like I needed to initialize it somehow before the initial product save. I have solves this issue, and I will update the answer later.
- [SOLVED]I tried to remedy problem 2 by setting the
manage_stock
to true, but I can't even set "Manage Stock" to "Yes".updateStock()
works for products that are already existing.
How can I solve these issues?