1
0
Fork 0
mirror of synced 2025-04-06 07:13:33 +03:00

Update get address in order

This commit is contained in:
dima-uryvskiy 2021-05-01 18:18:09 +03:00
parent d1d2037bf0
commit 0526ac2e7f
2 changed files with 44 additions and 31 deletions

View file

@ -82,24 +82,24 @@ abstract class WC_Retailcrm_Abstracts_Address extends WC_Retailcrm_Abstracts_Dat
}
/**
* Validate address
* Validate order address
*
* @param array $address
*
* @return bool
*/
public function validateAddress($address)
private function validateAddress($address)
{
if (empty($address['country']) ||
empty($address['state']) ||
empty($address['postcode']) ||
empty($address['city']) ||
empty($address['address_1'])
if (
!empty($address['country']) &&
!empty($address['state']) &&
!empty($address['city']) &&
!empty($address['address_1'])
) {
return false;
return true;
}
return true;
return false;
}
/**
@ -111,20 +111,26 @@ abstract class WC_Retailcrm_Abstracts_Address extends WC_Retailcrm_Abstracts_Dat
*/
protected function getOrderAddress($order)
{
$orderAddress = $order->get_address($this->address_type);
$checkEmptyArray = $this->validateAddress($orderAddress) ? array_filter($orderAddress) : array();
if (empty($checkEmptyArray) && $this->address_type === self::ADDRESS_TYPE_BILLING && $this->fallback_to_shipping) {
$orderAddress = $order->get_address(self::ADDRESS_TYPE_SHIPPING);
}
$shippingAddress = array_filter($order->get_address(self::ADDRESS_TYPE_SHIPPING));
$billingAddress = array_filter($order->get_address(self::ADDRESS_TYPE_BILLING));
if (empty($checkEmptyArray) && $this->address_type === self::ADDRESS_TYPE_SHIPPING && $this->fallback_to_billing) {
$orderAddress = $order->get_address(self::ADDRESS_TYPE_BILLING);
if ($this->validateAddress($shippingAddress)) {
// If shipping address full return his.
return $shippingAddress;
} elseif ($this->validateAddress($billingAddress)) {
// If billing address full return his.
return $billingAddress;
} else {
// If address not full, but somebody fields have data.
if (!empty($shippingAddress)) {
return $shippingAddress;
} elseif (!empty($billingAddress)) {
return $billingAddress;
}
}
return $orderAddress;
}
/**
* Glue two addresses
*

View file

@ -23,25 +23,32 @@ class WC_Retailcrm_Order_Address extends WC_Retailcrm_Abstracts_Address
{
$address = $this->getOrderAddress($order);
$postcode = isset($address['postcode']) ? $address['postcode'] : '';
$city = isset($address['city']) ? $address['city'] : '';
$state = isset($address['state']) ? $address['state'] : '';
$country = isset($address['country']) ? $address['country'] : '';
$region = $this->get_state_name($country, $state);
$address_1 = isset($address['address_1']) ? $address['address_1'] : '';
$address_2 = isset($address['address_2']) ? $address['address_2'] : '';
if (!empty($address)) {
$data = array(
'index' => $address['postcode'],
'city' => $address['city'],
'region' => $this->get_state_name($address['country'], $address['state'])
'index' => $postcode,
'city' => $city,
'region' => $region,
'text' => sprintf(
'%s %s %s %s %s',
$postcode,
$state,
$city,
$address_1,
$address_2
)
);
$this->set_data_fields($data);
}
$this->set_data_field('text', sprintf(
"%s %s %s %s %s",
$address['postcode'],
$address['state'],
$address['city'],
$address['address_1'],
$address['address_2']
));
return $this;
}
}