<p>I still haven't found a succinct way to exclude a particular address by ID within the commerce customer addresses tags, but I did come up with the following as a way to get a nice clean set of addresses I want to iterate over.</p>

<p>Rather than use a conditional in my loop, I decided to create the collection first, with the address I want to exclude being kept out of the collection. This gets the job done!</p>

<pre><code>{% set customerAddresses = [] %}

{% for address in craft.commerce.customer.addresses %}
    {% if address.id != craft.lhngroups.orgCommerceAddressId %}
        {% set customerAddresses = customerAddresses|merge([address]) %}
    {% endif %}
{% endfor %}
</code></pre>

CC BY-SA 3.0
2016-09-17T15:28:31.123Z
2016-09-17T15:28:31.123Z
jthomas
<p>The following is a very straightforward variable to put within your own plugin's variables/variables.php file:</p>

<pre><code>function getCommerceAddressById($id)
{
    return craft()-&gt;commerce_addresses-&gt;getAddressById($id);
}
</code></pre>

<p>In your case you use it to return just the addresses you need.</p>

CC BY-SA 3.0
2018-02-05T17:30:12.683Z
2018-02-05T20:04:26.027Z
2018-02-05T20:04:26.027Z
Clive Portman
<p>I would like to retrieve a list of all commerce customer addresses except for one. When working with entries, this can be done with the <code>{id: 'not ' ~ some.id }</code> parameter. This doesn't seem to work with <code>craft.commerce.customer.addresses</code>.</p>

<p>I'm currently achieving what I need with the following code:</p>

<pre><code>{% set customerAddresses = craft.commerce.customer.addresses %}
{% for address in customerAddresses %}
    {% if address.id != craft.lhngroups.orgCommerceAddressId %}
        output the address
    {% endif %}
{% endfor %}
</code></pre>

<p>This works, but it throws off some <code>{{ loop.index }}</code> operations I'm trying to perform since the actual array of addresses contains 1 more address than what I'm displaying.</p>

<plugin-craftcommerce><array><loop>
Can I exclude a commerce address by ID when retrieving a list of addresses?
# Can I exclude a commerce address by ID when retrieving a list of addresses?

(Asked by: username_0 on 2016-09-17)

I would like to retrieve a list of all commerce customer addresses except for one. When working with entries, this can be done with the {id: 'not ' ~ some.id } parameter. This doesn't seem to work with craft.commerce.customer.addresses.
I'm currently achieving what I need with the following code:
{% set customerAddresses = craft.commerce.customer.addresses %}
{% for address in customerAddresses %}
    {% if address.id != craft.lhngroups.orgCommerceAddressId %}
        output the address
    {% endif %}
{% endfor %}

This works, but it throws off some {{ loop.index }} operations I'm trying to perform since the actual array of addresses contains 1 more address than what I'm displaying.

**Tags:** plugin-craftcommerce, array, loop

---

## 2 answers

### Answer by username_0 on 2016-09-17. Score: 3

I still haven't found a succinct way to exclude a particular address by ID within the commerce customer addresses tags, but I did come up with the following as a way to get a nice clean set of addresses I want to iterate over.
Rather than use a conditional in my loop, I decided to create the collection first, with the address I want to exclude being kept out of the collection. This gets the job done!
{% set customerAddresses = [] %}

{% for address in craft.commerce.customer.addresses %}
    {% if address.id != craft.lhngroups.orgCommerceAddressId %}
        {% set customerAddresses = customerAddresses|merge([address]) %}
    {% endif %}
{% endfor %}


---

### Answer by username_1 on 2018-02-05. Score: 1

The following is a very straightforward variable to put within your own plugin's variables/variables.php file:
function getCommerceAddressById($id)
{
    return craft()->commerce_addresses->getAddressById($id);
}

In your case you use it to return just the addresses you need.


---

