1

I am a Drupal Commerce novice. I have a basic cart system working fine, but I now need to get into some custom code. I don't think I can use Rules because the code is a bit too complicated.

I have a Shopping Cart page with a Checkout button. I want to hook into that Checkout button.

I need to add a php function (which I already have) which will execute when that button is clicked but -before- the checkout page is rendered.

And I need this to do so without disabling the default actions that the Checkout button performs.

SO: How do I hook into this button without disabling the default behaviour?

Due to my noobness--I'd appreciate as complete an example as possible. I'm not lazy, just can't take for granted that I'll catch everything I need to if the answer is 'shorthand'.

TIA,

---JC

2

The checkout form is a form like any other, so you can just alter it (using hook_form_alter()) and add a submit handler:

function MYMODULE_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'views_form_commerce_cart_form_default') {
    $form['#submit'][] = 'MYMODULE_some_function';
  }
}

function MYMODULE_some_function($form, &$form_state) {
  drupal_set_message('This was run after the cart form was submitted, before the checkout page was rendered.');
}

Addressing the comments...

As you have a custom shopping cart form, with a link instead of submit button, no extra processing will be done so you can just hook into the page build of the linked page (/checkout/%) and perform your operation there; it will have the same effect.

There are a couple of places you could do this (neither really 'better' than the other), hook_page_alter() and hook_init(). There are other places too (theme preprocess functions) but for processing logic something a bit earlier is probably more appropriate. Basic example (will probably need tweaking for your needs):

function MYMODULE_init() {
  if (arg(0) == 'checkout' && $some_other_pertinent_condition) {
    // Do some processing.
  }
}

As for learning Commerce - that's a tricky one. Commerce is heavily integrated with Drupal and uses a lot of its functions/hooks/integrations just like any other module would. So really you need to know Drupal's workflow, and then overlay Commerce's workflow on top of that. The best advise I can give is to grab a Drupal book (Pro Drupal Development is excellent), and after you're happy with the workflow there, hit the Drupal Commerce support pages and read, read, read.

Drupal has a notoriously steep learning curve, and there isn't really a shortcut to understanding how it works as a whole. You just have to get stuck in. But it's well worth it, Drupal's awesome.

  • Wow, I went way too far - I thought he was asking for the button that links over to the cart page. – emc May 31 '13 at 16:05
  • @Clive --- Thanks. I was ready to jump for joy but the Checkout 'button' is actually a link <a href="ref="/?q=checkout"> Sorry for being so thick, but that's not really a form submit is it? So again, how do I connect my function to that hyperlink? – jchwebdev May 31 '13 at 17:16
  • 1
    Not thick at all :) In my commerce installations the Checkout "button" in the shopping cart is actually a button (<input type="submit" />) and submits the form. I tested that code out to make sure and it does the trick for me. If it's a link in your install, you can safely assume commerce does no extra processing when you click it. The easiest way to 'hook in' then would actually be on the checkout page itself. You could use hook_init(), hook_page_alter(), hook_preprocess_page() and a bunch of others to perform whatever you need to – Clive May 31 '13 at 17:35
  • OK, next noob question... is there a (relatively) simple-minded document that explains the 'flow' of these hooks during page rendering? IOW: how do I know which of those 3 hooks to choose? More broadly: The tough thing for me in understanding commerce is trying to understand the -sequence- of events firing on various pages? I looked @ the Developer docs and felt instantly -stupid-. I'm working on a project for a 'designer' who did this cart so I'm not building the forms from scratch. Any 'Commerce Hooks For Dummies' would be most appreciated. TIA!!!!! – jchwebdev May 31 '13 at 17:46
  • BTW: the 'designer' created the cart form with the 'Checkout' link using the Commerce 'form builder'. My next noob question is why the 'form builder' creates a -link- and in your installs, you use a -button-? Feeling sillier by the minute. – jchwebdev May 31 '13 at 17:49
1

Yikes. As far as I can tell, there isn't a direct hook for clicking the checkout button. The provided hooks for the Drupal Commerce checkout module are over here, there's a general hooks list here, and the actual checkout form output is generated here which can be overridden.

This is a tricky request, and I'm at a loss. The test site I set up doesn't even show the checkout button in a form, it's just a link - so I guess you can't use hook_form_alter() on it.

My thoughts for chasing this down are:

  • Add some custom JS and make an ajax call on button click that initiates a condition for the next page
  • Start logging the previous page in a session variable and do a comparison when you land on the checkout page - if you came from a page that has a checkout button, execute action. (Might be able to adapt Previous Pages for use)
  • Use Rules to trigger on a "Product is viewed" action with a path condition of the checkout page
  • Hook into the checkout_page_info_alter function to determine whether you're on the correct page
  • Redirect users to a staging page of sorts before they hit the actual checkout page - change the cart page to something of your own choosing, collect some data, append a ?destination parameter to the URL and roll through with your new info

I'm at a loss. Since this isn't really an answer, I can't give a great example of how to do any of this without great detail. I did figure out the Commerce hooks for you though:

hook_commerce_cart_order_is_cart_alter becomes YOURTHEME_commerce_cart_order_is_cart_alter

Lol. Sorry for not being more useful... good luck.

  • Interestingly all my commerce installs (those with and without kickstart) use a form on the shopping cart with a submit button. OP is describing the same as you though, so there's something fishy going on. This is all really useful info given that fact – Clive May 31 '13 at 18:10
  • Did you peek at the test site: cm.emc2innovation.com/drinks/guy-h20 - add to cart, then the page reloads and an overlay shows the cart with a checkout link. I wonder if there's a .tpl.php for the cart overlay somewheres... – emc May 31 '13 at 18:25
  • 1
    ohhhhh that checkout link. I read 'shopping cart' and just assumed we were talking about the shopping cart page :) This finally makes sense. That's actually the default message area with a View embedded, positioned like that with CSS/JS. It's from the commerce_add_to_cart_confirmation contrib module. No theme functions involved as far as I can tell – Clive May 31 '13 at 18:42
  • Clive can you please look at another question I posted (which is similar.) I tried your solution and the button #id isn't appearing as I expect. There's nothing in the submit array. TIA. the drupal.stackexchange.com/questions/75534/… – jchwebdev Jun 7 '13 at 18:11

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.