2
Answers
Vote up!
0
Vote down!

Submit handler for quantity change in cart?

Hi all,

I am writing a Commerce submodule that needs to run some code on the line items in an order after the Update Cart or Checkout buttons are clicked in the shopping cart form... but I'm not sure where to put it. More specifically, I need to see if the user updated the quantity of a line item in their cart and then write code that acts on the quantity change (without using Rules).

I am already using hook_form_FORM_ID_alter() to alter the Remove buttons in the shopping cart, I figured I could just add a submit handler in this hook but that doesn't seem to work:

function MYMODULE_form_views_form_commerce_cart_form_default_alter(&$form, &$form_state) {
  $form['#submit'][] = 'MYMODULE_cart_submit';
}

function MYMODULE_cart_submit($form, &$form_state) {
  dsm($form);
}

When I hit either Update Cart or Checkout, dsm doesn't load on the next page. If I try dsm('hello, world!'), I don't get that either. Am I in the right spot? I assumed I could just work with $form_save in the submit handler, and then resave the line items using commerce_line_item_save($line_item).

Thanks! :)

Asked by: ridgek_lives
on January 12, 2014

2 Answers

Vote up!
0
Vote down!

Okay, I've answered this question myself. \(^_^)/

Should be:

function MYMODULE_form_views_form_commerce_cart_form_default_alter(&$form, &$form_state) {
  $form['actions']['submit']['#submit'][] = 'MYMODULE_cart_update';
  $form['actions']['checkout']['#submit'][] = 'MYMODULE_cart_checkout';
}

function MYMODULE_cart_update($form, &$form_state) {
  dsm('Update Cart clicked);
}
function MYMODULE_cart_checkout($form, &$form_state) {
  dsm('Checkout clicked);
}
Answer by: ridgek_lives
Posted: Jan 12, 2014
Vote up!
0
Vote down!

Okay, I've answered this question myself. \(^_^)/

Should be:

function MYMODULE_form_views_form_commerce_cart_form_default_alter(&$form, &$form_state) {
  $form['actions']['submit']['#submit'][] = 'MYMODULE_cart_update';
  $form['actions']['checkout']['#submit'][] = 'MYMODULE_cart_checkout';
}

function MYMODULE_cart_update($form, &$form_state) {
  dsm('Update Cart clicked);
}
function MYMODULE_cart_checkout($form, &$form_state) {
  dsm('Checkout clicked);
}
Answer by: ridgek_lives
Posted: Jan 12, 2014