You are here

function hook_commerce_cart_attributes_refresh_alter in Commerce Core 7

Allows modules to add arbitrary AJAX commands to the array returned from the Add to Cart form attributes refresh.

When a product selection widget's value is changed, whether it is a product select list or a product attribute field widget, the Add to Cart form gets an AJAX refresh. The form will be rebuilt using the new form state and the AJAX callback of the element that was changed will be called. For this form it is commerce_cart_add_to_cart_form_attributes_refresh().

The cart form's particular AJAX refresh function returns an array of AJAX commands that perform HTML replacement on the page. However, other modules may want to interact with the refreshed form. They can use this hook to add additional items to the commands array, which is passed to the hook by reference. Note that the form array and form state cannot be altered, just the array of commands.

Parameters

&$commands: The array of AJAX commands used to refresh the cart form with updated form elements and to replace product fields rendered on the page to match the currently selected product.

$form: The rebuilt form array.

$form_state: The form state array from the form.

See also

commerce_cart_add_to_cart_form_attributes_refresh()

1 invocation of hook_commerce_cart_attributes_refresh_alter()
commerce_cart_add_to_cart_form_attributes_refresh in modules/cart/commerce_cart.module
Ajax callback: returns AJAX commands when an attribute widget is changed.

File

modules/cart/commerce_cart.api.php, line 202
Hooks provided by the Cart module.

Code

function hook_commerce_cart_attributes_refresh_alter(&$commands, $form, $form_state) {

  // Display an alert message showing the new default product ID.
  $commands[] = ajax_command_alert(t('Now defaulted to product @product_id.', array(
    '@product_id' => $form['product_id']['#value'],
  )));
}