<p>Instead of calling <code>getAllStates()</code> to get the same data back as the twig call:</p>

<pre><code>var states = {{ craft.commerce.statesArray|json_encode|raw }};
</code></pre>

<p>Within your plugin you need to call <code>getStatesGroupedByCountries()</code></p>

<p><strong>MyPlugin.php</strong></p>

<pre><code>if (craft()-&gt;request-&gt;isAjaxRequest()) {
    return $this-&gt;returnJson(['states' =&gt; craft()-&gt;commerce_states-&gt;getStatesGroupedByCountries()]);
}
</code></pre>

CC BY-SA 3.0
2016-02-19T05:08:59.703Z
2016-02-19T05:08:59.703Z
Damon
<p>I am trying to call <code>$states = craft()-&gt;commerce_states-&gt;getAllStates();</code> from within my own plugin, via an ajax request.</p>

<p><strong>MyPlugin.php</strong></p>

<pre><code>if (craft()-&gt;request-&gt;isAjaxRequest()) {
    return $this-&gt;returnJson(['states' =&gt; craft()-&gt;commerce_states-&gt;getAllStates()]);
}
</code></pre>

<p>The above will return this:</p>

<pre><code>Object {states: Array[72]}
</code></pre>

<p>However, as I'm walking myself through it, I can see that the original call:</p>

<pre><code>var states = {{ craft.commerce.statesArray|json_encode|raw }};
</code></pre>

<p>Gives me these objects:</p>

<pre><code>Object {13: Object, 38: Object, 233: Object}
Object {13: Object, 38: Object, 233: Object}
</code></pre>

<p>So it's pretty obvious i'm not calling the correct method. I am using <a href="http://requirejs.org/" rel="nofollow">requirejs</a> within my project and having the javascript right in the template is returning errors - it can't access jquery. </p>

<p>Thank you for any suggestions!</p>

<plugin-development><plugin-craftcommerce>
With Craft Commerce, how can I call getAllStates from my own plugin via ajax?
# With Craft Commerce, how can I call getAllStates from my own plugin via ajax?

(Asked by: username_0 on 2016-02-19)

I am trying to call $states = craft()->commerce_states->getAllStates(); from within my own plugin, via an ajax request.
MyPlugin.php
if (craft()->request->isAjaxRequest()) {
    return $this->returnJson(['states' => craft()->commerce_states->getAllStates()]);
}

The above will return this:
Object {states: Array[72]}

However, as I'm walking myself through it, I can see that the original call:
var states = {{ craft.commerce.statesArray|json_encode|raw }};

Gives me these objects:
Object {13: Object, 38: Object, 233: Object}
Object {13: Object, 38: Object, 233: Object}

So it's pretty obvious i'm not calling the correct method. I am using requirejs within my project and having the javascript right in the template is returning errors - it can't access jquery. 
Thank you for any suggestions!

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


---

## 1 answers

### Answer by username_0 on 2016-02-19. Score: 2 | Accepted answer

Instead of calling getAllStates() to get the same data back as the twig call:
var states = {{ craft.commerce.statesArray|json_encode|raw }};

Within your plugin you need to call getStatesGroupedByCountries()
MyPlugin.php
if (craft()->request->isAjaxRequest()) {
    return $this->returnJson(['states' => craft()->commerce_states->getStatesGroupedByCountries()]);
}


---

