Addresses

Type
Class
Namespace
craft\commerce\services
Inherits
craft\commerce\services\Addresses » yii\base\Component » yii\base\BaseObject
Implements
yii\base\Configurable
Since
2.0

Address service.

View source

# Public Properties

Property Description
behaviors yii\base\Behavior – List of behaviors attached to this component
storeLocationAddress craft\commerce\models\Address

# storeLocationAddress

Type
craft\commerce\models\Address
Access
Read-only

View source

# Public Methods

Method Description
__call() Calls the named method which is not a class method.
__clone() This method is called after the object is created by cloning an existing one.
__construct() Constructor.
__get() Returns the value of a component property.
__isset() Checks if a property is set, i.e. defined and not null.
__set() Sets the value of a component property.
__unset() Sets a component property to be null.
addressWithinZone()
attachBehavior() Attaches a behavior to this component.
attachBehaviors() Attaches a list of behaviors to the component.
behaviors() Returns a list of behaviors that this component should behave as.
canGetProperty() Returns a value indicating whether a property can be read.
canSetProperty() Returns a value indicating whether a property can be set.
className() Returns the fully qualified name of this class.
deleteAddressById() Deletes an address by its ID.
detachBehavior() Detaches a behavior from the component.
detachBehaviors() Detaches all behaviors from the component.
ensureBehaviors() Makes sure that the behaviors declared in behaviors() are attached to this component.
getAddressById() Returns an address by its ID.
getAddressByIdAndCustomerId() Returns an address by an address id and customer id.
getAddressesByCustomerId() Returns all of a customer's addresses, by the customer's ID.
getBehavior() Returns the named behavior object.
getBehaviors() Returns all behaviors attached to this component.
getStoreLocationAddress() Returns the stock location or a blank address if it's not defined.
hasEventHandlers() Returns a value indicating whether there is any handler attached to the named event.
hasMethod() Returns a value indicating whether a method is defined.
hasProperty() Returns a value indicating whether a property is defined for this component.
init() Initializes the object.
off() Detaches an existing event handler from this component.
on() Attaches an event handler to an event.
purgeOrphanedAddresses() Deletes all addresses not related to a customer, cart or order
removeReadOnlyAttributesFromArray()
saveAddress() Saves an address.
trigger() Triggers an event.

# addressWithinZone()

View source

Arguments

Returns

boolean

# deleteAddressById()

Deletes an address by its ID.

View source

Arguments

Returns

boolean – Whether the address was deleted successfully

# getAddressById()

Returns an address by its ID.

View source

Arguments

  • $addressId (integer) – The address' ID

Returns

craft\commerce\models\Address, null – The matched address or null if not found

# getAddressByIdAndCustomerId()

Returns an address by an address id and customer id.

View source

Arguments

  • $addressId (integer) – The address id
  • $customerId (integer) – The customer's ID

Returns

craft\commerce\models\Address, null – The matched address or null if not found

# getAddressesByCustomerId()

Returns all of a customer's addresses, by the customer's ID.

View source

Arguments

  • $customerId (integer) – The customer's ID

Returns

craft\commerce\models\Address[] – An array of matched addresses

# getStoreLocationAddress()

Returns the stock location or a blank address if it's not defined.

View source

Returns

craft\commerce\models\Address

# purgeOrphanedAddresses()

Since
3.0.4

Deletes all addresses not related to a customer, cart or order

View source

Throws

# removeReadOnlyAttributesFromArray()

Since
3.1

View source

Arguments

Returns

array

# saveAddress()

Saves an address.

View source

Arguments

Returns

boolean – Whether the address was saved successfully.

Throws

# Events

# EVENT_AFTER_DELETE_ADDRESS

Type
craft\commerce\events\AddressEvent

The event that is triggered after an address is deleted.

use craft\commerce\events\AddressEvent;
use craft\commerce\services\Addresses;
use craft\commerce\models\Address;
use yii\base\Event;

Event::on(
    Addresses::class,
    Addresses::EVENT_AFTER_DELETE_ADDRESS,
    function(AddressEvent $event) {
        // @var Address $address
        $address = $event->address;

        // Remove this address from a payment gateway
        // ...
    }
);



---



### EVENT_AFTER_SAVE_ADDRESS



Type

:   [craft\commerce\events\AddressEvent](craft-commerce-events-addressevent.md)



The event that is triggered after an address is saved.

```php
use craft\commerce\events\AddressEvent;
use craft\commerce\services\Addresses;
use craft\commerce\models\Address;
use yii\base\Event;

Event::on(
    Addresses::class,
    Addresses::EVENT_AFTER_SAVE_ADDRESS,
    function(AddressEvent $event) {
        // @var Address $address
        $address = $event->address;
        // @var bool $isNew
        $isNew = $event->isNew;

        // Set the default address in an external CRM
        // ...
    }
);

# EVENT_BEFORE_SAVE_ADDRESS

Type
craft\commerce\events\AddressEvent

The event that is triggered before an address is saved.

use craft\commerce\events\AddressEvent;
use craft\commerce\services\Addresses;
use craft\commerce\models\Address;
use yii\base\Event;

Event::on(
    Addresses::class,
    Addresses::EVENT_BEFORE_SAVE_ADDRESS,
    function(AddressEvent $event) {
        // @var Address $address
        $address = $event->address;
        // @var bool $isNew
        $isNew = $event->isNew;

        // Update customer’s address in an external CRM
        // ...
    }
);