<p>Figured it out:</p>

<pre><code>use craft\commerce\Plugin as Commerce;


$gatewayService = Commerce::getInstance()-&gt;getGateways();
$variables = [
    'gateways' =&gt; $gatewayService-&gt;getAllGateways(),
];
</code></pre>

CC BY-SA 4.0
2020-01-08T20:21:56.543Z
FYI: This pattern is a product of Plugins being "singleton" classes, and their instances having Services attached as [components](https://www.yiiframework.com/doc/guide/2.0/en/concept-components), sometimes with special "getter" methods (i.e. `->getGateways()`). In most Plugins, it's safe to try `MyPlugin::getInstance()->serviceName->methodName();`
August Miller
CC BY-SA 4.0
2020-01-08T21:42:44.693Z
So, this probably would have worked as well and I just didn't think of it? `$gateways = Commerce::getInstance()->commerce->getGateways()->getAllGateways();`?
Darryl Hardin
CC BY-SA 4.0
2020-01-09T03:25:00.257Z
No, you've already got a reference to the plugin instance from `Commerce::getInstance()`—the `commerce` property wouldn't exist. Just `Commerce::getInstance()->getGateways()->getAllGateways()` will do!

You might be thinking of the way you get access to Commerce services via Twig (`{% set gateways = craft.commerce.gateways.getAllGateways() %}`, but this is specific to the templating environment (`craft` is just a variable that you can hang functionality on, and a lot of plugins attach their singleton instance there).
August Miller
CC BY-SA 4.0
2020-01-08T14:44:12.637Z
2020-01-08T14:44:12.637Z
Darryl Hardin
<p>I have been trying to access <code>craft\commerce\services\Gateways;</code> from a plugin but I can't figure out how I need to access it. I've used the following:</p>

<pre><code>$gateways = Commerce::getInstance()-&gt;commerce-&gt;getAllGateways();

$gateways = craft\commerce\services\Gateways::getAllGateways();
</code></pre>

<p>The first one says <code>Getting unknown property: craft\commerce\Plugin::commerce</code> and the second one tells me 
<code>Non-static method craft\commerce\services\Gateways::getAllGateways() should not be called statically</code></p>

<p>Can anyone tell me the proper way of doing this? Thanks!</p>

<plugin-development><craft3>
How can I access the Gateways services?
# How can I access the Gateways services?

(Asked by: username_0 on 2020-01-08)

I have been trying to access craft\commerce\services\Gateways; from a plugin but I can't figure out how I need to access it. I've used the following:
$gateways = Commerce::getInstance()->commerce->getAllGateways();

$gateways = craft\commerce\services\Gateways::getAllGateways();

The first one says Getting unknown property: craft\commerce\Plugin::commerce and the second one tells me 
Non-static method craft\commerce\services\Gateways::getAllGateways() should not be called statically
Can anyone tell me the proper way of doing this? Thanks!

**Tags:** plugin-development, craft3

---

## 1 answers

### Answer by username_0 on 2020-01-08. Score: 1 | Accepted answer

Figured it out:
use craft\commerce\Plugin as Commerce;


$gatewayService = Commerce::getInstance()->getGateways();
$variables = [
    'gateways' => $gatewayService->getAllGateways(),
];

> **Comments:**
> **username_1**: FYI: This pattern is a product of Plugins being "singleton" classes, and their instances having Services attached as [components](https://www.yiiframework.com/doc/guide/2.0/en/concept-components), sometimes with special "getter" methods (i.e. `->getGateways()`). In most Plugins, it's safe to try `MyPlugin::getInstance()->serviceName->methodName();`  
> **username_0**: So, this probably would have worked as well and I just didn't think of it? `$gateways = Commerce::getInstance()->commerce->getGateways()->getAllGateways();`?  
> **username_1**: No, you've already got a reference to the plugin instance from `Commerce::getInstance()`—the `commerce` property wouldn't exist. Just `Commerce::getInstance()->getGateways()->getAllGateways()` will do!  You might be thinking of the way you get access to Commerce services via Twig (`{% set gateways = craft.commerce.gateways.getAllGateways() %}`, but this is specific to the templating environment (`craft` is just a variable that you can hang functionality on, and a lot of plugins attach their singleton instance there).  

---

