Update logic work address
This commit is contained in:
parent
a92253c0a4
commit
ea29da0025
28
doc/2. Workflow/Address.md
Normal file
28
doc/2. Workflow/Address.md
Normal 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;
|
||||
}
|
||||
```
|
@ -11,18 +11,6 @@
|
||||
|
||||
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 */
|
||||
protected $data = array(
|
||||
'index' => '',
|
||||
@ -47,63 +35,7 @@ abstract class WC_Retailcrm_Abstracts_Address extends WC_Retailcrm_Abstracts_Dat
|
||||
}
|
||||
|
||||
/**
|
||||
* @param bool $fallback_to_billing
|
||||
*
|
||||
* @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.
|
||||
* Returns shipping address from order.
|
||||
*
|
||||
* @param \WC_Order $order
|
||||
*
|
||||
@ -111,18 +43,61 @@ 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);
|
||||
if ($order === null) {
|
||||
return array();
|
||||
}
|
||||
|
||||
if (empty($checkEmptyArray) && $this->address_type === self::ADDRESS_TYPE_SHIPPING && $this->fallback_to_billing) {
|
||||
$orderAddress = $order->get_address(self::ADDRESS_TYPE_BILLING);
|
||||
$orderShippingAddress = array(
|
||||
'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 = '')
|
||||
{
|
||||
if (empty($address1) && empty($address2)) {
|
||||
return '';
|
||||
}
|
||||
|
||||
if (empty($address2) && !empty($address1)) {
|
||||
return $address1;
|
||||
}
|
||||
|
||||
return $address1 . ', ' . $address2;
|
||||
return implode(', ', array_filter(array($address1, $address2)));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -14,9 +14,6 @@
|
||||
*/
|
||||
abstract class WC_Retailcrm_Abstracts_Data
|
||||
{
|
||||
/** @var string */
|
||||
protected $filter_name;
|
||||
|
||||
/** @var array */
|
||||
protected $data = array();
|
||||
|
||||
@ -57,14 +54,6 @@ abstract class WC_Retailcrm_Abstracts_Data
|
||||
* @return array
|
||||
*/
|
||||
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;
|
||||
}
|
||||
|
@ -15,16 +15,6 @@ if (!class_exists('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 */
|
||||
protected $retailcrm;
|
||||
|
||||
@ -222,12 +212,11 @@ if (!class_exists('WC_Retailcrm_Customers')) :
|
||||
$found = false;
|
||||
$builder = new WC_Retailcrm_Customer_Corporate_Address();
|
||||
$newAddress = $builder
|
||||
->setFallbackToShipping(true)
|
||||
->setIsMain(false)
|
||||
->setExplicitIsMain(false)
|
||||
->setWCAddressType(WC_Retailcrm_Abstracts_Address::ADDRESS_TYPE_BILLING)
|
||||
->build($customer, $order)
|
||||
->get_data();
|
||||
|
||||
$addresses = $this->retailcrm->customersCorporateAddresses(
|
||||
$corporateId,
|
||||
array(),
|
||||
@ -327,17 +316,12 @@ if (!class_exists('WC_Retailcrm_Customers')) :
|
||||
$firstName = $customer->get_first_name();
|
||||
$lastName = $customer->get_last_name();
|
||||
$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) {
|
||||
$firstName = $order->get_billing_first_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)) {
|
||||
$firstName = $customer->get_username();
|
||||
}
|
||||
@ -359,7 +343,7 @@ if (!class_exists('WC_Retailcrm_Customers')) :
|
||||
'createdAt' => $createdAt->date('Y-m-d H:i:s'),
|
||||
'firstName' => $firstName ? $firstName : $customer->get_username(),
|
||||
'lastName' => $lastName,
|
||||
'email' => $customer->get_billing_email(),
|
||||
'email' => $email,
|
||||
'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(
|
||||
'retailcrm_process_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();
|
||||
|
||||
$address = $orderAddress
|
||||
->setWCAddressType(WC_Retailcrm_Abstracts_Address::ADDRESS_TYPE_BILLING)
|
||||
->build($order)
|
||||
->get_data();
|
||||
|
||||
$shippingAddress = $corpAddress
|
||||
->setWCAddressType(WC_Retailcrm_Abstracts_Address::ADDRESS_TYPE_BILLING)
|
||||
->setFallbackToBilling(true)
|
||||
$billingAddress = $corpAddress
|
||||
->setIsMain(true)
|
||||
->build($customer, $order)
|
||||
->get_data();
|
||||
|
||||
if (isset($address['text'])) {
|
||||
$data_company['contragent']['legalAddress'] = $address['text'];
|
||||
if (!empty($billingAddress)) {
|
||||
$data_company['contragent']['legalAddress'] = implode(
|
||||
', ',
|
||||
array(
|
||||
$billingAddress['index'],
|
||||
$billingAddress['city'],
|
||||
$billingAddress['region'],
|
||||
$billingAddress['text']
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
$this->customerCorporateAddress = $billingAddress;
|
||||
|
||||
$this->customerCorporate = apply_filters(
|
||||
'retailcrm_process_customer_corporate',
|
||||
WC_Retailcrm_Plugin::clearArray($data_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(
|
||||
'retailcrm_process_customer_corporate_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
|
||||
*/
|
||||
@ -457,11 +451,16 @@ if (!class_exists('WC_Retailcrm_Customers')) :
|
||||
if (isset($filter['externalId'])) {
|
||||
$search = $this->retailcrm->customersGet($filter['externalId']);
|
||||
} elseif (isset($filter['email'])) {
|
||||
if (empty($filter['email']) && count($filter) == 1) {
|
||||
if (empty($filter['email'])) {
|
||||
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()) {
|
||||
@ -472,7 +471,7 @@ if (!class_exists('WC_Retailcrm_Customers')) :
|
||||
return false;
|
||||
}
|
||||
|
||||
if (isset($filter['email']) && count($filter) == 1) {
|
||||
if (!empty($filter['email'])) {
|
||||
foreach ($search['customers'] as $finding) {
|
||||
if (isset($finding['email']) && $finding['email'] == $filter['email']) {
|
||||
$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
|
||||
*
|
||||
* @param $customerExternalId
|
||||
* @param $customerEmailOrPhone
|
||||
* @param string $customerExternalId Customer externalId.
|
||||
* @param string $customerEmailOrPhone Customer email or phone.
|
||||
* @param bool $isContact Customer is the contact person.
|
||||
*
|
||||
* @return array|bool
|
||||
*/
|
||||
public function findCustomerEmailOrId($customerExternalId, $customerEmailOrPhone)
|
||||
public function findCustomerEmailOrId($customerExternalId, $customerEmailOrPhone, $isContact)
|
||||
{
|
||||
$customer = false;
|
||||
|
||||
@ -509,7 +509,7 @@ if (!class_exists('WC_Retailcrm_Customers')) :
|
||||
}
|
||||
|
||||
if (!$customer && !empty($customerEmailOrPhone)) {
|
||||
$customer = $this->searchCustomer(array('email' => $customerEmailOrPhone));
|
||||
$customer = $this->searchCustomer(array('email' => $customerEmailOrPhone, 'isContact' => $isContact));
|
||||
}
|
||||
|
||||
return $customer;
|
||||
|
@ -187,9 +187,6 @@ if ( ! class_exists( 'WC_Retailcrm_History' ) ) :
|
||||
protected function ordersHistory($date, $sinceId)
|
||||
{
|
||||
$filter = array('startDate' => $date);
|
||||
|
||||
unset($this->retailcrmSettings['client_roles']);
|
||||
|
||||
$options = array_flip(array_filter($this->retailcrmSettings));
|
||||
|
||||
if ($sinceId) {
|
||||
@ -368,14 +365,14 @@ if ( ! class_exists( 'WC_Retailcrm_History' ) ) :
|
||||
$wcOrder->set_billing_email($order['email']);
|
||||
}
|
||||
|
||||
if (isset($order['company']['address'])) {
|
||||
$billingAddress = $order['company']['address'];
|
||||
if (isset($order['contact']['address'])) {
|
||||
$billingAddress = $order['contact']['address'];
|
||||
|
||||
$wcOrder->set_billing_state(self::arrayValue($billingAddress, 'region', '--'));
|
||||
$wcOrder->set_billing_postcode(self::arrayValue($billingAddress, 'index', '--'));
|
||||
$wcOrder->set_billing_country(self::arrayValue($billingAddress, 'country', '--'));
|
||||
$wcOrder->set_billing_city(self::arrayValue($billingAddress, 'city', '--'));
|
||||
$wcOrder->set_billing_address_1(self::arrayValue($billingAddress, 'text', '--'));
|
||||
$wcOrder->set_billing_state(self::arrayValue($billingAddress, 'region', ''));
|
||||
$wcOrder->set_billing_postcode(self::arrayValue($billingAddress, 'index', ''));
|
||||
$wcOrder->set_billing_country(self::arrayValue($billingAddress, 'country', ''));
|
||||
$wcOrder->set_billing_city(self::arrayValue($billingAddress, 'city', ''));
|
||||
$wcOrder->set_billing_address_1(self::arrayValue($billingAddress, 'text', ''));
|
||||
}
|
||||
}
|
||||
|
||||
@ -569,7 +566,8 @@ if ( ! class_exists( 'WC_Retailcrm_History' ) ) :
|
||||
return false;
|
||||
}
|
||||
|
||||
if (is_array($this->orderMethods)
|
||||
if (
|
||||
is_array($this->orderMethods)
|
||||
&& $this->orderMethods
|
||||
&& isset($order['orderMethod'])
|
||||
&& !in_array($order['orderMethod'], $this->orderMethods)
|
||||
@ -613,22 +611,18 @@ if ( ! class_exists( 'WC_Retailcrm_History' ) ) :
|
||||
$wcOrder->set_date_created($order['createdAt']);
|
||||
$customer = $order['customer'];
|
||||
$contactOrCustomer = array();
|
||||
$address = isset($order['customer']['address']) ? $order['customer']['address'] : array();
|
||||
$billingAddress = $address;
|
||||
$companyName = '';
|
||||
|
||||
if ($this->retailcrm->getCorporateEnabled()) {
|
||||
$billingAddress = isset($order['company']['address']) ? $order['company']['address'] : $address;
|
||||
|
||||
if (empty($billingAddress)) {
|
||||
$billingAddress = $address;
|
||||
}
|
||||
}
|
||||
$billingAddress = '';
|
||||
|
||||
if ($this->retailcrm->getCorporateEnabled() && self::isOrderCorporate($order)) {
|
||||
if (isset($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)) {
|
||||
$response = $this->retailcrm->customersGet($contactOrCustomer['id'], 'id');
|
||||
|
||||
@ -640,6 +634,13 @@ if ( ! class_exists( 'WC_Retailcrm_History' ) ) :
|
||||
} else {
|
||||
$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)) {
|
||||
$response = $this->retailcrm->customersGet($contactOrCustomer['id'], 'id');
|
||||
|
||||
@ -667,13 +668,15 @@ if ( ! class_exists( 'WC_Retailcrm_History' ) ) :
|
||||
$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!
|
||||
if ($this->retailcrm->getCorporateEnabled()
|
||||
$companyName = '';
|
||||
|
||||
if (
|
||||
$this->retailcrm->getCorporateEnabled()
|
||||
&& self::isOrderCorporate($order)
|
||||
&& !empty($order['company'])
|
||||
&& isset($order['company']['name'])
|
||||
&& !empty($customer['mainCompany'])
|
||||
&& isset($customer['mainCompany']['name'])
|
||||
) {
|
||||
$companyName = $order['company']['name'];
|
||||
$companyName = $customer['mainCompany']['name'];
|
||||
}
|
||||
|
||||
$addressShipping = array(
|
||||
|
@ -142,12 +142,6 @@ if ( ! class_exists( 'WC_Retailcrm_Orders' ) ) :
|
||||
$firstName = $wcOrder->get_shipping_first_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['lastName'] = $lastName;
|
||||
}
|
||||
@ -166,8 +160,13 @@ if ( ! class_exists( 'WC_Retailcrm_Orders' ) ) :
|
||||
*/
|
||||
protected function fillOrderCreate($wcCustomerId, $wcCustomerEmail, $wcOrder)
|
||||
{
|
||||
$foundCustomerId = '';
|
||||
$foundCustomer = $this->customers->findCustomerEmailOrId($wcCustomerId, $wcCustomerEmail);
|
||||
$isContact = $this->retailcrm->getCorporateEnabled() && static::isCorporateOrder($wcOrder);
|
||||
|
||||
$foundCustomer = $this->customers->findCustomerEmailOrId(
|
||||
$wcCustomerId,
|
||||
strtolower($wcCustomerEmail),
|
||||
$isContact
|
||||
);
|
||||
|
||||
if (empty($foundCustomer)) {
|
||||
$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_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 */
|
||||
foreach ($order->get_items() as $item) {
|
||||
|
@ -1,4 +1,5 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* PHP version 5.3
|
||||
*
|
||||
@ -14,8 +15,6 @@
|
||||
*/
|
||||
class WC_Retailcrm_Customer_Address extends WC_Retailcrm_Abstracts_Address
|
||||
{
|
||||
protected $filter_name = 'customer_address';
|
||||
|
||||
/**
|
||||
* @param WC_Customer $customer
|
||||
* @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)
|
||||
{
|
||||
$customerBillingAddress = $customer->get_billing_address();
|
||||
$address = $this->getCustomerAddress($customer, $order);
|
||||
|
||||
if ($order instanceof WC_Order && empty($customerBillingAddress)) {
|
||||
$data = 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' => $order->get_billing_address_1() . ', ' . $order->get_billing_address_2()
|
||||
if (!empty($address)) {
|
||||
$customerAddress = apply_filters(
|
||||
'retailcrm_process_customer_address',
|
||||
WC_Retailcrm_Plugin::clearArray($address),
|
||||
$customer,
|
||||
$order
|
||||
);
|
||||
|
||||
$this->set_data_fields($customerAddress);
|
||||
} else {
|
||||
$data = 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' => $customer->get_billing_address_1() . ', ' . $customer->get_billing_address_2()
|
||||
);
|
||||
WC_Retailcrm_Logger::add('Error Customer address is empty');
|
||||
}
|
||||
|
||||
$this->set_data_fields($data);
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
|
@ -14,39 +14,12 @@
|
||||
*/
|
||||
class WC_Retailcrm_Customer_Corporate_Address extends WC_Retailcrm_Abstracts_Address
|
||||
{
|
||||
/** @var string $filter_name */
|
||||
protected $filter_name = 'customer_address';
|
||||
|
||||
/** @var bool $isMain */
|
||||
protected $isMain = true;
|
||||
|
||||
/** @var bool $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
|
||||
*
|
||||
@ -77,82 +50,30 @@ class WC_Retailcrm_Customer_Corporate_Address extends WC_Retailcrm_Abstracts_Add
|
||||
*/
|
||||
public function build($customer, $order = null)
|
||||
{
|
||||
if ($order instanceof WC_Order) {
|
||||
$address = $this->getOrderAddress($order);
|
||||
$data = array(
|
||||
'index' => $address['postcode'],
|
||||
'countryIso' => $address['country'],
|
||||
'region' => $address['state'],
|
||||
'city' => $address['city'],
|
||||
'name' => $address['company'],
|
||||
'text' => $this->joinAddresses($address['address_1'], $address['address_2'])
|
||||
$address = $this->getCustomerAddress($customer, $order);
|
||||
|
||||
if (!empty($address)) {
|
||||
if ($this->isMain) {
|
||||
$address['isMain'] = true;
|
||||
} elseif ($this->explicitIsMain) {
|
||||
$address['isMain'] = false;
|
||||
}
|
||||
|
||||
$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) {
|
||||
$data = $this->getCustomerBillingAddress($customer);
|
||||
}
|
||||
} elseif (self::ADDRESS_TYPE_BILLING === $this->address_type) {
|
||||
$data = $this->getCustomerBillingAddress($customer);
|
||||
|
||||
if (empty($address) && $this->fallback_to_shipping) {
|
||||
$data = $this->getCustomerShippingAddress($customer);
|
||||
}
|
||||
$this->set_data_fields($corporateCustomerAddress);
|
||||
} else {
|
||||
WC_Retailcrm_Logger::add('Error Corporate Customer address is empty');
|
||||
}
|
||||
|
||||
if ($this->isMain) {
|
||||
$data['isMain'] = true;
|
||||
} elseif ($this->explicitIsMain) {
|
||||
$data['isMain'] = false;
|
||||
}
|
||||
|
||||
$this->set_data_fields($data);
|
||||
|
||||
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()
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -11,9 +11,6 @@
|
||||
|
||||
class WC_Retailcrm_Order_Address extends WC_Retailcrm_Abstracts_Address
|
||||
{
|
||||
/** @var string $filter_name */
|
||||
protected $filter_name = 'order_address';
|
||||
|
||||
/**
|
||||
* @param WC_Order $order
|
||||
*
|
||||
@ -24,24 +21,17 @@ class WC_Retailcrm_Order_Address extends WC_Retailcrm_Abstracts_Address
|
||||
$address = $this->getOrderAddress($order);
|
||||
|
||||
if (!empty($address)) {
|
||||
$data = array(
|
||||
'index' => $address['postcode'],
|
||||
'city' => $address['city'],
|
||||
'region' => $this->get_state_name($address['country'], $address['state'])
|
||||
$orderAddress = apply_filters(
|
||||
'retailcrm_process_order_address',
|
||||
WC_Retailcrm_Plugin::clearArray($address),
|
||||
$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;
|
||||
}
|
||||
}
|
||||
|
@ -18,11 +18,18 @@ class WC_Retailcrm_Customer_Address_Test extends WC_Retailcrm_Test_Case_Helper
|
||||
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()
|
||||
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();
|
||||
|
||||
$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('text', $data);
|
||||
$this->assertArrayHasKey('countryIso', $data);
|
||||
$this->assertNotEmpty($data['index']);
|
||||
$this->assertNotEmpty($data['city']);
|
||||
$this->assertNotEmpty($data['region']);
|
||||
$this->assertNotEmpty($data['text']);
|
||||
$this->assertNotEmpty($data['countryIso']);
|
||||
$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']);
|
||||
|
||||
// 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']);
|
||||
}
|
||||
}
|
||||
|
||||
|
110
tests/customer/test-wc-retailcrm-customer-corporate-address.php
Normal file
110
tests/customer/test-wc-retailcrm-customer-corporate-address.php
Normal 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']);
|
||||
}
|
||||
}
|
||||
|
@ -22,19 +22,56 @@ class WC_Retailcrm_Order_Address_Test extends WC_Retailcrm_Test_Case_Helper
|
||||
parent::setUp();
|
||||
|
||||
$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();
|
||||
$data = $order_address
|
||||
->setWCAddressType(WC_Retailcrm_Abstracts_Address::ADDRESS_TYPE_BILLING)
|
||||
->build($this->order)
|
||||
->get_data();
|
||||
$data = $order_address->build($this->order)->get_data();
|
||||
|
||||
$this->assertArrayHasKey('index', $data);
|
||||
$this->assertArrayHasKey('city', $data);
|
||||
$this->assertArrayHasKey('region', $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']);
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user