0

I was looking for a hook that will fire after a user hits the add to cart button. The commerce api isn't very clear about this.

What I'm trying to is: Add an extra validation for a customized product (date field). So basically I want to interrupt the adding to the cart process and proceed if validation is ok, and lead the user back if not.

Thanks!

1

Seems like hook_commerce_cart_product_add is what you're after. I couldn't find any examples for preventing the line item being added to the cart but at the very least you should be able to use drupal_goto to interupt the process. You might also want to look at the Rules events\actions provided by Commerce.

|improve this answer|||||
  • Could I use hook_form_alter for this task, too? I was thinking about something like this: function xxx_form_alter(&$form, &$form_state, $form_id) { if($form_id == 'xxx') { $form['#validate'][] = 'xxx_custom_validate_function'; } } – marcmueller Apr 7 '15 at 21:36
0

You will want to run a hook_form_alter to add your own validation callback. An example may look like the following.

/**
 * Implements hook_form_alter().
 */
function commerce_stock_form_alter(&$form, &$form_state, $form_id) {
  if (strpos($form_id, "commerce_cart_add_to_cart_form") === 0) {
    $form['#validate'][] = 'mymodule_add_to_cart_validate';
  }
}

Your call back can then check $form_state['values']['quantity'] or $form_state['values']['product_id'], for example, to check quantity and the product added.

A great example of hooking into the add to cart form is in the Commerce Stock module, for advanced usages: http://cgit.drupalcode.org/commerce_stock/tree/commerce_stock.module#n64

|improve this answer|||||
-3

Maybe this post can help you. https://www.drupal.org/node/1292742

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.