Skip navigation
Help

hook_uc_add_to_cart

7 uc_cart.api.php hook_uc_add_to_cart($nid, $qty, $data)

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

$nid: The node ID of the product.

$qty: The quantity being added.

$data: The data array, including attributes and model number adjustments.

Return value

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

Related topics

3 functions implement hook_uc_add_to_cart()

1 invocation of hook_uc_add_to_cart()

File

ubercart/uc_cart/uc_cart.api.php, line 47
Hooks provided by the Cart module.

Code

function hook_uc_add_to_cart($nid, $qty, $data) {
  if ($qty > 1) {
    $result[] = array(
      'success' => FALSE, 
      'message' => t('Sorry, you can only add one of those at a time.'),
    );
  }
  return $result;
}