Hello,

I want to know if is possible to update the value of the stock of a product from a custom module. there is some public function or hook to do this task? if not, is safe to update the values directly to the field_data_commerce_stock database table?

thanks in advance.

Comments

guy_schneerson’s picture

Hi you can update the commerce_stock field directly as long as you do it using Drupal you should not update the database directly (this is not only for stock and is generally a bad practice)

cigotete’s picture

Hi, thanks for your reply and your guidance.

$product->commerce_stock['und'][0]['value']
r_morgan’s picture

Recently I encountered the same issue, and wanted to share the solution I came up with.

$product = commerce_product_load_by_sku($form_state['values']['sku']);
$product->commerce_stock[$product->language][0]['value'] += $form_state['values']['amount'];
commerce_product_save($product);

The commerce_product_load_by_sku could be replaced with either commerce_product_load or commerce_product_load_multiple

guy_schneerson’s picture

Issue summary: View changes
Status: Active » Closed (works as designed)

A couple of notes:

  • This issue has come up in rules integration and may be a new feature soon.
  • You should be able to call commerce_ss_stock_adjust()

closing due to lack of activity, re open if still relevant.