AddressCheckoutComponent::setData PHP Method

setData() public method

Create a new address if the existing address has changed, or is not yet created.
public setData ( Order $order, array $data )
$order Order order to get addresses from
$data array data to set
    public function setData(Order $order, array $data)
    {
        $address = $this->getAddress($order);
        //if the value matches the current address then unset
        //this is to fix issues with blank fields & the readonly Country field
        $addressfields = Address::database_fields(get_class($address));
        foreach ($data as $key => $value) {
            if (!isset($addressfields[$key]) || !$value && !$address->{$key}) {
                unset($data[$key]);
            }
        }
        $address->update($data);
        //if only one country is available, then set it
        if ($country = SiteConfig::current_site_config()->getSingleCountry()) {
            $address->Country = $country;
        }
        //write new address, or duplicate if changed
        if (!$address->isInDB()) {
            $address->write();
        } elseif ($address->isChanged()) {
            $address = $address->duplicate();
        }
        //set billing address, if not already set
        $order->{$this->addresstype . "AddressID"} = $address->ID;
        if (!$order->BillingAddressID) {
            $order->BillingAddressID = $address->ID;
        }
        $order->write();
        //update user info based on shipping address
        if ($this->addresstype === "Shipping") {
            ShopUserInfo::singleton()->setAddress($address);
            Zone::cache_zone_ids($address);
        }
        //associate member to address
        if ($member = Member::currentUser()) {
            $default = $member->{"Default" . $this->addresstype . "Address"}();
            //set default address
            if (!$default->exists()) {
                $member->{"Default" . $this->addresstype . "AddressID"} = $address->ID;
                $member->write();
            }
            if ($this->addtoaddressbook) {
                $member->AddressBook()->add($address);
            }
        }
        //extension hooks
        $order->extend('onSet' . $this->addresstype . 'Address', $address);
    }

Usage Example

 public function setData(Order $order, array $data)
 {
     if (!isset($data[$this->addresstype . 'ToSameAddress'])) {
         parent::setData($order, $data);
     } else {
         $order->{$this->addresstype . "AddressID"} = $order->{$this->useAddressType . "AddressID"};
         if (!$order->BillingAddressID) {
             $order->BillingAddressID = $order->{$this->useAddressType . "AddressID"};
         }
     }
     // FIX for missing name fields
     $fields = array_intersect_key($data, array_flip(['FirstName', 'Surname', 'Name', 'Email']));
     $changed = false;
     foreach ($fields as $field => $value) {
         if (!$order->{$this->addresstype . "Address"}->{$field}) {
             $order->{$this->addresstype . "Address"}->{$field} = $value;
             $changed = true;
         }
     }
     if ($changed) {
         $order->{$this->addresstype . "Address"}->write();
     }
 }
All Usage Examples Of AddressCheckoutComponent::setData