diff --git a/doc/2. Workflow/Address.md b/doc/2. Workflow/Address.md new file mode 100644 index 0000000..9d5e6c4 --- /dev/null +++ b/doc/2. Workflow/Address.md @@ -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 + '', @@ -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))); } /** diff --git a/src/include/abstracts/class-wc-retailcrm-abstracts-data.php b/src/include/abstracts/class-wc-retailcrm-abstracts-data.php index dcd6f79..e968dc4 100644 --- a/src/include/abstracts/class-wc-retailcrm-abstracts-data.php +++ b/src/include/abstracts/class-wc-retailcrm-abstracts-data.php @@ -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; } diff --git a/src/include/class-wc-retailcrm-customers.php b/src/include/class-wc-retailcrm-customers.php index bc12a4c..b4aa0a8 100644 --- a/src/include/class-wc-retailcrm-customers.php +++ b/src/include/class-wc-retailcrm-customers.php @@ -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; diff --git a/src/include/class-wc-retailcrm-history.php b/src/include/class-wc-retailcrm-history.php index 064b77f..176cdbf 100644 --- a/src/include/class-wc-retailcrm-history.php +++ b/src/include/class-wc-retailcrm-history.php @@ -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( diff --git a/src/include/class-wc-retailcrm-orders.php b/src/include/class-wc-retailcrm-orders.php index 11f6586..9abbf5c 100644 --- a/src/include/class-wc-retailcrm-orders.php +++ b/src/include/class-wc-retailcrm-orders.php @@ -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) { diff --git a/src/include/customer/class-wc-retailcrm-customer-address.php b/src/include/customer/class-wc-retailcrm-customer-address.php index 88edf00..1a90105 100644 --- a/src/include/customer/class-wc-retailcrm-customer-address.php +++ b/src/include/customer/class-wc-retailcrm-customer-address.php @@ -1,4 +1,5 @@ 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; } } diff --git a/src/include/customer/class-wc-retailcrm-customer-corporate-address.php b/src/include/customer/class-wc-retailcrm-customer-corporate-address.php index 088c432..a94f042 100644 --- a/src/include/customer/class-wc-retailcrm-customer-corporate-address.php +++ b/src/include/customer/class-wc-retailcrm-customer-corporate-address.php @@ -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() - ) - ); - } } diff --git a/src/include/order/class-wc-retailcrm-order-address.php b/src/include/order/class-wc-retailcrm-order-address.php index 934d468..4b65ec8 100644 --- a/src/include/order/class-wc-retailcrm-order-address.php +++ b/src/include/order/class-wc-retailcrm-order-address.php @@ -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; } } diff --git a/tests/customer/test-wc-retailcrm-customer-address.php b/tests/customer/test-wc-retailcrm-customer-address.php index 880b34a..cd8aa5a 100644 --- a/tests/customer/test-wc-retailcrm-customer-address.php +++ b/tests/customer/test-wc-retailcrm-customer-address.php @@ -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']); } } + diff --git a/tests/customer/test-wc-retailcrm-customer-corporate-address.php b/tests/customer/test-wc-retailcrm-customer-corporate-address.php new file mode 100644 index 0000000..9fe51ab --- /dev/null +++ b/tests/customer/test-wc-retailcrm-customer-corporate-address.php @@ -0,0 +1,110 @@ + + * @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']); + } +} + diff --git a/tests/order/test-wc-retailcrm-order-address.php b/tests/order/test-wc-retailcrm-order-address.php index 945b8d9..64c1017 100644 --- a/tests/order/test-wc-retailcrm-order-address.php +++ b/tests/order/test-wc-retailcrm-order-address.php @@ -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']); } } +