You are here

function commerce_stock_decrease_by_line_item in Commerce Stock 7

If the line item is stock-enabled, subtract the sold amount in a line item from stock.

Parameters

$line_item: A line item object.

1 call to commerce_stock_decrease_by_line_item()
commerce_stock_adjust_by_line_item in ./commerce_stock.rules.inc
For backward compatibility, keep a function by the old name.

File

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

Code

function commerce_stock_decrease_by_line_item($line_item) {
  if (in_array($line_item->type, commerce_product_line_item_types())) {

    // The product SKU that will have its stock level adjusted.
    $sku = $line_item->line_item_label;
    $product = commerce_product_load_by_sku($sku);
    $product_wrapper = entity_metadata_wrapper('commerce_product', $product);
    if (commerce_stock_product_type_enabled($product->type)) {
      if (!(commerce_stock_product_type_override_enabled($product->type) && isset($product_wrapper->commerce_stock_override) && $product_wrapper->commerce_stock_override
        ->value() == 1)) {
        $qty = (int) $line_item->quantity;

        // Subtract the sold amount from the available stock level.
        commerce_stock_adjust($product, -$qty);
      }
    }
  }
}