I am trying to add some logic that can stop an order transaction from happening depending on values stored in the Order Model. Due to how my client wants the purchase page setup, I need to be able to do last-minute validating through the commerce/payments/pay action, which I can intervene before it happens using the onBeforeGatewayRequestSend event.

Below is a simplified version of what I'm trying to do. The issue I am having is accessing the Order Model, It looks like it isn't available in the $event parameter - which is why the below doesn't work.

craft()->on('commerce_payments.onBeforeGatewayRequestSend', function($event) {

    // This doesn't work. No 'order' param available.
    $order = $event->params['order']; 

    /* logic using $order, 
    -- set $is_valid (boolean) */;

    if(!$is_valid) {

       $transaction = $event->params['transaction'];
       $transaction->message = "This order is not yet complete.";
       $event->performAction = false;
       return $event;



Is it at all possible to get the order model inside of this hook? Or is this just not the right place to be doing this?


You have access to the order from the transaction like this:

craft()->on('commerce_payments.onBeforeGatewayRequestSend', function($event) {

    $transaction = $event->params['transaction']; 
    $order = $transaction->order;


You can see what is available for each event in the docs here:


...and indeed order is not one of the parameters. (I would say it should be really, and you might want to request @lukeholder add this!)

However it's very simple to get the current cart (it's a cart at this point, technically, but that's just an OrderModel that is not set as completed yet). Use this code:

$cart = craft()->commerce_cart->getCart();

One other note - you don't need to return $event; as the event is passed in by reference anyway (hence you can set $event->performAction = false; and that sets it on the original event passed in by reference, not a local copy of the event).

  • I feel sad that I never knew you could grab the cart like that, all this time! That's going to be helpful in a lot of other ways. Thanks for that and the return tip!
    – taylor
    Oct 26 '16 at 5:11
  • don't use getCart(), get the transaction's order as per my answer below. Oct 26 '16 at 5:51
  • Don't listen to that Luke guy, what does he know?! ;) Oct 26 '16 at 8:02

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.