Address

Type
Class
Namespace
craft\commerce\models
Inherits
craft\commerce\models\Address » craft\commerce\base\Model » craft\base\Model » yii\base\Model » yii\base\Component » yii\base\BaseObject
Implements
ArrayAccess, IteratorAggregate, yii\base\Arrayable, yii\base\Configurable, yii\base\StaticInstanceInterface
Uses traits
craft\base\ClonefixTrait, yii\base\ArrayableTrait, yii\base\StaticInstanceTrait
Since
2.0

Address Model

View source

# Public Properties

Property Description
abbreviationText string
activeValidators yii\validators\Validator – The validators applicable to the current scenario.
address1 string – Address Line 1
address2 string – Address Line 2
address3 string – Address Line 3
addressLines array
alternativePhone string – Alternative Phone
attention string – Attention
attributes array – Attribute values (name => value).
behaviors yii\base\Behavior – List of behaviors attached to this component
businessId string – Business ID
businessName string – Business Name
businessTaxId string – Business Tax ID
city string – City
country craft\commerce\models\Country, null
countryId integer – Country ID
countryIso string
countryText string
cpEditUrl string
custom1 string – Custom Field 1
custom2 string – Custom Field 2
custom3 string – Custom Field 3
custom4 string – Custom Field 4
errors array – Errors for all attributes or the specified attribute.
firstErrors array – The first errors.
firstName string – First Name
fullName string – Full Name
id integer – Address ID
isEstimated boolean – If this address is used for estimated values
isStoreLocation boolean – Is this the store location.
iterator ArrayIterator – An iterator for traversing the items in the list.
label string – Label
lastName string – Last Name
notes string – Notes, only field that can contain Emoji
phone string – Phone
scenario string – The scenario that this model is in.
state craft\commerce\models\State, null
stateId integer – State ID
stateName string – State Name
stateText string
stateValue integer, string
title string – Title
validators ArrayObject, yii\validators\Validator – All the validators declared in the model.
zipCode string – Zip

# abbreviationText

Type
string
Access
Read-only

View source

# address1

Type
string

Address Line 1

View source

# address2

Type
string

Address Line 2

View source

# address3

Type
string
Since
2.2

Address Line 3

View source

# addressLines

Type
array
Access
Read-only
Since
3.2.0

View source

# alternativePhone

Type
string

Alternative Phone

View source

# attention

Type
string

Attention

View source

# businessId

Type
string

Business ID

View source

# businessName

Type
string

Business Name

View source

# businessTaxId

Type
string

Business Tax ID

View source

# city

Type
string

City

View source

# country

Type
craft\commerce\models\Country, null
Access
Read-only

View source

# countryId

Type
integer

Country ID

View source

# countryIso

Type
string
Access
Read-only
Since
3.1.4

View source

# countryText

Type
string
Access
Read-only

View source

# cpEditUrl

Type
string
Access
Read-only

View source

# custom1

Type
string
Since
2.2

Custom Field 1

View source

# custom2

Type
string
Since
2.2

Custom Field 2

View source

# custom3

Type
string
Since
2.2

Custom Field 3

View source

# custom4

Type
string
Since
2.2

Custom Field 4

View source

# firstName

Type
string

First Name

View source

# fullName

Type
string
Since
2.2

Full Name

View source

# id

Type
integer

Address ID

View source

# isEstimated

Type
boolean
Since
2.2

If this address is used for estimated values

View source

# isStoreLocation

Type
boolean

Is this the store location.

View source

# label

Type
string
Since
2.2

Label

View source

# lastName

Type
string

Last Name

View source

# notes

Type
string
Since
2.2

Notes, only field that can contain Emoji

View source

# phone

Type
string

Phone

View source

# state

Type
craft\commerce\models\State, null
Access
Read-only

View source

# stateId

Type
integer

State ID

View source

# stateName

Type
string

State Name

View source

# stateText

Type
string
Access
Read-only

View source

# stateValue

Type
integer, string

View source

# title

Type
string

Title

View source

# zipCode

Type
string

