1

I am writing a Commerce submodule that needs to run some code on the line items in an order when 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. Am I in the right spot?

Thanks! :)

1

You could use the Entity API type hooks (load, presave, update etc..) to act when the commerce_line_item is updated

e.g:

/**
 * Implements hook_entity_update
 */
function MYMODULE_entity_update($entity, $type){

  if($type == 'commerce_line_item'){
    // Do stuff with line item in $entity;
  }

}

You might have access to $entity->unchanged or similar in $entity which stores the entity content prior to the save. Useful for comparing quantity to see if it's changed!

dpm($entity); // for details.

You can also use hook_ENTITY_TYPE_update

/**
 * Implements hook_ENTITY_TYPE_update
 */
function MYMODULE_commerce_line_item_update($entity){

  dpm($entity);

}

The hooks for load, presave, insert, update and delete are available for all Commerce entity types.

  • You're welcome, note I'd probably just use Rules since it's a Commerce dependency anyway. You can also define your Rules in code with hook_default_rules_configuration – David Thomas Jan 14 '14 at 2:37
  • I get the feeling I'll be revisiting this subject as I learn more about Rules API and how it integrates into Commmerce. Thanks again! – ridgek Jan 19 '14 at 7:09
0

Welcome to the Drupal StackExchange !

If you are looking to detect changes in cart orders or line items i believe you can use below hooks which are exposed by cart module.

/**
 * Rules event hook: allows modules to react to the addition of a product to a
 * shopping cart order.
 *
 * @param $order
 *   The cart order object the product was added to.
 * @param $product
 *   The product that was added to the cart.
 * @param $quantity
 *   The quantity of the product added to the cart.
 * @param $line_item
 *   The new or updated line item representing that product on the given order.
 */
function hook_commerce_cart_product_add($order, $product, $quantity, $line_item) {
  // No example.
}

/**
 * Rules event hook: allows modules to react to the removal of a product from a
 * shopping cart order.
 *
 * @param $order
 *   The cart order object the product was removed from.
 * @param $product
 *   The product that was removed from the cart.
 * @param $quantity
 *   The quantity of the product line item removed from the cart.
 * @param $line_item
 *   The product line item that was deleted to remove the product from the cart.
 */
function hook_commerce_cart_product_remove($order, $product, $quantity, $line_item) {
  // No example.
}

Cheers, Anil Sagar

  • Thanks Anil, but I need to act on data already stored in the form in $form/$form_state, which, I think would require creating a new Rules data type if I were to use those hooks? I'm a noob to Drupal devel so I am still figuring out all these APIs. – ridgek Jan 14 '14 at 1:54
0

Thanks for all your answers, but I answered this myself. I think! :)

Should be:

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

function MYMODULE_cart_validate($form, &$form_state) {
  dsm('Form validated');
}
function MYMODULE_cart_update($form, &$form_state) {
  dsm('Update Cart button pressed');
}

function MYMODULE_cart_checkout($form, &$form_state) {
  dsm('Checkout button pressed');
}

So I guess my question now is why doesn't the regular submit handler work?

  $form['#submit'][] = 'MYMODULE_cart_submit';
  • 1
    The problem with that approach is you're assuming line items are only updated during the checkout process. A line item could be updated in the admin order area as well as programmatically in other contexts, hence the suggestion to use hook_commerce_line_item_update instead. – David Thomas Jan 14 '14 at 21:00
  • Oh snap! You're right, I am totally centered around the checkout process at the moment. Haha, you may have saved me several wasted hours, friend. Thanks!!! – ridgek Jan 19 '14 at 6:05

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.