Question

Hi I am using php module to add a new product. Everything works great but this code for some reason does not set the quantity of product.

$id_product = (int)Db::getInstance()->getValue("SELECT id_product FROM `$product` WHERE reference = '$product_xml[id]'");
$p = new Product();
$p->reference = $product_xml['id'];
$p->price = (float)$price;
$p->active = 1;
$p->quantity = (int)$product_xml['count']; 
$p->minimal_quantity = 1;
$p->available_for_order=1;
$p->id_category = array(26);
$p->id_category_default = 26;
$p->name[1] = $product_xml['name'];
$p->description[1] = utf8_encode($product_xml->Description);
$p->description_short[1] = utf8_encode($product_xml->Short_Description);
$p->link_rewrite[1] = Tools::link_rewrite($product_xml['name']);
if (!isset($p->date_add) || empty($p->date_add))
$p->date_add = date('Y-m-d H:i:s');
$p->date_upd = date('Y-m-d H:i:s');
$p->save();
$id_product ? $p->updateCategories(array(26)) : $p->addToCategories(array(26));
Was it helpful?

Solution

You should also use:

StockAvailable::setQuantity($p->id, null, (int)$product_xml['count']);

Put that after your current code.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top