Zip

View source

# Public Methods

Method Description
__call() Calls the named method which is not a class method.
__clone()
__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.
activeAttributes() Returns the attribute names that are subject to validation in the current scenario.
addError() Adds a new error to the specified attribute.
addErrors() Adds a list of errors.
addModelErrors() Adds errors from another model, with a given attribute name prefix.
afterValidate() This method is invoked after validation ends.
attachBehavior() Attaches a behavior to this component.
attachBehaviors() Attaches a list of behaviors to the component.
attributeHints() Returns the attribute hints.
attributeLabels() Returns the attribute labels.
attributes() Returns the list of attribute names.
beforeValidate() This method is invoked before validation starts.
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.
clearErrors() Removes errors for all attributes or a single attribute.
createValidators() Creates validator objects based on the validation rules specified in rules().
datetimeAttributes() Returns the names of any attributes that should hold DateTime values.
defineRules() Returns the validation rules for attributes.
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.
extraFields() Returns the list of fields that can be expanded further and returned by toArray().
fields() Returns the list of fields that should be returned by default by toArray() when no specific fields are specified.
formName() Returns the form name that this model class should use.
generateAttributeLabel() Generates a user friendly attribute label based on the give attribute name.
getAbbreviationText()
getActiveValidators() Returns the validators applicable to the current scenario.
getAddressLines() Return a keyed array of address lines. Useful for outputting an address in a consistent format.
getAttributeHint() Returns the text hint for the specified attribute.
getAttributeLabel() Returns the text label for the specified attribute.
getAttributes() Returns attribute values.
getBehavior() Returns the named behavior object.
getBehaviors() Returns all behaviors attached to this component.
getCountry()
getCountryIso()
getCountryText()
getCpEditUrl()
getError() Returns the first error of the specified attribute.
getErrorSummary() Returns the errors for all attributes as a one-dimensional array.
getErrors() Returns the errors for all attributes or a single attribute.
getFirstError() Returns the first error of the specified attribute.
getFirstErrors() Returns the first error of every attribute in the model.
getIterator() Returns an iterator for traversing the attributes in the model.
getScenario() Returns the scenario that this model is used in.
getState()
getStateText()
getStateValue()
getValidators() Returns all the validators declared in rules().
hasErrors() Returns a value indicating whether there is any validation error.
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.
instance() Returns static class instance, which can be used to obtain meta information.
isAttributeActive() Returns a value indicating whether the attribute is active in the current scenario.
isAttributeRequired() Returns a value indicating whether the attribute is required.
isAttributeSafe() Returns a value indicating whether the attribute is safe for massive assignments.
load() Populates the model with input data.
loadMultiple() Populates a set of models with the data from end user.
off() Detaches an existing event handler from this component.
offsetExists() Returns whether there is an element at the specified offset.
offsetGet() Returns the element at the specified offset.
offsetSet() Sets the element at the specified offset.
offsetUnset() Sets the element value at the specified offset to null.
on() Attaches an event handler to an event.
onUnsafeAttribute() This method is invoked when an unsafe attribute is being massively assigned.
rules() Returns the validation rules for attributes.
safeAttributes() Returns the attribute names that are safe to be massively assigned in the current scenario.
sameAs() This method can be used to determine if the other addresses supplied has the same address contents (minus the address ID).
scenarios() Returns a list of scenarios and the corresponding active attributes.
setAttributes() Sets the attribute values in a massive way.
setScenario() Sets the scenario for the model.
setStateValue() Sets the stateId or stateName based on the value parameter.
toArray() Converts the model into an array.
trigger() Triggers an event.
validate() Performs the data validation.
validateBusinessTaxId()
validateMultiple() Validates multiple models.
validateState()

# attributeLabels()

Returns the attribute labels.

Attribute labels are mainly used for display purpose. For example, given an attribute firstName, we can declare a label First Name which is more user-friendly and can be displayed to end users.

By default an attribute label is generated using generateAttributeLabel(). This method allows you to explicitly specify attribute labels.

