1

We have gotten an API from our manufacturer, now we want to enable stock in these products and give them the right quantity...

We use this code:

$_product->setData('use_config_manage_stock', 0);
$_product->setData('manage_stock', 1); // should be 1 to make something   out of stock
$_product->setData('is_in_stock', 1); // is 0 or 1
$_product->setData('qty', 1234);

try {
    $_product->save();
    echo "done." . PHP_EOL;
} catch (Exception $e) {
    echo "{$e}";
}

But for some reason, stock wont be enabled on these products and the quantity is also not applied... Whats wrong with this code?!

this is how the products are normal and even after the script it stays like that http://puu.sh/di3RT/f9ecfc32ce.png

Thanks in Advance,

1 Answer 1

1

Try it like this:

$_stockItem = $_product->getStockItem();
$_stockItem->setData('use_config_manage_stock', 0);
$_stockItem->setData('manage_stock', 1); // should be 1 to make something   out of stock
$_stockItem->setData('is_in_stock', 1); // is 0 or 1
$_stockItem->setData('qty', 1234);

try {
    $_product->save();
    echo "done." . PHP_EOL;
} catch (Exception $e) {
    echo "{$e}";
}
3
  • i get ...Call to a member function setData() on null in... on the first stockItem->setData
    – xvilo
    Commented Dec 8, 2014 at 13:03
  • This is strange. it means that $_product->getStockItem(); returns null. This shouldn't happen.
    – Marius
    Commented Dec 8, 2014 at 13:25
  • Still not working....
    – xvilo
    Commented Jan 8, 2015 at 9:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.