function uc_stock_adjust_product_stock in Ubercart 8.4
Same name and namespace in other branches
- 6.2 uc_stock/uc_stock.module \uc_stock_adjust_product_stock()
- 7.3 uc_stock/uc_stock.module \uc_stock_adjust_product_stock()
Decrement a product's stock.
Parameters
\Drupal\uc_order\OrderProductInterface $product: The product whose stock is being adjusted.
$key: Internal, so this function can be used as a callback of array_walk().
\Drupal\uc_order\OrderInterface $order: Order object.
3 calls to uc_stock_adjust_product_stock()
- IncrementStockAction::incrementCallback in uc_stock/
src/ Plugin/ RulesAction/ IncrementStockAction.php - Increment a product's stock.
- Products::addProductSubmit in uc_order/
src/ Plugin/ Ubercart/ OrderPane/ Products.php - Form submit callback: add a product to an order.
- Products::submitForm in uc_order/
src/ Plugin/ Ubercart/ OrderPane/ Products.php - Form submission handler.
2 string references to 'uc_stock_adjust_product_stock'
- DecrementStockAction::doExecute in uc_stock/
src/ Plugin/ RulesAction/ DecrementStockAction.php - Decreases the stock of ordered products.
- uc_stock_uc_checkout_complete in uc_stock/
uc_stock.module - Implements hook_uc_checkout_complete().
File
- uc_stock/
uc_stock.module, line 206 - Allow ubercart products to have stock levels associated with their SKU.
Code
function uc_stock_adjust_product_stock(OrderProductInterface $product, $key, OrderInterface $order) {
// Product has an active stock?
if (!uc_stock_is_active($product->model->value)) {
return;
}
// Do nothing if decrement quantity is 0.
if ($product->qty->value == 0) {
return;
}
// Adjust the product's stock.
uc_stock_adjust($product->model->value, -$product->qty->value);
// Load the new stock record.
$connection = \Drupal::database();
$stock = $connection
->query("SELECT * FROM {uc_product_stock} WHERE sku = :sku", [
':sku' => $product->model->value,
])
->fetchObject();
// Should we notify?
if (\Drupal::config('uc_stock.settings')
->get('notify') && $stock->stock <= $stock->threshold) {
$node = Node::load($product->nid->target_id);
_uc_stock_send_mail($order, $node, $stock);
}
// Save a comment about the stock level.
$comment = t('The stock level for %model_name has been @action to @qty.', [
'%model_name' => $product->model->value,
'@qty' => $stock->stock,
'@action' => -$product->qty->value <= 0 ? t('decreased') : t('increased'),
]);
uc_order_comment_save($order
->id(), 0, $comment);
}