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

PropertyDescription
$behaviorsyii\base\Behavior – List of behaviors attached to this component
$storeLocationAddresscraft\commerce\models\Address

$storeLocationAddress

Signature

public craft\commerce\models\Address getStoreLocationAddress ( )

Public Methods

MethodDescription
__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 an object property.
__isset()Checks if a property is set, i.e. defined and not null.
__set()Sets value of an object property.
__unset()Sets an object property to 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.
init()Initializes the object.
off()Detaches an existing event handler from this component.
on()Attaches an event handler to an event.
saveAddress()Saves an address.
trigger()Triggers an event.

addressWithinZone()

View source

Arguments

  • $address
  • $zone

Returns

boolean

Signature

public boolean addressWithinZone ( $address, craft\commerce\base\AddressZoneInterface $zone )

deleteAddressById()

Deletes an address by its ID.

View source

Arguments

Returns

boolean – Whether the address was deleted successfully

Signature

public boolean deleteAddressById ( \craft\commerce\services\int $id )

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

Signature

public craft\commerce\models\Address, null getAddressById ( \craft\commerce\services\int $addressId )

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

Signature

public craft\commerce\models\Address, null getAddressByIdAndCustomerId ( \craft\commerce\services\int $addressId, $customerId = null )

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

Signature

public craft\commerce\models\Address[] getAddressesByCustomerId ( \craft\commerce\services\int $customerId )

getStoreLocationAddress()

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

View source

Returns

craft\commerce\models\Address

Signature

public craft\commerce\models\Address getStoreLocationAddress ( )

saveAddress()

Saves an address.

View source

Arguments

Returns

boolean – Whether the address was saved successfully.

Throws

Signature

public boolean saveAddress ( craft\commerce\models\Address $addressModel, \craft\commerce\services\bool $runValidation = true )

Events

EVENT_AFTER_SAVE_ADDRESS

Type
craft\commerce\events\AddressEvent

The event that is raised after an address is saved.

Plugins can get notified before an address is being saved

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

Event::on(Addresses::class, Addresses::EVENT_AFTER_SAVE_ADDRESS, function(AddressEvent $e) {
    // Do something - perhaps set this address as default in an external CRM system
});

EVENT_BEFORE_SAVE_ADDRESS

Type
craft\commerce\events\AddressEvent

The event that is raised before an address is saved.

Plugins can get notified before an address is being saved

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

Event::on(Addresses::class, Addresses::EVENT_BEFORE_SAVE_ADDRESS, function(AddressEvent $e) {
    // Do something - perhaps let an external CRM system know about a client's new address
});