1
0
mirror of synced 2025-02-22 18:03:14 +03:00

Update logic work address

This commit is contained in:
Dima Uryvskiy 2021-08-27 16:17:18 +03:00 committed by GitHub
parent a92253c0a4
commit ea29da0025
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 398 additions and 329 deletions

View File

@ -0,0 +1,28 @@
# Address processing
С версии `4.3.8` изменена логика работы с адресами.
В заказ CRM c заказа WooCommerce будет передаваться, только shipping адрес. Если при оформлении заказа указан только billing адрес, то WooCommerce записывает в БД эти же данные в shipping, то есть shipping = billing.
При создании обычных/корпоративных клиентов в CRM будет передаваться billing адрес с заказа/пользователя WooCommerce. Если клиент гость и у него нет данных по billing адресу, тогда будет передан billing адрес заказа.
Для кастомизаций адресов в CRM, добавили новые фильтры:
* `retailcrm_process_order_address`
* `retailcrm_process_customer_address`
* `retailcrm_process_customer_corporate_address`
## Пример работы фильтров
В приведенном ниже примере показано, как возможно кастомизировать адрес заказа:
```php
<?php
add_filter('retailcrm_process_order_address', 'editOrderAddress', 10, 2);
function editOrderAddress($address, $order)
{
$address['text'] = 'Test';
return $address;
}
```

View File

