1

I am using this code to insert the commerce stock but it fails some how:

$cp = commerce_product_new($product_type);
$cp->is_new = TRUE;
$cp->revision_id = NULL;
$cp->uid = 1;
$cp->status = 1;
$cp->created = $cp->changed = time();
$cp->sku = $product_sku;
$cp->title = $product_title;
$cp->language = LANGUAGE_NONE;
$cp->commerce_price = array(LANGUAGE_NONE => array( 0 => array(
   'amount' => $product_price,
   'currency_code' => 'GBP',
)));
$cp->commerce_stock[LANGUAGE_NONE][0]['value'] = 10;
commerce_product_save($cp);

I checked on the documentation using:

$cp->commerce_stock[LANGUAGE_NONE][0]['value'] = 10;

should do the trick, but strangely its not updating. Am I missing anything? All the rest is working except the stock.

thank you

6
  • Have you confirmed the field you are wanting to update is "commerce_stock"?
    – joshmiller
    Feb 2 '16 at 14:07
  • yes, its commerce_stock, I checked it, strangely the stock is not updating..
    – Boby
    Feb 2 '16 at 15:51
  • 1
    Have you enabled the commerce_ss module and enabled the stock field?
    – nvahalik
    Feb 2 '16 at 20:30
  • 1
    yes both are enabled and stock field are good, as I am able to do it via the UI and the stock is updated but not by this code..
    – Boby
    Feb 3 '16 at 1:06
  • In the above example, is the product being created at all and just the stock information is missing? Or does nothing get created?
    – nvahalik
    Feb 6 '16 at 11:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.