The revisions let you track differences between multiple versions of a post.

hook_add_to_cart

Function hook_add_to_cart() in uc_cart.module:
<?php
  hook_add_to_cart
($nid, $qty, $data)
?>

Description:

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 two 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.
This only applies to items being added to the cart at this point. Things will change for Alpha 7, but for now if you have a module that also needs to check quantity from the cart view form, you must hook into the form with hook_form_alter to add a validate function for the form. The form ID for that form is uc_cart_view_form. Your function may take advantage of uc_cart_get_contents() to get the current contents of the customer's shopping cart.

Example:
<?php
function simple_inventory_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;
}
?>