<p>You have access to the order from the transaction like this:</p>

<pre><code>craft()-&gt;on('commerce_payments.onBeforeGatewayRequestSend', function($event) {

    $transaction = $event-&gt;params['transaction']; 
    $order = $transaction-&gt;order;

}
</code></pre>

CC BY-SA 3.0
2016-10-26T08:02:41.797Z
Ha - even easier.  Would be great if the transaction model was documented (and we could search the Commerce docs)!
Jeremy Daalder
CC BY-SA 3.0
2016-10-26T10:54:42.167Z
@JeremyDaalder just added https://craftcommerce.com/docs/transaction-model
Luke Holder
CC BY-SA 3.0
2016-10-26T05:51:00.997Z
2016-10-26T08:01:34.247Z
2016-10-26T08:01:34.247Z
Luke Holder
<p>You can see what is available for each event in the docs here: </p>

<p><a href="https://craftcommerce.com/docs/events-reference#commerce_payments.onbeforegatewayrequestsend" rel="nofollow">https://craftcommerce.com/docs/events-reference#commerce_payments.onbeforegatewayrequestsend</a></p>

<p>...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!)</p>

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

<pre><code>$cart = craft()-&gt;commerce_cart-&gt;getCart();
</code></pre>

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

CC BY-SA 3.0
2016-10-26T05:11:31.423Z
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
CC BY-SA 3.0
2016-10-26T05:51:51.723Z
don't use `getCart()`, get the transaction's order as per my answer below.
Luke Holder
CC BY-SA 3.0
2016-10-26T08:02:39.720Z
Don't listen to that Luke guy, what does he know?! ;)
Jeremy Daalder
CC BY-SA 3.0
2016-10-26T04:57:23.260Z
2016-10-26T04:57:23.260Z
Jeremy Daalder
<p>I am trying to add some logic that can stop an order transaction from happening depending on values stored in the <strong>Order Model</strong>. Due to how my client wants the purchase page setup, I need to be able to do last-minute validating through the <code>commerce/payments/pay</code> action, which I can intervene before it happens using the <code>onBeforeGatewayRequestSend</code> event.</p>

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

<pre><code>craft()-&gt;on('commerce_payments.onBeforeGatewayRequestSend', function($event) {

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

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

    if(!$is_valid) {

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

    }

});
</code></pre>

<p>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?</p>

<plugin-development><plugin-craftcommerce><php>
Commerce Order Model in Gateway Event Plugin Hook
# Commerce Order Model in Gateway Event Plugin Hook

(Asked by: username_0 on 2016-10-26)

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?

**Tags:** plugin-development, plugin-craftcommerce, php


---

## 2 answers

### Answer by username_1 on 2016-10-26. Score: 3 | Accepted answer

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;

}

> **Comments:**
> **username_2**: Ha - even easier.  Would be great if the transaction model was documented (and we could search the Commerce docs)!  
> **username_1**: @JeremyDaalder just added https://craftcommerce.com/docs/transaction-model  

---

### Answer by username_2 on 2016-10-26. Score: 1

You can see what is available for each event in the docs here: 
https://craftcommerce.com/docs/events-reference#commerce_payments.onbeforegatewayrequestsend
...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).

> **Comments:**
> **username_0**: 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!  
> **username_1**: don't use `getCart()`, get the transaction's order as per my answer below.  
> **username_2**: Don't listen to that Luke guy, what does he know?! ;)  

---

