function hook_uc_add_to_cart in Ubercart 8.4
Same name and namespace in other branches
- 7.3 uc_cart/uc_cart.api.php \hook_uc_add_to_cart()
Performs extra processing when an item is added to the shopping cart.
Some modules need to be able to hook into the process of adding items to a cart. For example, an inventory system may need to check stock levels and prevent an out of stock item from being added to a customer's cart. This hook lets developers squeeze right in at the end of the process after the product information is all loaded and the product is about to be added to the cart. In the event that a product should not be added to the cart, you simply have to return a failure message described below. This hook may also be used simply to perform some routine action when products are added to the cart.
Parameters
int $nid: The node ID of the product.
int $qty: The quantity being added.
array $data: The data array, including attributes and model number adjustments.
Return value
array The function can use this data to whatever purpose to see if the item can be added to the cart or not. The function should return an array containing the result array. (This is due to the nature of Drupal's \Drupal::moduleHandler()->invokeAll() function. You must return an array within an array or other module data will end up getting ignored.) At this moment, there are only three keys:
- success: TRUE or FALSE for whether the specified quantity of the item may be added to the cart or not; defaults to TRUE.
- message: The fail message to display in the event of a failure; if omitted, Ubercart will display a default fail message.
- silent: Return TRUE to suppress the display of any messages; useful when a module simply needs to do some other processing during an add to cart or fail silently.
3 functions implement hook_uc_add_to_cart()
Note: this list is generated by pattern matching, so it may include some functions that are not actually implementations of this hook.
- uc_cart_links_uc_add_to_cart in uc_cart_links/
uc_cart_links.module - Implements hook_uc_add_to_cart().
- uc_file_uc_add_to_cart in uc_file/
uc_file.module - Implements hook_uc_add_to_cart().
- uc_product_kit_uc_add_to_cart in uc_product_kit/
uc_product_kit.module - Implements hook_uc_add_to_cart().
1 invocation of hook_uc_add_to_cart()
- Cart::addItem in uc_cart/
src/ Cart.php - Adds an item to the cart.
File
- uc_cart/
uc_cart.api.php, line 52 - Hooks provided by the Cart module.
Code
function hook_uc_add_to_cart($nid, $qty, array $data) {
if ($qty > 1) {
$result[] = [
'success' => FALSE,
'message' => t('Sorry, you can only add one of those at a time.'),
];
}
return $result;
}