You are here

function commerce_stock_increase_by_line_item in Commerce Stock 7

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).

Parameters

$line_item: A line item object.

File

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

Code

function commerce_stock_increase_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);
      }
    }
  }
}