You are here

function hook_add_to_cart in Ubercart 6.2

Same name and namespace in other branches
  1. 5 docs/hooks.php \hook_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

$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.
7 functions implement hook_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.

theme_uc_attribute_add_to_cart in uc_attribute/uc_attribute.module
Displays the attribute selection form elements.
theme_uc_product_add_to_cart in uc_product/uc_product.module
Wraps the "Add to Cart" form in a <div>.
theme_uc_product_kit_add_to_cart in uc_product_kit/uc_product_kit.module
Wraps the "Add to Cart" form in a <div>.
uc_cart_links_add_to_cart in uc_cart_links/uc_cart_links.module
Implements hook_add_to_cart().
uc_file_add_to_cart in uc_file/uc_file.module
Implements hook_add_to_cart().

... See full list

1 invocation of hook_add_to_cart()
uc_cart_add_item in uc_cart/uc_cart.module
Adds an item to a user's cart.

File

docs/hooks.php, line 50
These are the hooks that are invoked by the Ubercart core.

Code

function hook_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;
}