2

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.

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 %}
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.

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.