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!

3 Answers 3

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.

1
  • 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'; } } Commented Apr 7, 2015 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

-3

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

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.