You are here

function commerce_stock_adjust in Commerce Stock 7

Adjusts a particular product SKU by a certain value. A positive integer will add to stock, a negative integer will remove from stock. Somewhat the equivalent of uc_stock_adjust().

Parameters

$product: The product for which to change the stock level.

$qty: The quantity to add to the stock level.

2 calls to commerce_stock_adjust()
commerce_stock_decrease_by_line_item in ./commerce_stock.rules.inc
If the line item is stock-enabled, subtract the sold amount in a line item from stock.
commerce_stock_increase_by_line_item in ./commerce_stock.rules.inc
If the line item is stock-enabled, add the sold amount in a line item to stock. Typically used when a line item is removed from an order (as when items are added to and removed from cart).

File

./commerce_stock.rules.inc, line 159
Rules integration for Commerce Stock.

Code

function commerce_stock_adjust($product, $qty) {
  if (!commerce_stock_product_type_enabled($product->type)) {
    return;
  }
  $wrapper = entity_metadata_wrapper('commerce_product', $product);
  $new_stock = $wrapper->commerce_stock
    ->value() + intval($qty);
  $wrapper->commerce_stock
    ->set($new_stock);
  $result = $wrapper
    ->save();
  if ($result) {
    watchdog('commerce_stock', 'Modified stock level of product %sku by %amount', array(
      '%sku' => $product->sku,
      '%amount' => $qty,
    ));
  }
  else {
    watchdog('commerce_stock', 'Failed attempt to modify stock level of product %sku by %amount', array(
      '%sku' => $product->sku,
      '%amount' => $qty,
    ), WATCHDOG_ERROR);
  }
}