Note, in order to inherit labels defined in the parent class, a child class needs to merge the parent labels with child labels using functions such as array_merge().

View source

Returns

array – Attribute labels (name => label)

# attributes()

Returns the list of attribute names.

By default, this method returns all public non-static properties of the class. You may override this method to change the default behavior.

View source

Returns

array – List of attribute names.

# defineRules()

Returns the validation rules for attributes.

See rules() for details about what should be returned.

Models should override this method instead of rules() so EVENT_DEFINE_RULES handlers can modify the class-defined rules.

View source

Returns

array

# extraFields()

Returns the list of fields that can be expanded further and returned by toArray().

This method is similar to fields() except that the list of fields returned by this method are not returned by default by toArray(). Only when field names to be expanded are explicitly specified when calling toArray(), will their values be exported.

The default implementation returns an empty array.

You may override this method to return a list of expandable fields based on some context information (e.g. the current application user).

View source

Returns

array – The list of expandable field names or field definitions. Please refer to fields() on the format of the return value.

# fields()

Since
3.2.1

Returns the list of fields that should be returned by default by toArray() when no specific fields are specified.

A field is a named element in the returned array by toArray().

This method should return an array of field names or field definitions. If the former, the field name will be treated as an object property name whose value will be used as the field value. If the latter, the array key should be the field name while the array value should be the corresponding field definition which can be either an object property name or a PHP callable returning the corresponding field value. The signature of the callable should be:

function ($model, $field) {
    // return field value
}

For example, the following code declares four fields:

  • email: the field name is the same as the property name email;
  • firstName and lastName: the field names are firstName and lastName, and their values are obtained from the first_name and last_name properties;
  • fullName: the field name is fullName. Its value is obtained by concatenating first_name and last_name.
return [
    'email',
    'firstName' => 'first_name',
    'lastName' => 'last_name',
    'fullName' => function ($model) {
        return $model->first_name . ' ' . $model->last_name;
    },
];

In this method, you may also want to return different lists of fields based on some context information. For example, depending on scenario or the privilege of the current application user, you may return different sets of visible fields or filter out some fields.

The default implementation of this method returns attributes() indexed by the same attribute names.

View source

Returns

array – The list of field names or field definitions.

# getAbbreviationText()

View source

Returns

string

# getAddressLines()

Since
3.2.0

Return a keyed array of address lines. Useful for outputting an address in a consistent format.

View source

Arguments

Returns

array

# getCountry()

View source

Returns

craft\commerce\models\Country, null

# getCountryIso()

Since
3.1.4

View source

Returns

string

# getCountryText()

View source

Returns

string

# getCpEditUrl()

View source

Returns

string

# getState()

View source

Returns

craft\commerce\models\State, null

# getStateText()

View source

Returns

string

# getStateValue()

View source

Returns

integer, string

# init()

Initializes the object.

This method is invoked at the end of the constructor after the object is initialized with the given configuration.

View source

# sameAs()

Since
3.2.1

This method can be used to determine if the other addresses supplied has the same address contents (minus the address ID).

View source

Arguments

Returns

boolean

# setStateValue()

Sets the stateId or stateName based on the value parameter.

View source

Arguments

  • $value (string, integer, null) – A state ID or a state name, null to clear the state from the address.

# validateBusinessTaxId()

View source

Arguments

  • $attribute
  • $params
  • $validator

# validateState()

View source

Arguments

  • $attribute
  • $params
  • $validator

# Protected Methods

Method Description
extractFieldsFor() Extract nested fields from a fields collection for a given root field Nested fields are separated with dots (.). e.g: "item.id" The previous example would extract "id".
extractRootFields() Extracts the root field names from nested fields.
resolveFields() Determines which fields can be returned by toArray().

# Constants

Constant Description
SCENARIO_DEFAULT The name of the default scenario.

# Events

# EVENT_DEFINE_ADDRESS_LINES

Type
craft\commerce\events\DefineAddressLinesEvent
Since
3.2.0

The event that is triggered when determining the lines of the address to display.

See also getAddressLines()