@ -11,18 +11,6 @@
abstract class WC_Retailcrm_Abstracts_Address extends WC_Retailcrm_Abstracts_Data abstract class WC_Retailcrm_Abstracts_Address extends WC_Retailcrm_Abstracts_Data
{ {
const ADDRESS_TYPE_BILLING = 'billing';
const ADDRESS_TYPE_SHIPPING = 'shipping';
/** @var string $address_type */
protected $address_type = 'shipping';
/** @var bool $fallback_to_billing */
protected $fallback_to_billing = false;
/** @var bool $fallback_to_shipping */
protected $fallback_to_shipping = false;
/** @var array $data */ /** @var array $data */
protected $data = array( protected $data = array(
'index' => '', 'index' => '',
@ -47,63 +35,7 @@ abstract class WC_Retailcrm_Abstracts_Address extends WC_Retailcrm_Abstracts_Dat
} }
/** /**
* @param bool $fallback_to_billing * Returns shipping address from order.
*
* @return self
*/
public function setFallbackToBilling($fallback_to_billing)
{
$this->fallback_to_billing = $fallback_to_billing;
return $this;
}
/**
* @param bool $fallback_to_shipping
*
* @return WC_Retailcrm_Abstracts_Address
*/
public function setFallbackToShipping($fallback_to_shipping)
{
$this->fallback_to_shipping = $fallback_to_shipping;
return $this;
}
/**
* Sets woocommerce address type to work with
*
* @param string $addressType
*
* @return self
*/
public function setWCAddressType($addressType = WC_Retailcrm_Abstracts_Address::ADDRESS_TYPE_SHIPPING)
{
$this->address_type = $addressType;
return $this;
}
/**
* Validate address
*
* @param array $address
*
* @return bool
*/
public function validateAddress($address)
{
if (empty($address['country']) ||
empty($address['state']) ||
empty($address['postcode']) ||
empty($address['city']) ||
empty($address['address_1'])
) {
return false;
}
return true;
}
/**
* Returns address from order. Respects fallback_to_billing parameter.
* *
* @param \WC_Order $order * @param \WC_Order $order
* *
@ -111,18 +43,61 @@ abstract class WC_Retailcrm_Abstracts_Address extends WC_Retailcrm_Abstracts_Dat
*/ */
protected function getOrderAddress($order) protected function getOrderAddress($order)
{ {
$orderAddress = $order->get_address($this->address_type); if ($order === null) {
$checkEmptyArray = $this->validateAddress($orderAddress) ? array_filter($orderAddress) : array(); return array();
if (empty($checkEmptyArray) && $this->address_type === self::ADDRESS_TYPE_BILLING && $this->fallback_to_shipping) {
$orderAddress = $order->get_address(self::ADDRESS_TYPE_SHIPPING);
} }
if (empty($checkEmptyArray) && $this->address_type === self::ADDRESS_TYPE_SHIPPING && $this->fallback_to_billing) { $orderShippingAddress = array(
$orderAddress = $order->get_address(self::ADDRESS_TYPE_BILLING); 'postcode' => $order->get_shipping_postcode(),
'state' => $order->get_shipping_state(),
'city' => $order->get_shipping_city(),
'address_1' => $order->get_shipping_address_1(),
'address_2' => $order->get_shipping_address_2()
);
if (!empty($orderShippingAddress)) {
return array(
'index' => $orderShippingAddress['postcode'],
'city' => $orderShippingAddress['city'],
'region' => $this->get_state_name($order->get_shipping_country(), $orderShippingAddress['state']),
'text' => implode(' ', $orderShippingAddress)
);
}
}
/**
* Returns billing address from customer.
*
* @param \WC_Customer $customer
* @param \WC_Order $order
*
* @return array
*/
protected function getCustomerAddress($customer, $order)
{
if ($customer === null) {
return array();
} }
return $orderAddress; $customerBillingAddress = $customer->get_billing_address();
if ($order instanceof WC_Order && empty($customerBillingAddress)) {
return array(
'index' => $order->get_billing_postcode(),
'countryIso' => $order->get_billing_country(),
'region' => $this->get_state_name($order->get_billing_country(), $order->get_billing_state()),
'city' => $order->get_billing_city(),
'text' => $this->joinAddresses($order->get_billing_address_1(), $order->get_billing_address_2())
);
} else {
return array(
'index' => $customer->get_billing_postcode(),
'countryIso' => $customer->get_billing_country(),
'region' => $this->get_state_name($customer->get_billing_country(), $customer->get_billing_state()),
'city' => $customer->get_billing_city(),
'text' => $this->joinAddresses($customer->get_billing_address_1(), $customer->get_billing_address_2())
);
}
} }
/** /**
@ -135,15 +110,7 @@ abstract class WC_Retailcrm_Abstracts_Address extends WC_Retailcrm_Abstracts_Dat
*/ */
protected function joinAddresses($address1 = '', $address2 = '') protected function joinAddresses($address1 = '', $address2 = '')
{ {
if (empty($address1) && empty($address2)) { return implode(', ', array_filter(array($address1, $address2)));
return '';
}
if (empty($address2) && !empty($address1)) {
return $address1;
}
return $address1 . ', ' . $address2;
} }
/** /**

View File

@ -14,9 +14,6 @@
*/ */
abstract class WC_Retailcrm_Abstracts_Data abstract class WC_Retailcrm_Abstracts_Data
{ {
/** @var string */
protected $filter_name;
/** @var array */ /** @var array */
protected $data = array(); protected $data = array();
@ -57,14 +54,6 @@ abstract class WC_Retailcrm_Abstracts_Data
* @return array * @return array
*/ */
public function get_data() public function get_data()
{
return apply_filters('retailcrm_before_send_' . $this->filter_name, WC_Retailcrm_Plugin::clearArray($this->data));
}
/**
* @return array
*/
protected function get_data_without_filters()
{ {
return $this->data; return $this->data;
} }

View File

@ -15,16 +15,6 @@ if (!class_exists('WC_Retailcrm_Customers')) :
*/ */
class WC_Retailcrm_Customers class WC_Retailcrm_Customers
{ {
/**
* Administrator role
*/
const ADMIN_ROLE = 'administrator';
/**
* Every customer has this role
*/
const CUSTOMER_ROLE = 'customer';
/** @var bool | WC_Retailcrm_Proxy | \WC_Retailcrm_Client_V5 */ /** @var bool | WC_Retailcrm_Proxy | \WC_Retailcrm_Client_V5 */
protected $retailcrm; protected $retailcrm;
@ -222,12 +212,11 @@ if (!class_exists('WC_Retailcrm_Customers')) :
$found = false; $found = false;
$builder = new WC_Retailcrm_Customer_Corporate_Address(); $builder = new WC_Retailcrm_Customer_Corporate_Address();
$newAddress = $builder $newAddress = $builder
->setFallbackToShipping(true)
->setIsMain(false) ->setIsMain(false)
->setExplicitIsMain(false) ->setExplicitIsMain(false)
->setWCAddressType(WC_Retailcrm_Abstracts_Address::ADDRESS_TYPE_BILLING)
->build($customer, $order) ->build($customer, $order)
->get_data(); ->get_data();
$addresses = $this->retailcrm->customersCorporateAddresses( $addresses = $this->retailcrm->customersCorporateAddresses(
$corporateId, $corporateId,
array(), array(),
@ -327,17 +316,12 @@ if (!class_exists('WC_Retailcrm_Customers')) :
$firstName = $customer->get_first_name(); $firstName = $customer->get_first_name();
$lastName = $customer->get_last_name(); $lastName = $customer->get_last_name();
$billingPhone = $customer->get_billing_phone(); $billingPhone = $customer->get_billing_phone();
$email = $customer->get_billing_email(); $email = strtolower($customer->get_billing_email());
if (empty($firstName) && empty($lastName) && $order instanceof WC_Order) { if (empty($firstName) && empty($lastName) && $order instanceof WC_Order) {
$firstName = $order->get_billing_first_name(); $firstName = $order->get_billing_first_name();
$lastName = $order->get_billing_last_name(); $lastName = $order->get_billing_last_name();
if (empty($firstName) && empty($lastName)) {
$firstName = $order->get_shipping_first_name();
$lastName = $order->get_shipping_last_name();
}
if (empty($firstName)) { if (empty($firstName)) {
$firstName = $customer->get_username(); $firstName = $customer->get_username();
} }
@ -359,7 +343,7 @@ if (!class_exists('WC_Retailcrm_Customers')) :
'createdAt' => $createdAt->date('Y-m-d H:i:s'), 'createdAt' => $createdAt->date('Y-m-d H:i:s'),
'firstName' => $firstName ? $firstName : $customer->get_username(), 'firstName' => $firstName ? $firstName : $customer->get_username(),
'lastName' => $lastName, 'lastName' => $lastName,
'email' => $customer->get_billing_email(), 'email' => $email,
'address' => $this->customer_address->build($customer, $order)->get_data() 'address' => $this->customer_address->build($customer, $order)->get_data()
); );
@ -373,6 +357,21 @@ if (!class_exists('WC_Retailcrm_Customers')) :
); );
} }
// If the client is corporate, set the value isContact.
if ($this->isCorporateEnabled()) {
if ($order !== null) {
$company = $order->get_billing_company();
}
if (empty($company)) {
$company = $customer->get_billing_company();
}
if (!empty($company)) {
$data_customer['isContact'] = true;
}
}
$this->customer = apply_filters( $this->customer = apply_filters(
'retailcrm_process_customer', 'retailcrm_process_customer',
WC_Retailcrm_Plugin::clearArray($data_customer), WC_Retailcrm_Plugin::clearArray($data_customer),
@ -408,38 +407,33 @@ if (!class_exists('WC_Retailcrm_Customers')) :
) )
); );
$orderAddress = new WC_Retailcrm_Order_Address();
$corpAddress = new WC_Retailcrm_Customer_Corporate_Address(); $corpAddress = new WC_Retailcrm_Customer_Corporate_Address();
$address = $orderAddress $billingAddress = $corpAddress
->setWCAddressType(WC_Retailcrm_Abstracts_Address::ADDRESS_TYPE_BILLING)
->build($order)
->get_data();
$shippingAddress = $corpAddress
->setWCAddressType(WC_Retailcrm_Abstracts_Address::ADDRESS_TYPE_BILLING)
->setFallbackToBilling(true)
->setIsMain(true) ->setIsMain(true)
->build($customer, $order) ->build($customer, $order)
->get_data(); ->get_data();
if (isset($address['text'])) { if (!empty($billingAddress)) {
$data_company['contragent']['legalAddress'] = $address['text']; $data_company['contragent']['legalAddress'] = implode(
', ',
array(
$billingAddress['index'],
$billingAddress['city'],
$billingAddress['region'],
$billingAddress['text']
)
);
} }
$this->customerCorporateAddress = $billingAddress;
$this->customerCorporate = apply_filters( $this->customerCorporate = apply_filters(
'retailcrm_process_customer_corporate', 'retailcrm_process_customer_corporate',
WC_Retailcrm_Plugin::clearArray($data_customer), WC_Retailcrm_Plugin::clearArray($data_customer),
$customer $customer
); );
$this->customerCorporateAddress = apply_filters(
'retailcrm_process_customer_corporate_address',
WC_Retailcrm_Plugin::clearArray(array_merge(
$shippingAddress,
array('isMain' => true)
)),
$customer
);
$this->customerCorporateCompany = apply_filters( $this->customerCorporateCompany = apply_filters(
'retailcrm_process_customer_corporate_company', 'retailcrm_process_customer_corporate_company',
WC_Retailcrm_Plugin::clearArray($data_company), WC_Retailcrm_Plugin::clearArray($data_company),
@ -448,7 +442,7 @@ if (!class_exists('WC_Retailcrm_Customers')) :
} }
/** /**
* @param array $filter * @param array $filter Search customer by fields.
* *
* @return bool|array * @return bool|array
*/ */
@ -457,11 +451,16 @@ if (!class_exists('WC_Retailcrm_Customers')) :
if (isset($filter['externalId'])) { if (isset($filter['externalId'])) {
$search = $this->retailcrm->customersGet($filter['externalId']); $search = $this->retailcrm->customersGet($filter['externalId']);
} elseif (isset($filter['email'])) { } elseif (isset($filter['email'])) {
if (empty($filter['email']) && count($filter) == 1) { if (empty($filter['email'])) {
return false; return false;
} }
$search = $this->retailcrm->customersList(array('email' => $filter['email'])); // If customer not corporate, we need unset this field.
if (empty($filter['isContact'])) {
unset($filter['isContact']);
}
$search = $this->retailcrm->customersList($filter);
} }
if (!empty($search) && $search->isSuccessful()) { if (!empty($search) && $search->isSuccessful()) {
@ -472,7 +471,7 @@ if (!class_exists('WC_Retailcrm_Customers')) :
return false; return false;
} }
if (isset($filter['email']) && count($filter) == 1) { if (!empty($filter['email'])) {
foreach ($search['customers'] as $finding) { foreach ($search['customers'] as $finding) {
if (isset($finding['email']) && $finding['email'] == $filter['email']) { if (isset($finding['email']) && $finding['email'] == $filter['email']) {
$customer = $finding; $customer = $finding;
@ -495,12 +494,13 @@ if (!class_exists('WC_Retailcrm_Customers')) :
/** /**
* Returns customer data by externalId or by email, returns false in case of failure * Returns customer data by externalId or by email, returns false in case of failure
* *
* @param $customerExternalId * @param string $customerExternalId Customer externalId.
* @param $customerEmailOrPhone * @param string $customerEmailOrPhone Customer email or phone.
* @param bool $isContact Customer is the contact person.
* *
* @return array|bool * @return array|bool
*/ */
public function findCustomerEmailOrId($customerExternalId, $customerEmailOrPhone) public function findCustomerEmailOrId($customerExternalId, $customerEmailOrPhone, $isContact)
{ {
$customer = false; $customer = false;
@ -509,7 +509,7 @@ if (!class_exists('WC_Retailcrm_Customers')) :
} }
if (!$customer && !empty($customerEmailOrPhone)) { if (!$customer && !empty($customerEmailOrPhone)) {
$customer = $this->searchCustomer(array('email' => $customerEmailOrPhone)); $customer = $this->searchCustomer(array('email' => $customerEmailOrPhone, 'isContact' => $isContact));
} }
return $customer; return $customer;

View File

@ -187,9 +187,6 @@ if ( ! class_exists( 'WC_Retailcrm_History' ) ) :
protected function ordersHistory($date, $sinceId) protected function ordersHistory($date, $sinceId)
{ {
$filter = array('startDate' => $date); $filter = array('startDate' => $date);
unset($this->retailcrmSettings['client_roles']);
$options = array_flip(array_filter($this->retailcrmSettings)); $options = array_flip(array_filter($this->retailcrmSettings));
if ($sinceId) { if ($sinceId) {
@ -368,14 +365,14 @@ if ( ! class_exists( 'WC_Retailcrm_History' ) ) :
$wcOrder->set_billing_email($order['email']); $wcOrder->set_billing_email($order['email']);
} }
if (isset($order['company']['address'])) { if (isset($order['contact']['address'])) {
$billingAddress = $order['company']['address']; $billingAddress = $order['contact']['address'];
$wcOrder->set_billing_state(self::arrayValue($billingAddress, 'region', '--')); $wcOrder->set_billing_state(self::arrayValue($billingAddress, 'region', ''));
$wcOrder->set_billing_postcode(self::arrayValue($billingAddress, 'index', '--')); $wcOrder->set_billing_postcode(self::arrayValue($billingAddress, 'index', ''));
$wcOrder->set_billing_country(self::arrayValue($billingAddress, 'country', '--')); $wcOrder->set_billing_country(self::arrayValue($billingAddress, 'country', ''));
$wcOrder->set_billing_city(self::arrayValue($billingAddress, 'city', '--')); $wcOrder->set_billing_city(self::arrayValue($billingAddress, 'city', ''));
$wcOrder->set_billing_address_1(self::arrayValue($billingAddress, 'text', '--')); $wcOrder->set_billing_address_1(self::arrayValue($billingAddress, 'text', ''));
} }
} }
@ -569,7 +566,8 @@ if ( ! class_exists( 'WC_Retailcrm_History' ) ) :
return false; return false;
} }
if (is_array($this->orderMethods) if (
is_array($this->orderMethods)
&& $this->orderMethods && $this->orderMethods
&& isset($order['orderMethod']) && isset($order['orderMethod'])
&& !in_array($order['orderMethod'], $this->orderMethods) && !in_array($order['orderMethod'], $this->orderMethods)
@ -613,22 +611,18 @@ if ( ! class_exists( 'WC_Retailcrm_History' ) ) :
$wcOrder->set_date_created($order['createdAt']); $wcOrder->set_date_created($order['createdAt']);
$customer = $order['customer']; $customer = $order['customer'];
$contactOrCustomer = array(); $contactOrCustomer = array();
$address = isset($order['customer']['address']) ? $order['customer']['address'] : array(); $billingAddress = '';
$billingAddress = $address;
$companyName = '';
if ($this->retailcrm->getCorporateEnabled()) {
$billingAddress = isset($order['company']['address']) ? $order['company']['address'] : $address;
if (empty($billingAddress)) {
$billingAddress = $address;
}
}
if ($this->retailcrm->getCorporateEnabled() && self::isOrderCorporate($order)) { if ($this->retailcrm->getCorporateEnabled() && self::isOrderCorporate($order)) {
if (isset($order['contact'])) { if (isset($order['contact'])) {
$contactOrCustomer = $order['contact']; $contactOrCustomer = $order['contact'];
if (!empty($order['contact']['address'])) {
$billingAddress = $order['contact']['address'];
} else {
WC_Retailcrm_Logger::add(sprintf('[%d] => %s', $order['id'], 'Error: Contact address is empty'));
}
if (self::noRealDataInEntity($contactOrCustomer)) { if (self::noRealDataInEntity($contactOrCustomer)) {
$response = $this->retailcrm->customersGet($contactOrCustomer['id'], 'id'); $response = $this->retailcrm->customersGet($contactOrCustomer['id'], 'id');
@ -640,6 +634,13 @@ if ( ! class_exists( 'WC_Retailcrm_History' ) ) :
} else { } else {
$contactOrCustomer = $customer; $contactOrCustomer = $customer;
if (!empty($customer['address'])) {
$billingAddress = $customer['address'];
} else {
WC_Retailcrm_Logger::add(sprintf('[%d] => %s', $order['id'], 'Error: Customer address is empty'));
}
if (!self::isOrderCorporate($order) && self::noRealDataInEntity($contactOrCustomer)) { if (!self::isOrderCorporate($order) && self::noRealDataInEntity($contactOrCustomer)) {
$response = $this->retailcrm->customersGet($contactOrCustomer['id'], 'id'); $response = $this->retailcrm->customersGet($contactOrCustomer['id'], 'id');
@ -667,13 +668,15 @@ if ( ! class_exists( 'WC_Retailcrm_History' ) ) :
$wcOrder->set_customer_note($order['customerComment']); $wcOrder->set_customer_note($order['customerComment']);
} }
// TODO Check if that works; also don't forget to set this company field while creating order from CMS! $companyName = '';
if ($this->retailcrm->getCorporateEnabled()
if (
$this->retailcrm->getCorporateEnabled()
&& self::isOrderCorporate($order) && self::isOrderCorporate($order)
&& !empty($order['company']) && !empty($customer['mainCompany'])
&& isset($order['company']['name']) && isset($customer['mainCompany']['name'])
) { ) {
$companyName = $order['company']['name']; $companyName = $customer['mainCompany']['name'];
} }
$addressShipping = array( $addressShipping = array(

View File

@ -142,12 +142,6 @@ if ( ! class_exists( 'WC_Retailcrm_Orders' ) ) :
$firstName = $wcOrder->get_shipping_first_name(); $firstName = $wcOrder->get_shipping_first_name();
$lastName = $wcOrder->get_shipping_last_name(); $lastName = $wcOrder->get_shipping_last_name();
if(empty($firstName) && empty($lastName))
{
$firstName = $wcOrder->get_billing_first_name();
$lastName = $wcOrder->get_billing_last_name();
}
$this->order['firstName'] = $firstName; $this->order['firstName'] = $firstName;
$this->order['lastName'] = $lastName; $this->order['lastName'] = $lastName;
} }
@ -166,8 +160,13 @@ if ( ! class_exists( 'WC_Retailcrm_Orders' ) ) :
*/ */
protected function fillOrderCreate($wcCustomerId, $wcCustomerEmail, $wcOrder) protected function fillOrderCreate($wcCustomerId, $wcCustomerEmail, $wcOrder)
{ {
$foundCustomerId = ''; $isContact = $this->retailcrm->getCorporateEnabled() && static::isCorporateOrder($wcOrder);
$foundCustomer = $this->customers->findCustomerEmailOrId($wcCustomerId, $wcCustomerEmail);
$foundCustomer = $this->customers->findCustomerEmailOrId(
$wcCustomerId,
strtolower($wcCustomerEmail),
$isContact
);
if (empty($foundCustomer)) { if (empty($foundCustomer)) {
$foundCustomerId = $this->customers->createCustomer($wcCustomerId, $wcOrder); $foundCustomerId = $this->customers->createCustomer($wcCustomerId, $wcOrder);
@ -368,12 +367,8 @@ if ( ! class_exists( 'WC_Retailcrm_Orders' ) ) :
} }
} }
$order_data['delivery']['address'] = $this->order_address->build($order)->get_data();
$order_items = array(); $order_items = array();
$order_data['delivery']['address'] = $this->order_address
->setFallbackToBilling(true)
->setWCAddressType(WC_Retailcrm_Abstracts_Address::ADDRESS_TYPE_SHIPPING)
->build($order)
->get_data();
/** @var WC_Order_Item_Product $item */ /** @var WC_Order_Item_Product $item */
foreach ($order->get_items() as $item) { foreach ($order->get_items() as $item) {

View File

@ -1,4 +1,5 @@
<?php <?php
/** /**
* PHP version 5.3 * PHP version 5.3
* *
@ -14,8 +15,6 @@
*/ */
class WC_Retailcrm_Customer_Address extends WC_Retailcrm_Abstracts_Address class WC_Retailcrm_Customer_Address extends WC_Retailcrm_Abstracts_Address
{ {
protected $filter_name = 'customer_address';
/** /**
* @param WC_Customer $customer * @param WC_Customer $customer
* @param \WC_Order|null $order * @param \WC_Order|null $order
@ -24,28 +23,21 @@ class WC_Retailcrm_Customer_Address extends WC_Retailcrm_Abstracts_Address
*/ */
public function build($customer, $order = null) public function build($customer, $order = null)
{ {
$customerBillingAddress = $customer->get_billing_address(); $address = $this->getCustomerAddress($customer, $order);
if ($order instanceof WC_Order && empty($customerBillingAddress)) { if (!empty($address)) {
$data = array( $customerAddress = apply_filters(
'index' => $order->get_billing_postcode(), 'retailcrm_process_customer_address',
'countryIso' => $order->get_billing_country(), WC_Retailcrm_Plugin::clearArray($address),
'region' => $this->get_state_name($order->get_billing_country(), $order->get_billing_state()), $customer,
'city' => $order->get_billing_city(), $order
'text' => $order->get_billing_address_1() . ', ' . $order->get_billing_address_2()
); );
$this->set_data_fields($customerAddress);
} else { } else {
$data = array( WC_Retailcrm_Logger::add('Error Customer address is empty');
'index' => $customer->get_billing_postcode(),
'countryIso' => $customer->get_billing_country(),
'region' => $this->get_state_name($customer->get_billing_country(), $customer->get_billing_state()),
'city' => $customer->get_billing_city(),
'text' => $customer->get_billing_address_1() . ', ' . $customer->get_billing_address_2()
);
} }
$this->set_data_fields($data);
return $this; return $this;
} }
} }

View File

@ -14,39 +14,12 @@
*/ */
class WC_Retailcrm_Customer_Corporate_Address extends WC_Retailcrm_Abstracts_Address class WC_Retailcrm_Customer_Corporate_Address extends WC_Retailcrm_Abstracts_Address
{ {
/** @var string $filter_name */
protected $filter_name = 'customer_address';
/** @var bool $isMain */ /** @var bool $isMain */
protected $isMain = true; protected $isMain = true;
/** @var bool $explicitIsMain */ /** @var bool $explicitIsMain */
protected $explicitIsMain; protected $explicitIsMain;
/**
* Sets woocommerce address type to work with
*
* @param string $addressType
*
* @return \WC_Retailcrm_Customer_Corporate_Address
*/
public function setWCAddressType($addressType = WC_Retailcrm_Abstracts_Address::ADDRESS_TYPE_SHIPPING)
{
$this->address_type = $addressType;
return $this;
}
/**
* @param bool $fallback_to_shipping
*
* @return WC_Retailcrm_Customer_Corporate_Address
*/
public function setFallbackToShipping($fallback_to_shipping)
{
$this->fallback_to_shipping = $fallback_to_shipping;
return $this;
}
/** /**
* @param bool $isMain * @param bool $isMain
* *
@ -77,82 +50,30 @@ class WC_Retailcrm_Customer_Corporate_Address extends WC_Retailcrm_Abstracts_Add
*/ */
public function build($customer, $order = null) public function build($customer, $order = null)
{ {
if ($order instanceof WC_Order) { $address = $this->getCustomerAddress($customer, $order);
$address = $this->getOrderAddress($order);
$data = array( if (!empty($address)) {
'index' => $address['postcode'], if ($this->isMain) {
'countryIso' => $address['country'], $address['isMain'] = true;
'region' => $address['state'], } elseif ($this->explicitIsMain) {
'city' => $address['city'], $address['isMain'] = false;
'name' => $address['company'], }
'text' => $this->joinAddresses($address['address_1'], $address['address_2'])
$corporateCustomerAddress = apply_filters(
'retailcrm_process_customer_corporate_address',
WC_Retailcrm_Plugin::clearArray(array_merge(
$address,
array('isMain' => $address['isMain'])
)),
$customer
); );
} else if (self::ADDRESS_TYPE_SHIPPING === $this->address_type) {
$data = $this->getCustomerShippingAddress($customer);
if (empty($address) && $this->fallback_to_billing) { $this->set_data_fields($corporateCustomerAddress);
$data = $this->getCustomerBillingAddress($customer); } else {
} WC_Retailcrm_Logger::add('Error Corporate Customer address is empty');
} elseif (self::ADDRESS_TYPE_BILLING === $this->address_type) {
$data = $this->getCustomerBillingAddress($customer);
if (empty($address) && $this->fallback_to_shipping) {
$data = $this->getCustomerShippingAddress($customer);
}
} }
if ($this->isMain) {
$data['isMain'] = true;
} elseif ($this->explicitIsMain) {
$data['isMain'] = false;
}
$this->set_data_fields($data);
return $this; return $this;
} }
/**
* Returns built customer billing address
*
* @param \WC_Customer|\WP_User $customer
*
* @return array
*/
public function getCustomerBillingAddress($customer)
{
return array(
'index' => $customer->get_billing_postcode(),
'countryIso' => $customer->get_billing_country(),
'region' => $customer->get_billing_state(),
'city' => $customer->get_billing_city(),
'name' => $customer->get_billing_company(),
'text' => $this->joinAddresses(
$customer->get_billing_address_1(),
$customer->get_billing_address_2()
)
);
}
/**
* Returns built customer shipping address
*
* @param \WC_Customer|\WP_User $customer
*
* @return array
*/
public function getCustomerShippingAddress($customer)
{
return array(
'index' => $customer->get_shipping_postcode(),
'countryIso' => $customer->get_shipping_country(),
'region' => $customer->get_shipping_state(),
'city' => $customer->get_shipping_city(),
'name' => $customer->get_shipping_company(),
'text' => $this->joinAddresses(
$customer->get_shipping_address_1(),
$customer->get_shipping_address_2()
)
);
}
} }

View File

@ -11,9 +11,6 @@
class WC_Retailcrm_Order_Address extends WC_Retailcrm_Abstracts_Address class WC_Retailcrm_Order_Address extends WC_Retailcrm_Abstracts_Address
{ {
/** @var string $filter_name */
protected $filter_name = 'order_address';
/** /**
* @param WC_Order $order * @param WC_Order $order
* *
@ -24,24 +21,17 @@ class WC_Retailcrm_Order_Address extends WC_Retailcrm_Abstracts_Address
$address = $this->getOrderAddress($order); $address = $this->getOrderAddress($order);
if (!empty($address)) { if (!empty($address)) {
$data = array( $orderAddress = apply_filters(
'index' => $address['postcode'], 'retailcrm_process_order_address',
'city' => $address['city'], WC_Retailcrm_Plugin::clearArray($address),
'region' => $this->get_state_name($address['country'], $address['state']) $order
); );
$this->set_data_fields($data); $this->set_data_fields($orderAddress);
} else {
WC_Retailcrm_Logger::add('Error: Order address is empty');
} }
$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; return $this;
} }
} }

View File

@ -18,11 +18,18 @@ class WC_Retailcrm_Customer_Address_Test extends WC_Retailcrm_Test_Case_Helper
parent::setUp(); parent::setUp();
$this->customer = WC_Helper_Customer::create_customer(); $this->customer = WC_Helper_Customer::create_customer();
$this->customer->set_billing_country('CO');
$this->customer->set_billing_postcode('000000');
$this->customer->set_billing_state('TestState');
$this->customer->set_billing_city('TestCity');
$this->customer->set_billing_address_1('TestAddress1');
$this->customer->set_billing_address_2('TestAddress2');
} }
public function test_build() public function test_build_and_reset_address()
{ {
$customer_address = new WC_Retailcrm_Customer_Address; $customer_address = new WC_Retailcrm_Customer_Address();
$data = $customer_address->build($this->customer)->get_data(); $data = $customer_address->build($this->customer)->get_data();
$this->assertArrayHasKey('index', $data); $this->assertArrayHasKey('index', $data);
@ -30,10 +37,40 @@ class WC_Retailcrm_Customer_Address_Test extends WC_Retailcrm_Test_Case_Helper
$this->assertArrayHasKey('region', $data); $this->assertArrayHasKey('region', $data);
$this->assertArrayHasKey('text', $data); $this->assertArrayHasKey('text', $data);
$this->assertArrayHasKey('countryIso', $data); $this->assertArrayHasKey('countryIso', $data);
$this->assertNotEmpty($data['index']); $this->assertEquals('000000', $data['index']);
$this->assertNotEmpty($data['city']); $this->assertEquals('TestCity', $data['city']);
$this->assertNotEmpty($data['region']); $this->assertEquals('TestState', $data['region']);
$this->assertNotEmpty($data['text']); $this->assertEquals('TestAddress1, TestAddress2', $data['text']);
$this->assertNotEmpty($data['countryIso']); $this->assertEquals('CO', $data['countryIso']);
// Check reset customer address data
$customer_address->reset_data();
$data = $customer_address->get_data();
$this->assertArrayHasKey('index', $data);
$this->assertArrayHasKey('city', $data);
$this->assertArrayHasKey('region', $data);
$this->assertArrayHasKey('text', $data);
$this->assertEquals('', $data['index']);
$this->assertEquals('', $data['city']);
$this->assertEquals('', $data['region']);
$this->assertEquals('', $data['text']);
}
public function test_empty_address()
{
$customer_address = new WC_Retailcrm_Customer_Address();
$data = $customer_address->build(null)->get_data();
$this->assertArrayHasKey('index', $data);
$this->assertArrayHasKey('city', $data);
$this->assertArrayHasKey('region', $data);
$this->assertArrayHasKey('text', $data);
$this->assertEquals('', $data['index']);
$this->assertEquals('', $data['city']);
$this->assertEquals('', $data['region']);
$this->assertEquals('', $data['text']);
} }
} }

View File

@ -0,0 +1,110 @@
<?php
/**
* PHP version 5.3
*
* @category Integration
* @author RetailCRM <integration@retailcrm.ru>
* @license http://retailcrm.ru Proprietary
* @link http://retailcrm.ru
* @see http://help.retailcrm.ru
*/
class WC_Retailcrm_Customer_Corporate_Address_Test extends WC_Retailcrm_Test_Case_Helper
{
protected $customer;
public function setUp()
{
parent::setUp();
$this->customer = WC_Helper_Customer::create_customer();
$this->customer->set_billing_country('CO');
$this->customer->set_billing_postcode('000000');
$this->customer->set_billing_state('TestState');
$this->customer->set_billing_city('TestCity');
$this->customer->set_billing_address_1('TestAddress1');
$this->customer->set_billing_address_2('TestAddress2');
}
public function test_build_and_reset_address()
{
$customer_address = new WC_Retailcrm_Customer_Corporate_Address();
$data = $customer_address
->setIsMain(true)
->setExplicitIsMain(false)
->build($this->customer)
->get_data();
$this->assertArrayHasKey('index', $data);
$this->assertArrayHasKey('city', $data);
$this->assertArrayHasKey('region', $data);
$this->assertArrayHasKey('text', $data);
$this->assertArrayHasKey('countryIso', $data);
$this->assertArrayHasKey('isMain', $data);
$this->assertEquals('000000', $data['index']);
$this->assertEquals('TestCity', $data['city']);
$this->assertEquals('TestState', $data['region']);
$this->assertEquals('TestAddress1, TestAddress2', $data['text']);
$this->assertEquals('CO', $data['countryIso']);
$this->assertEquals(true, $data['isMain']);
// Check reset customer corporate address data
$customer_address->reset_data();
$data = $customer_address->get_data();
$this->assertArrayHasKey('index', $data);
$this->assertArrayHasKey('city', $data);
$this->assertArrayHasKey('region', $data);
$this->assertArrayHasKey('text', $data);
$this->assertEquals('', $data['index']);
$this->assertEquals('', $data['city']);
$this->assertEquals('', $data['region']);
$this->assertEquals('', $data['text']);
}
public function test_build_not_main_company()
{
$customer_address = new WC_Retailcrm_Customer_Corporate_Address();
$data = $customer_address
->setIsMain(false)
->setExplicitIsMain(true)
->build($this->customer)
->get_data();
$this->assertArrayHasKey('index', $data);
$this->assertArrayHasKey('city', $data);
$this->assertArrayHasKey('region', $data);
$this->assertArrayHasKey('text', $data);
$this->assertArrayHasKey('countryIso', $data);
$this->assertArrayHasKey('isMain', $data);
$this->assertEquals('000000', $data['index']);
$this->assertEquals('TestCity', $data['city']);
$this->assertEquals('TestState', $data['region']);
$this->assertEquals('TestAddress1, TestAddress2', $data['text']);
$this->assertEquals('CO', $data['countryIso']);
$this->assertEquals(false, $data['isMain']);
}
public function test_empty_address()
{
$customer_address = new WC_Retailcrm_Customer_Corporate_Address();
$data = $customer_address
->setIsMain(false)
->setExplicitIsMain(true)
->build(null)
->get_data();
$this->assertArrayHasKey('index', $data);
$this->assertArrayHasKey('city', $data);
$this->assertArrayHasKey('region', $data);
$this->assertArrayHasKey('text', $data);
$this->assertEquals('', $data['index']);
$this->assertEquals('', $data['city']);
$this->assertEquals('', $data['region']);
$this->assertEquals('', $data['text']);
}
}

View File

@ -22,19 +22,56 @@ class WC_Retailcrm_Order_Address_Test extends WC_Retailcrm_Test_Case_Helper
parent::setUp(); parent::setUp();
$this->order = WC_Helper_Order::create_order(); $this->order = WC_Helper_Order::create_order();
$this->order->set_shipping_postcode('000000');
$this->order->set_shipping_state('TestState');
$this->order->set_shipping_city('TestCity');
$this->order->set_shipping_address_1('TestAddress1');
$this->order->set_shipping_address_2('TestAddress2');
} }
public function test_build() public function test_build_and_reset_address()
{ {
$order_address = new WC_Retailcrm_Order_Address(); $order_address = new WC_Retailcrm_Order_Address();
$data = $order_address $data = $order_address->build($this->order)->get_data();
->setWCAddressType(WC_Retailcrm_Abstracts_Address::ADDRESS_TYPE_BILLING)
->build($this->order)
->get_data();
$this->assertArrayHasKey('index', $data); $this->assertArrayHasKey('index', $data);
$this->assertArrayHasKey('city', $data); $this->assertArrayHasKey('city', $data);
$this->assertArrayHasKey('region', $data); $this->assertArrayHasKey('region', $data);
$this->assertArrayHasKey('text', $data); $this->assertArrayHasKey('text', $data);
$this->assertEquals('000000', $data['index']);
$this->assertEquals('TestCity', $data['city']);
$this->assertEquals('TestState', $data['region']);
$this->assertEquals('000000 TestState TestCity TestAddress1 TestAddress2', $data['text']);
// Check reset order address data
$order_address->reset_data();
$data = $order_address->get_data();
$this->assertArrayHasKey('index', $data);
$this->assertArrayHasKey('city', $data);
$this->assertArrayHasKey('region', $data);
$this->assertArrayHasKey('text', $data);
$this->assertEquals('', $data['index']);
$this->assertEquals('', $data['city']);
$this->assertEquals('', $data['region']);
$this->assertEquals('', $data['text']);
}
public function test_empty_address()
{
$order_address = new WC_Retailcrm_Order_Address();
$data = $order_address->build(null)->get_data();
$this->assertArrayHasKey('index', $data);
$this->assertArrayHasKey('city', $data);
$this->assertArrayHasKey('region', $data);
$this->assertArrayHasKey('text', $data);
$this->assertEquals('', $data['index']);
$this->assertEquals('', $data['city']);
$this->assertEquals('', $data['region']);
$this->assertEquals('', $data['text']);
} }
} }