hook_add_to_cart($nid, $qty, $data)
ubercart/docs/hooks.php, line 48
Do 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.
$nid The node ID of the product
$qty The quantity being added
$data The data array, including attributes and model number adjustments
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:
| Name | Description |
|---|---|
| Hooks | Allow modules to interact with the Drupal core. |
<?php
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;
}
?>