You are here

function commerce_ss_stock_adjust in Commerce Stock 7.2

Adjusts a particular product SKU by a certain value.

A positive number will add to stock, a negative number will remove from stock. Somewhat the equivalent of uc_stock_adjust().

Parameters

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

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

2 calls to commerce_ss_stock_adjust()
commerce_ss_decrease_by_line_item in modules/commerce_ss/commerce_ss.rules.inc
Subtracts from stock the sold amount in a line item.
commerce_ss_increase_by_line_item in modules/commerce_ss/commerce_ss.rules.inc
Adds the sold amount in a line item to stock.

File

modules/commerce_ss/commerce_ss.rules.inc, line 140
Rules integration for Commerce Simple Stock.

Code

function commerce_ss_stock_adjust($product, $qty) {
  if (!commerce_ss_product_type_enabled($product->type)) {
    return;
  }
  $wrapper = entity_metadata_wrapper('commerce_product', $product);
  $new_stock = $wrapper->commerce_stock
    ->value() + $qty;
  $wrapper->commerce_stock
    ->set($new_stock);
  $result = $wrapper
    ->save();

  // @todo should this be moved to the
  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);
  }
}