You are here

function uc_stock_adjust in Ubercart 8.4

Same name and namespace in other branches
  1. 5 uc_stock/uc_stock.module \uc_stock_adjust()
  2. 6.2 uc_stock/uc_stock.module \uc_stock_adjust()
  3. 7.3 uc_stock/uc_stock.module \uc_stock_adjust()

Adjusts the product stock level by a set amount.

Parameters

string $sku: The product SKU of the stock level to adjust.

int $qty: The amount to add to or subtract from the stock level.

bool $check_active: If FALSE, don't check if stock tracking is active for this SKU.

2 calls to uc_stock_adjust()
Products::removeProduct in uc_order/src/Plugin/Ubercart/OrderPane/Products.php
Form submit callback: remove a product from an order.
uc_stock_adjust_product_stock in uc_stock/uc_stock.module
Decrement a product's stock.

File

uc_stock/uc_stock.module, line 73
Allow ubercart products to have stock levels associated with their SKU.

Code

function uc_stock_adjust($sku, $qty, $check_active = TRUE) {
  $connection = \Drupal::database();
  $stock = $connection
    ->query("SELECT active, stock FROM {uc_product_stock} WHERE sku = :sku", [
    ':sku' => $sku,
  ])
    ->fetchObject();
  if ($check_active) {
    if (!$stock || !$stock->active) {
      return;
    }
  }
  $connection
    ->update('uc_product_stock')
    ->expression('stock', 'stock + :qty', [
    ':qty' => $qty,
  ])
    ->condition('sku', $sku)
    ->execute();
  \Drupal::moduleHandler()
    ->invokeAll('uc_stock_adjusted', [
    $sku,
    $stock->stock,
    $qty,
  ]);
}