Skip to content

Customer Addresses Model

DB Table Name{wp_db_prefix}_fct_customer_addresses
SchemaCheck Schema
Source Filefluent-cart/app/Models/CustomerAddresses.php
Name SpaceFluentCart\App\Models
ClassFluentCart\App\Models\CustomerAddresses

Traits

TraitDescription
CanSearchProvides search(), groupSearch(), whereLike(), whereBeginsWith(), whereEndsWith() query scopes

Attributes

AttributeData TypeComment
idIntegerPrimary Key
customer_idIntegerReference to customer
is_primaryBooleanWhether this is the primary address
typeStringAddress type (billing, shipping, etc.)
statusStringAddress status (active, archived)
labelStringAddress label/name
nameStringFull name
address_1StringPrimary address line
address_2StringSecondary address line
cityStringCity
stateStringState/Province
postcodeStringPostal/ZIP code
countryStringCountry code
phoneStringPhone number
emailStringEmail address
metaJSON NULLStored as JSON string, auto-encoded/decoded via mutator/accessor
company_nameVirtualStored inside meta->other_data.company_name, accessible as a virtual attribute via mutator/accessor
created_atDate TimeCreation timestamp
updated_atDate TimeLast update timestamp

Appended Attributes

The following attributes are appended to every model serialization (e.g. toArray(), toJson()):

AttributeTypeDescription
formatted_addressArrayFull formatted address with resolved country/state names, full address string, etc.
company_nameStringCompany name extracted from meta->other_data.company_name

Usage

Please check Model Basic for Common methods.

Accessing Attributes

php
$customerAddress = FluentCart\App\Models\CustomerAddresses::find(1);

$customerAddress->id; // returns id
$customerAddress->customer_id; // returns customer ID
$customerAddress->is_primary; // returns primary status
$customerAddress->type; // returns address type
$customerAddress->company_name; // returns company name from meta
$customerAddress->formatted_address; // returns formatted address array

Scopes

This model has the following scopes that you can use

ofActive()

Filter active addresses

  • Parameters
    • none

Usage:

php
// Get all active addresses
$activeAddresses = FluentCart\App\Models\CustomerAddresses::ofActive()->get();

ofArchived()

Filter archived addresses

  • Parameters
    • none

Usage:

php
// Get all archived addresses
$archivedAddresses = FluentCart\App\Models\CustomerAddresses::ofArchived()->get();

search($params) from CanSearch

Search addresses by parameters. Supports operators: =, between, like_all, in, not_in, is_null, is_not_null, and more.

  • Parameters
    • $params (Array) - Search parameters

Usage:

php
$addresses = FluentCart\App\Models\CustomerAddresses::search([
    'country' => ['value' => 'US', 'operator' => '=']
])->get();

Relations

This model has the following relationships that you can use

customer

Access the associated customer

  • return FluentCart\App\Models\Customer Model (BelongsTo)

Example:

php
// Accessing Customer
$customer = $customerAddress->customer;

// For Filtering by customer relationship
$customerAddresses = FluentCart\App\Models\CustomerAddresses::whereHas('customer', function($query) {
    $query->where('status', 'active');
})->get();

Methods

Along with Global Model methods, this model has few helper methods.

setMetaAttribute($value)

Set meta value with automatic JSON encoding (mutator). Called when setting $address->meta = [...].

  • Parameters
    • $value - mixed (array or other value)
  • Returns void

Usage

php
$customerAddress->meta = ['other_data' => ['company_name' => 'Acme Inc']];
// Automatically JSON encodes the value

getMetaAttribute($value)

Get meta value with automatic JSON decoding (accessor). Called when accessing $address->meta.

  • Parameters
    • $value - string (raw JSON from database)
  • Returns array

Usage

php
$meta = $customerAddress->meta; // Returns decoded array

setCompanyNameAttribute($value)

Set the company name inside the meta JSON field at other_data.company_name (mutator). Called when setting $address->company_name = '...'.

  • Parameters
    • $value - string
  • Returns void

Usage

php
$customerAddress->company_name = 'Acme Inc';
// Stores the value inside meta->other_data.company_name

getCompanyNameAttribute()

Get the company name from the meta JSON field at other_data.company_name (accessor).

  • Parameters
    • none
  • Returns string (empty string if not set)

Usage

php
$companyName = $customerAddress->company_name; // Returns company name or ''

getFormattedAddressAttribute()

Get formatted address as array (accessor). This is an appended attribute available as $address->formatted_address.

  • Parameters
    • none
  • Returns array

The returned array contains:

KeyDescription
countryFull country name (resolved from country code)
stateFull state name (resolved from state code)
cityCity
postcodePostal/ZIP code
address_1Primary address line
address_2Secondary address line
typeAddress type
nameFull name
first_nameFirst name
last_nameLast name
full_nameFull name
company_nameCompany name
labelAddress label
phonePhone number
full_addressComma-separated full address string

Usage

php
$formattedAddress = $customerAddress->formatted_address;
echo $formattedAddress['full_address']; // "Acme Inc, 123 Main St, New York, NY, United States"
echo $formattedAddress['country']; // "United States"

getFormattedDataForCheckout($prefix)

Get address data formatted for checkout forms with a configurable field prefix.

  • Parameters
    • $prefix - string (default: 'billing_')
  • Returns array

The returned array keys are prefixed with the given $prefix:

Key (with default prefix)Value
billing_full_nameName
billing_address_1Address line 1
billing_address_2Address line 2
billing_cityCity
billing_stateState
billing_phonePhone
billing_postcodePostcode
billing_countryCountry
billing_company_nameCompany name

Usage

php
$billingData = $customerAddress->getFormattedDataForCheckout(); // Uses 'billing_' prefix
$shippingData = $customerAddress->getFormattedDataForCheckout('shipping_'); // Uses 'shipping_' prefix

Usage Examples

Get Customer Addresses

php
$customer = FluentCart\App\Models\Customer::find(123);
$addresses = $customer->addresses;

foreach ($addresses as $address) {
    echo "Address Type: " . $address->type;
    echo "Label: " . $address->label;
    echo "Is Primary: " . ($address->is_primary ? 'Yes' : 'No');
}

Get Active Addresses

php
$activeAddresses = FluentCart\App\Models\CustomerAddresses::ofActive()->get();

Get Primary Address

php
$primaryAddress = FluentCart\App\Models\CustomerAddresses::where('customer_id', 123)
    ->where('is_primary', true)
    ->first();

Create Customer Address

php
$customerAddress = FluentCart\App\Models\CustomerAddresses::create([
    'customer_id' => 123,
    'is_primary' => true,
    'type' => 'billing',
    'status' => 'active',
    'label' => 'Home Address',
    'name' => 'John Doe',
    'address_1' => '123 Main Street',
    'city' => 'New York',
    'state' => 'NY',
    'postcode' => '10001',
    'country' => 'US',
    'phone' => '+1-555-123-4567',
    'email' => '[email protected]',
    'company_name' => 'Acme Inc'
]);

Get Formatted Address

php
$address = FluentCart\App\Models\CustomerAddresses::find(1);
$formatted = $address->formatted_address;
// Returns array with formatted address components including full_address string
echo $formatted['full_address'];

Get Checkout-Formatted Data

php
$address = FluentCart\App\Models\CustomerAddresses::find(1);
$billingFields = $address->getFormattedDataForCheckout('billing_');
$shippingFields = $address->getFormattedDataForCheckout('shipping_');

Archive Address

php
$address = FluentCart\App\Models\CustomerAddresses::find(1);
$address->status = 'archived';
$address->save();

FluentCart developer documentation