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

Change logic work with address

This commit is contained in:
Dima Uryvskiy 2022-09-22 15:17:34 +03:00 committed by GitHub
parent b77635f2c5
commit 8a47750818
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
20 changed files with 240 additions and 310 deletions

View File

@ -41,7 +41,8 @@ abstract class WC_Retailcrm_Abstract_Builder implements WC_Retailcrm_Builder_Int
*/ */
public function reset() public function reset()
{ {
$this->data = array(); $this->data = [];
return $this; return $this;
} }

View File

@ -12,28 +12,10 @@
*/ */
abstract class WC_Retailcrm_Abstracts_Address extends WC_Retailcrm_Abstracts_Data abstract class WC_Retailcrm_Abstracts_Address extends WC_Retailcrm_Abstracts_Data
{ {
/** @var array $data */
protected $data = array(
'index' => '',
'city' => '',
'region' => '',
'text' => '',
);
/** /**
* Resets inner state * Divider for order delivery address_1 and address_2
*/ */
public function reset_data() const ADDRESS_LINE_DIVIDER = ' || ';
{
$this->data = array(
'index' => '',
'city' => '',
'region' => '',
'text' => '',
);
return $this;
}
/** /**
* Returns shipping address from order. * Returns shipping address from order.
@ -44,26 +26,16 @@ abstract class WC_Retailcrm_Abstracts_Address extends WC_Retailcrm_Abstracts_Dat
*/ */
protected function getOrderAddress($order) protected function getOrderAddress($order)
{ {
if ($order === null) { if (!$order instanceof WC_Order) {
return array(); return [];
} }
$orderShippingAddress = array( return [
'postcode' => $order->get_shipping_postcode(), 'index' => $order->get_shipping_postcode(),
'state' => $order->get_shipping_state(),
'city' => $order->get_shipping_city(), 'city' => $order->get_shipping_city(),
'address_1' => $order->get_shipping_address_1(), 'region' => $this->getRegion($order->get_shipping_country(), $order->get_shipping_state()),
'address_2' => $order->get_shipping_address_2() 'text' => $this->getText($order, 'order'),
); ];
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)
);
}
} }
/** /**
@ -76,45 +48,31 @@ abstract class WC_Retailcrm_Abstracts_Address extends WC_Retailcrm_Abstracts_Dat
*/ */
protected function getCustomerAddress($customer, $order) protected function getCustomerAddress($customer, $order)
{ {
if ($customer === null) { if (!$customer instanceof WC_Customer) {
return array(); return [];
} }
$customerBillingAddress = $customer->get_billing_address(); $customerBillingAddress = $customer->get_billing_address();
if ($order instanceof WC_Order && empty($customerBillingAddress)) { if ($order instanceof WC_Order && empty($customerBillingAddress)) {
return array( return [
'index' => $order->get_billing_postcode(), 'index' => $order->get_billing_postcode(),
'countryIso' => $this->validateCountryCode($order->get_billing_country()), 'countryIso' => $this->getCountryCode($order->get_billing_country()),
'region' => $this->get_state_name($order->get_billing_country(), $order->get_billing_state()), 'region' => $this->getRegion($order->get_billing_country(), $order->get_billing_state()),
'city' => $order->get_billing_city(), 'city' => $order->get_billing_city(),
'text' => $this->joinAddresses($order->get_billing_address_1(), $order->get_billing_address_2()) 'text' => $this->getText($order),
); ];
} else { } else {
return array( return [
'index' => $customer->get_billing_postcode(), 'index' => $customer->get_billing_postcode(),
'countryIso' => $this->validateCountryCode($customer->get_billing_country()), 'countryIso' => $this->getCountryCode($customer->get_billing_country()),
'region' => $this->get_state_name($customer->get_billing_country(), $customer->get_billing_state()), 'region' => $this->getRegion($customer->get_billing_country(), $customer->get_billing_state()),
'city' => $customer->get_billing_city(), 'city' => $customer->get_billing_city(),
'text' => $this->joinAddresses($customer->get_billing_address_1(), $customer->get_billing_address_2()) 'text' => $this->getText($customer),
); ];
} }
} }
/**
* Validate countryIso. Check if a given code represents a valid ISO 3166-1 alpha-2 code.
*
* @param $countryCode
*
* @return string
*/
private function validateCountryCode($countryCode)
{
$countries = new WC_Countries();
return $countries->country_exists($countryCode) ? $countryCode : '';
}
/** /**
* Glue two addresses * Glue two addresses
* *
@ -123,9 +81,23 @@ abstract class WC_Retailcrm_Abstracts_Address extends WC_Retailcrm_Abstracts_Dat
* *
* @return string * @return string
*/ */
protected function joinAddresses($address1 = '', $address2 = '') protected function joinAddresses(string $address1 = '', string $address2 = '')
{ {
return implode(', ', array_filter(array($address1, $address2))); return implode(self::ADDRESS_LINE_DIVIDER, array_filter([$address1, $address2]));
}
/**
* Validate countryIso. Check if a given code represents a valid ISO 3166-1 alpha-2 code.
*
* @param $countryCode
*
* @return string
*/
private function getCountryCode($countryCode)
{
$countries = new WC_Countries();
return $countries->country_exists($countryCode) ? $countryCode : '';
} }
/** /**
@ -136,7 +108,7 @@ abstract class WC_Retailcrm_Abstracts_Address extends WC_Retailcrm_Abstracts_Dat
* *
* @return string * @return string
*/ */
protected function get_state_name($countryCode, $stateCode) protected function getRegion(string $countryCode, string $stateCode)
{ {
if (preg_match('/^[A-Z\-0-9]{0,5}$/', $stateCode) && !is_null($countryCode)) { if (preg_match('/^[A-Z\-0-9]{0,5}$/', $stateCode) && !is_null($countryCode)) {
$countriesProvider = new WC_Countries(); $countriesProvider = new WC_Countries();
@ -149,4 +121,24 @@ abstract class WC_Retailcrm_Abstracts_Address extends WC_Retailcrm_Abstracts_Dat
return $stateCode; return $stateCode;
} }
/**
* Returns data for CRM field 'text'.
* If type entity equals 'order', get address for order and use shipping address,
* else get address for customer and use billing address.
*
* @return string
*/
protected function getText($wcEntity, $typeEntity = 'customer')
{
if ($typeEntity === 'order') {
return empty($wcEntity->get_shipping_address_2())
? $wcEntity->get_shipping_address_1()
: $this->joinAddresses($wcEntity->get_shipping_address_1(), $wcEntity->get_shipping_address_2());
} else {
return empty($wcEntity->get_billing_address_2())
? $wcEntity->get_billing_address_1()
: $this->joinAddresses($wcEntity->get_billing_address_1(), $wcEntity->get_billing_address_2());
}
}
} }

View File

@ -13,12 +13,7 @@
abstract class WC_Retailcrm_Abstracts_Data abstract class WC_Retailcrm_Abstracts_Data
{ {
/** @var array */ /** @var array */
protected $data = array(); protected $data = [];
/**
* @return void
*/
abstract public function reset_data();
/** /**
* @param $data * @param $data
@ -30,7 +25,7 @@ abstract class WC_Retailcrm_Abstracts_Data
/** /**
* @codeCoverageIgnore * @codeCoverageIgnore
*/ */
protected function set_data_field($field, $value) protected function setField($field, $value)
{ {
if (isset($this->data[$field]) && \gettype($value) !== \gettype($this->data[$field])) { if (isset($this->data[$field]) && \gettype($value) !== \gettype($this->data[$field])) {
return false; return false;
@ -44,11 +39,11 @@ abstract class WC_Retailcrm_Abstracts_Data
/** /**
* @param $fields * @param $fields
*/ */
protected function set_data_fields($fields) protected function setDataFields($fields)
{ {
if (!empty($fields)) { if (!empty($fields)) {
foreach ($fields as $field => $value) { foreach ($fields as $field => $value) {
$this->set_data_field($field, $value); $this->setField($field, $value);
} }
} }
} }
@ -56,7 +51,7 @@ abstract class WC_Retailcrm_Abstracts_Data
/** /**
* @return array * @return array
*/ */
public function get_data() public function getData()
{ {
return $this->data; return $this->data;
} }

View File

@ -274,7 +274,7 @@ if (!class_exists('WC_Retailcrm_Customers')) :
$newAddress = $builder $newAddress = $builder
->setIsMain(false) ->setIsMain(false)
->build($customer, $order) ->build($customer, $order)
->get_data(); ->getData();
$addresses = $this->retailcrm->customersCorporateAddresses( $addresses = $this->retailcrm->customersCorporateAddresses(
$corporateId, $corporateId,
@ -335,9 +335,9 @@ if (!class_exists('WC_Retailcrm_Customers')) :
); );
if ($response->isSuccessful() && $response->offsetExists('id')) { if ($response->isSuccessful() && $response->offsetExists('id')) {
$this->customerCorporateCompany['address'] = array( $this->customerCorporateCompany['address'] = [
'id' => $response['id'], 'id' => $response['id'],
); ];
$this->retailcrm->customersCorporateCompaniesCreate( $this->retailcrm->customersCorporateCompaniesCreate(
$customerId, $customerId,
@ -401,22 +401,22 @@ if (!class_exists('WC_Retailcrm_Customers')) :
// then we take $order->get_date_created() order // then we take $order->get_date_created() order
$createdAt = empty($createdAt) ? $order->get_date_created() : $createdAt; $createdAt = empty($createdAt) ? $order->get_date_created() : $createdAt;
$data_customer = array( $customerData = [
'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' => $email, 'email' => $email,
'address' => $this->customer_address->build($customer, $order)->get_data() 'address' => $this->customer_address->build($customer, $order)->getData()
); ];
if ($customer->get_id() > 0) { if ($customer->get_id() > 0) {
$data_customer['externalId'] = $customer->get_id(); $customerData['externalId'] = $customer->get_id();
} }
if (!empty($billingPhone)) { if (!empty($billingPhone)) {
$data_customer['phones'][] = array( $customerData['phones'][] = [
'number' => $customer->get_billing_phone() 'number' => $customer->get_billing_phone()
); ];
} }
// If the client is corporate, set the value isContact. // If the client is corporate, set the value isContact.
@ -430,7 +430,7 @@ if (!class_exists('WC_Retailcrm_Customers')) :
} }
if (!empty($company)) { if (!empty($company)) {
$data_customer['isContact'] = true; $customerData['isContact'] = true;
} }
} }
@ -439,14 +439,14 @@ if (!class_exists('WC_Retailcrm_Customers')) :
$metaValue = $customer->get_meta($metaKey); $metaValue = $customer->get_meta($metaKey);
if (!empty($metaValue)) { if (!empty($metaValue)) {
$data_customer['customFields'][$customKey] = $metaValue; $customerData['customFields'][$customKey] = $metaValue;
} }
} }
} }
$this->customer = apply_filters( $this->customer = apply_filters(
'retailcrm_process_customer', 'retailcrm_process_customer',
WC_Retailcrm_Plugin::clearArray($data_customer), WC_Retailcrm_Plugin::clearArray($customerData),
$customer $customer
); );
} }
@ -462,39 +462,39 @@ if (!class_exists('WC_Retailcrm_Customers')) :
*/ */
protected function processCorporateCustomer($crmCustomerId, $customer, $order) protected function processCorporateCustomer($crmCustomerId, $customer, $order)
{ {
$data_company = array( $data_company = [
'isMain' => true, 'isMain' => true,
'name' => $order->get_billing_company() 'name' => $order->get_billing_company()
); ];
$data_customer = array( $data_customer = [
'nickName' => $order->get_billing_company(), 'nickName' => $order->get_billing_company(),
'customerContacts' => array( 'customerContacts' => [
array( [
'isMain' => true, 'isMain' => true,
'customer' => array( 'customer' => [
'id' => $crmCustomerId 'id' => $crmCustomerId
) ]
) ]
) ]
); ];
$corpAddress = new WC_Retailcrm_Customer_Corporate_Address(); $corpAddress = new WC_Retailcrm_Customer_Corporate_Address();
$billingAddress = $corpAddress $billingAddress = $corpAddress
->setIsMain(true) ->setIsMain(true)
->build($customer, $order) ->build($customer, $order)
->get_data(); ->getData();
if (!empty($billingAddress)) { if (!empty($billingAddress)) {
$data_company['contragent']['legalAddress'] = implode( $data_company['contragent']['legalAddress'] = implode(
', ', ', ',
array( [
$billingAddress['index'], $billingAddress['index'],
$billingAddress['city'], $billingAddress['city'],
$billingAddress['region'], $billingAddress['region'],
$billingAddress['text'] $billingAddress['text']
) ]
); );
} }
@ -577,11 +577,11 @@ if (!class_exists('WC_Retailcrm_Customers')) :
$customer = false; $customer = false;
if (!empty($customerExternalId)) { if (!empty($customerExternalId)) {
$customer = $this->searchCustomer(array('externalId' => $customerExternalId)); $customer = $this->searchCustomer(['externalId' => $customerExternalId]);
} }
if (!$customer && !empty($customerEmailOrPhone)) { if (!$customer && !empty($customerEmailOrPhone)) {
$customer = $this->searchCustomer(array('email' => $customerEmailOrPhone, 'isContact' => $isContact)); $customer = $this->searchCustomer(['email' => $customerEmailOrPhone, 'isContact' => $isContact]);
} }
return $customer; return $customer;

View File

@ -438,11 +438,11 @@ if (!class_exists('WC_Retailcrm_History')) :
} }
if (isset($billingAddress['street'])) { if (isset($billingAddress['street'])) {
$wcOrder->set_shipping_address_1($billingAddress['street']); $wcOrder->set_billing_address_1($billingAddress['street']);
} }
if (isset($billingAddress['building'])) { if (isset($billingAddress['building'])) {
$wcOrder->set_shipping_address_2($billingAddress['building']); $wcOrder->set_billing_address_2($billingAddress['building']);
} }
// @codeCoverageIgnoreEnd // @codeCoverageIgnoreEnd
} }
@ -744,32 +744,40 @@ if (!class_exists('WC_Retailcrm_History')) :
$companyName = $customer['mainCompany']['name']; $companyName = $customer['mainCompany']['name'];
} }
$shippingAddress = $order['delivery']['address'];
$shippingAddressLines = $this->getAddressLines($shippingAddress['text']);
$addressShipping = [ $addressShipping = [
'first_name' => $order['firstName'] ?? '', 'first_name' => $order['firstName'] ?? '',
'last_name' => $order['lastName'] ?? '', 'last_name' => $order['lastName'] ?? '',
'company' => '', 'company' => '',
'address_1' => $order['delivery']['address']['text'] ?? '', 'address_1' => $shippingAddressLines['address_1'] ?? $shippingAddressLines,
'address_2' => '', 'address_2' => $shippingAddressLines['address_2'] ?? '',
'city' => $order['delivery']['address']['city'] ?? '', 'city' => $shippingAddress['city'] ?? '',
'state' => $order['delivery']['address']['region'] ?? '', 'state' => $shippingAddress['region'] ?? '',
'postcode' => $order['delivery']['address']['index'] ?? '', 'postcode' => $shippingAddress['index'] ?? '',
'country' => $order['delivery']['address']['countryIso'] ?? '' 'country' => $shippingAddress['countryIso'] ?? ''
]; ];
$billingAddressLines = $this->getAddressLines($billingAddress['text']);
$addressBilling = [ $addressBilling = [
'first_name' => $contactOrCustomer['firstName'] ?? '', 'first_name' => $contactOrCustomer['firstName'] ?? '',
'last_name' => $contactOrCustomer['lastName'] ?? '', 'last_name' => $contactOrCustomer['lastName'] ?? '',
'company' => $companyName, 'company' => $companyName,
'email' => $contactOrCustomer['email'] ?? '', 'email' => $contactOrCustomer['email'] ?? '',
'phone' => $contactOrCustomer['phones'][0]['number'] ?? '', 'phone' => $contactOrCustomer['phones'][0]['number'] ?? '',
'address_1' => $billingAddress['text'] ?? '', 'address_1' => $billingAddressLines['address_1'] ?? $billingAddressLines,
'address_2' => '', 'address_2' => $billingAddressLines['address_2'] ?? '',
'city' => $billingAddress['city'] ?? '', 'city' => $billingAddress['city'] ?? '',
'state' => $billingAddress['region'] ?? '', 'state' => $billingAddress['region'] ?? '',
'postcode' => $billingAddress['index'] ?? '', 'postcode' => $billingAddress['index'] ?? '',
'country' => $billingAddress['countryIso'] ?? '' 'country' => $billingAddress['countryIso'] ?? ''
]; ];
$wcOrder->set_address($addressShipping, 'shipping');
$wcOrder->set_address($addressBilling, 'billing');
if (isset($order['payments']) && $order['payments']) { if (isset($order['payments']) && $order['payments']) {
$payment = WC_Payment_Gateways::instance(); $payment = WC_Payment_Gateways::instance();
@ -784,9 +792,6 @@ if (!class_exists('WC_Retailcrm_History')) :
} }
} }
$wcOrder->set_address($addressBilling, 'billing');
$wcOrder->set_address($addressShipping, 'shipping');
$crmOrderItems = $order['items'] ?? []; $crmOrderItems = $order['items'] ?? [];
if ($crmOrderItems) { if ($crmOrderItems) {
@ -989,6 +994,24 @@ if (!class_exists('WC_Retailcrm_History')) :
} }
} }
/**
* Returns data for address_1 and address_2(if exist data for this field) for WC order.
*
* @param string $addressLine
*
* @return mixed
*/
private function getAddressLines(string $addressLine)
{
if (strpos($addressLine, WC_Retailcrm_Abstracts_Address::ADDRESS_LINE_DIVIDER) !== false) {
$addressLines = explode(WC_Retailcrm_Abstracts_Address::ADDRESS_LINE_DIVIDER, $addressLine);
return ['address_1' => $addressLines[0], 'address_2' => $addressLines[1]];
}
return $addressLine;
}
/** /**
* Add product in WC order. * Add product in WC order.
* *

View File

@ -36,7 +36,7 @@ if (!class_exists('WC_Retailcrm_Orders')) :
protected $orders; protected $orders;
/** @var array */ /** @var array */
private $ordersGetRequestCache = array(); private $ordersGetRequestCache = [];
/** @var array */ /** @var array */
private $order = []; private $order = [];
@ -83,7 +83,7 @@ if (!class_exists('WC_Retailcrm_Orders')) :
return null; return null;
} }
$this->order_payment->reset_data(); $this->order_payment->resetData();
$wcOrder = wc_get_order($order_id); $wcOrder = wc_get_order($order_id);
$this->processOrder($wcOrder); $this->processOrder($wcOrder);
@ -345,7 +345,7 @@ if (!class_exists('WC_Retailcrm_Orders')) :
$this->orders->is_new = false; $this->orders->is_new = false;
} }
$orderData = $this->orders->build($order)->get_data(); $orderData = $this->orders->build($order)->getData();
if ($order->get_items('shipping')) { if ($order->get_items('shipping')) {
$shippings = $order->get_items('shipping'); $shippings = $order->get_items('shipping');
@ -393,14 +393,14 @@ if (!class_exists('WC_Retailcrm_Orders')) :
} }
} }
$orderData['delivery']['address'] = $this->order_address->build($order)->get_data(); $orderData['delivery']['address'] = $this->order_address->build($order)->getData();
$orderItems = []; $orderItems = [];
/** @var WC_Order_Item_Product $item */ /** @var WC_Order_Item_Product $item */
foreach ($order->get_items() as $item) { foreach ($order->get_items() as $item) {
$orderItems[] = $this->order_item->build($item)->get_data(); $orderItems[] = $this->order_item->build($item)->getData();
$this->order_item->reset_data(); $this->order_item->resetData();
} }
$orderData['items'] = $orderItems; $orderData['items'] = $orderItems;
@ -409,7 +409,7 @@ if (!class_exists('WC_Retailcrm_Orders')) :
if (!$update && $order->get_total() > 0) { if (!$update && $order->get_total() > 0) {
$this->order_payment->isNew = true; $this->order_payment->isNew = true;
$orderData['payments'][] = $this->order_payment->build($order)->get_data(); $orderData['payments'][] = $this->order_payment->build($order)->getData();
} }
if (!empty($this->customFields)) { if (!empty($this->customFields)) {
@ -444,8 +444,8 @@ if (!class_exists('WC_Retailcrm_Orders')) :
protected function sendPayment($order, $update = false, $externalId = false) protected function sendPayment($order, $update = false, $externalId = false)
{ {
$this->order_payment->isNew = !$update; $this->order_payment->isNew = !$update;
$payment = $this->order_payment->build($order, $externalId)->get_data();
$payment = $this->order_payment->build($order, $externalId)->getData();
$integrationPayments = get_option('retailcrm_integration_payments'); $integrationPayments = get_option('retailcrm_integration_payments');
if (is_array($integrationPayments)) { if (is_array($integrationPayments)) {

View File

@ -30,7 +30,7 @@ class WC_Retailcrm_Customer_Address extends WC_Retailcrm_Abstracts_Address
$order $order
); );
$this->set_data_fields($customerAddress); $this->setDataFields($customerAddress);
} else { } else {
WC_Retailcrm_Logger::add('Error Customer address is empty'); WC_Retailcrm_Logger::add('Error Customer address is empty');
} }

View File

@ -43,12 +43,12 @@ class WC_Retailcrm_Customer_Corporate_Address extends WC_Retailcrm_Abstracts_Add
'retailcrm_process_customer_corporate_address', 'retailcrm_process_customer_corporate_address',
WC_Retailcrm_Plugin::clearArray(array_merge( WC_Retailcrm_Plugin::clearArray(array_merge(
$address, $address,
array('isMain' => $this->isMain) ['isMain' => $this->isMain]
)), )),
$customer $customer
); );
$this->set_data_fields($corporateCustomerAddress); $this->setDataFields($corporateCustomerAddress);
} else { } else {
WC_Retailcrm_Logger::add('Error Corporate Customer address is empty'); WC_Retailcrm_Logger::add('Error Corporate Customer address is empty');
} }

View File

@ -28,7 +28,7 @@ class WC_Retailcrm_Order_Address extends WC_Retailcrm_Abstracts_Address
$order $order
); );
$this->set_data_fields($orderAddress); $this->setDataFields($orderAddress);
} else { } else {
WC_Retailcrm_Logger::add('Error: Order address is empty'); WC_Retailcrm_Logger::add('Error: Order address is empty');
} }

View File

@ -63,9 +63,9 @@ class WC_Retailcrm_Order_Item extends WC_Retailcrm_Abstracts_Data
] ]
]; ];
$this->set_data_fields($data); $this->setDataFields($data);
$this->set_offer($item); $this->setOffer($item);
$this->set_data_field('discountManualAmount', $discountPrice); $this->setField('discountManualAmount', $discountPrice);
return $this; return $this;
} }
@ -75,7 +75,7 @@ class WC_Retailcrm_Order_Item extends WC_Retailcrm_Abstracts_Data
* *
* @return void * @return void
*/ */
private function set_offer(WC_Order_Item_Product $item) private function setOffer(WC_Order_Item_Product $item)
{ {
$uid = $item['variation_id'] > 0 ? $item['variation_id'] : $item['product_id'] ; $uid = $item['variation_id'] > 0 ? $item['variation_id'] : $item['product_id'] ;
$offer = ['externalId' => $uid]; $offer = ['externalId' => $uid];
@ -92,7 +92,7 @@ class WC_Retailcrm_Order_Item extends WC_Retailcrm_Abstracts_Data
unset($offer['externalId']); unset($offer['externalId']);
} }
$this->set_data_field('offer', $offer); $this->setField('offer', $offer);
} }
/** /**
@ -125,9 +125,9 @@ class WC_Retailcrm_Order_Item extends WC_Retailcrm_Abstracts_Data
} }
/** /**
* Reset data for object * Reset item data.
*/ */
public function reset_data() public function resetData()
{ {
$this->data = [ $this->data = [
'offer' => [], 'offer' => [],

View File

@ -46,7 +46,8 @@ class WC_Retailcrm_Order_Payment extends WC_Retailcrm_Abstracts_Data
*/ */
public function build($order, $externalId = false) public function build($order, $externalId = false)
{ {
$this->reset_data(); $this->resetData();
$paymentData = []; $paymentData = [];
if (!$this->isNew) { if (!$this->isNew) {
@ -100,7 +101,7 @@ class WC_Retailcrm_Order_Payment extends WC_Retailcrm_Abstracts_Data
$order $order
); );
$this->set_data_fields($paymentData); $this->setDataFields($paymentData);
return $this; return $this;
} }
@ -110,9 +111,9 @@ class WC_Retailcrm_Order_Payment extends WC_Retailcrm_Abstracts_Data
* *
* @return array * @return array
*/ */
public function get_data() public function getData()
{ {
$data = parent::get_data(); $data = parent::getData();
if (empty($data['type'])) { if (empty($data['type'])) {
return []; return [];
@ -122,7 +123,7 @@ class WC_Retailcrm_Order_Payment extends WC_Retailcrm_Abstracts_Data
return array_filter($data); return array_filter($data);
} }
public function reset_data() public function resetData()
{ {
$this->data = [ $this->data = [
'externalId' => '', 'externalId' => '',

View File

@ -78,44 +78,25 @@ class WC_Retailcrm_Order extends WC_Retailcrm_Abstracts_Data
$data['countryIso'] = $countries->get_base_country(); $data['countryIso'] = $countries->get_base_country();
} }
$this->set_data_fields($data); $this->setDataFields($data);
$this->set_number($order); $this->setNumber($order);
if (isset($this->settings[$order->get_status()]) && 'not-upload' !== $this->settings[$order->get_status()]) { if (isset($this->settings[$order->get_status()]) && 'not-upload' !== $this->settings[$order->get_status()]) {
$this->set_data_field('status', $this->settings[$order->get_status()]); $this->setField('status', $this->settings[$order->get_status()]);
} }
return $this; return $this;
} }
/** /**
* @param WC_Order $order * @param WC_Order $order
*/ */
protected function set_number($order) protected function setNumber($order)
{ {
if (isset($this->settings['update_number']) && $this->settings['update_number'] == WC_Retailcrm_Base::YES) { if (isset($this->settings['update_number']) && $this->settings['update_number'] == WC_Retailcrm_Base::YES) {
$this->set_data_field('number', $order->get_order_number()); $this->setField('number', $order->get_order_number());
} else { } else {
unset($this->data['number']); unset($this->data['number']);
} }
} }
public function reset_data()
{
$this->data = [
'externalId' => '',
'status' => '',
'number' => '',
'createdAt' => '',
'firstName' => '',
'lastName' => '',
'email' => '',
'paymentType' => '',
'customerComment' => '',
'paymentStatus' => '',
'phone' => '',
'countryIso' => ''
];
}
} }

View File

@ -28,10 +28,10 @@ class WC_Retailcrm_Customer_Address_Test extends WC_Retailcrm_Test_Case_Helper
$this->customer->set_billing_address_2('TestAddress2'); $this->customer->set_billing_address_2('TestAddress2');
} }
public function test_build_and_reset_address() public function test_build_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)->getData();
$this->assertArrayHasKey('index', $data); $this->assertArrayHasKey('index', $data);
$this->assertArrayHasKey('city', $data); $this->assertArrayHasKey('city', $data);
@ -41,37 +41,20 @@ class WC_Retailcrm_Customer_Address_Test extends WC_Retailcrm_Test_Case_Helper
$this->assertEquals('000000', $data['index']); $this->assertEquals('000000', $data['index']);
$this->assertEquals('TestCity', $data['city']); $this->assertEquals('TestCity', $data['city']);
$this->assertEquals('TestState', $data['region']); $this->assertEquals('TestState', $data['region']);
$this->assertEquals('TestAddress1, TestAddress2', $data['text']); $this->assertEquals('TestAddress1 || TestAddress2', $data['text']);
$this->assertEquals('CO', $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() public function test_empty_address()
{ {
$customer_address = new WC_Retailcrm_Customer_Address(); $customerAddress = new WC_Retailcrm_Customer_Address();
$data = $customer_address->build(null)->get_data();
$this->assertArrayHasKey('index', $data); $addressData = $customerAddress
$this->assertArrayHasKey('city', $data); ->build(null)
$this->assertArrayHasKey('region', $data); ->getData();
$this->assertArrayHasKey('text', $data);
$this->assertEquals('', $data['index']); $this->assertInternalType('array', $addressData);
$this->assertEquals('', $data['city']); $this->assertEquals([], $addressData);
$this->assertEquals('', $data['region']);
$this->assertEquals('', $data['text']);
} }
} }

View File

@ -28,13 +28,13 @@ class WC_Retailcrm_Customer_Corporate_Address_Test extends WC_Retailcrm_Test_Cas
$this->customer->set_billing_address_2('TestAddress2'); $this->customer->set_billing_address_2('TestAddress2');
} }
public function test_build_and_reset_address() public function test_build_address()
{ {
$customer_address = new WC_Retailcrm_Customer_Corporate_Address(); $customer_address = new WC_Retailcrm_Customer_Corporate_Address();
$data = $customer_address $data = $customer_address
->setIsMain(true) ->setIsMain(true)
->build($this->customer) ->build($this->customer)
->get_data(); ->getData();
$this->assertArrayHasKey('index', $data); $this->assertArrayHasKey('index', $data);
$this->assertArrayHasKey('city', $data); $this->assertArrayHasKey('city', $data);
@ -45,23 +45,9 @@ class WC_Retailcrm_Customer_Corporate_Address_Test extends WC_Retailcrm_Test_Cas
$this->assertEquals('000000', $data['index']); $this->assertEquals('000000', $data['index']);
$this->assertEquals('TestCity', $data['city']); $this->assertEquals('TestCity', $data['city']);
$this->assertEquals('TestState', $data['region']); $this->assertEquals('TestState', $data['region']);
$this->assertEquals('TestAddress1, TestAddress2', $data['text']); $this->assertEquals('TestAddress1 || TestAddress2', $data['text']);
$this->assertEquals('CO', $data['countryIso']); $this->assertEquals('CO', $data['countryIso']);
$this->assertEquals(true, $data['isMain']); $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() public function test_build_not_main_company()
@ -70,7 +56,7 @@ class WC_Retailcrm_Customer_Corporate_Address_Test extends WC_Retailcrm_Test_Cas
$data = $customer_address $data = $customer_address
->setIsMain(false) ->setIsMain(false)
->build($this->customer) ->build($this->customer)
->get_data(); ->getData();
$this->assertArrayHasKey('index', $data); $this->assertArrayHasKey('index', $data);
$this->assertArrayHasKey('city', $data); $this->assertArrayHasKey('city', $data);
@ -81,7 +67,7 @@ class WC_Retailcrm_Customer_Corporate_Address_Test extends WC_Retailcrm_Test_Cas
$this->assertEquals('000000', $data['index']); $this->assertEquals('000000', $data['index']);
$this->assertEquals('TestCity', $data['city']); $this->assertEquals('TestCity', $data['city']);
$this->assertEquals('TestState', $data['region']); $this->assertEquals('TestState', $data['region']);
$this->assertEquals('TestAddress1, TestAddress2', $data['text']); $this->assertEquals('TestAddress1 || TestAddress2', $data['text']);
$this->assertEquals('CO', $data['countryIso']); $this->assertEquals('CO', $data['countryIso']);
$this->assertEquals(false, $data['isMain']); $this->assertEquals(false, $data['isMain']);
} }
@ -89,20 +75,15 @@ class WC_Retailcrm_Customer_Corporate_Address_Test extends WC_Retailcrm_Test_Cas
public function test_empty_address() public function test_empty_address()
{ {
$customer_address = new WC_Retailcrm_Customer_Corporate_Address(); $customerCorporateAddress = new WC_Retailcrm_Customer_Corporate_Address();
$data = $customer_address
$addressData = $customerCorporateAddress
->setIsMain(false) ->setIsMain(false)
->build(null) ->build(null)
->get_data(); ->getData();
$this->assertArrayHasKey('index', $data); $this->assertInternalType('array', $addressData);
$this->assertArrayHasKey('city', $data); $this->assertEquals([], $addressData);
$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

@ -169,7 +169,7 @@ class DataHistoryRetailCrm
'countryIso' => 'ES', 'countryIso' => 'ES',
'region' => 'Region', 'region' => 'Region',
'city' => 'City', 'city' => 'City',
'text' => 'Street' 'text' => 'Street1 || Street2'
] ]
], ],
'site' => 'test-com', 'site' => 'test-com',

View File

@ -28,10 +28,10 @@ class WC_Retailcrm_Order_Address_Test extends WC_Retailcrm_Test_Case_Helper
$this->order->set_shipping_address_2('TestAddress2'); $this->order->set_shipping_address_2('TestAddress2');
} }
public function test_build_and_reset_address() public function test_build_address()
{ {
$order_address = new WC_Retailcrm_Order_Address(); $order_address = new WC_Retailcrm_Order_Address();
$data = $order_address->build($this->order)->get_data(); $data = $order_address->build($this->order)->getData();
$this->assertArrayHasKey('index', $data); $this->assertArrayHasKey('index', $data);
$this->assertArrayHasKey('city', $data); $this->assertArrayHasKey('city', $data);
@ -40,36 +40,19 @@ class WC_Retailcrm_Order_Address_Test extends WC_Retailcrm_Test_Case_Helper
$this->assertEquals('000000', $data['index']); $this->assertEquals('000000', $data['index']);
$this->assertEquals('TestCity', $data['city']); $this->assertEquals('TestCity', $data['city']);
$this->assertEquals('TestState', $data['region']); $this->assertEquals('TestState', $data['region']);
$this->assertEquals('000000 TestState TestCity TestAddress1 TestAddress2', $data['text']); $this->assertEquals('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() public function test_empty_address()
{ {
$order_address = new WC_Retailcrm_Order_Address(); $orderAddress = new WC_Retailcrm_Order_Address();
$data = $order_address->build(null)->get_data();
$this->assertArrayHasKey('index', $data); $addressData = $orderAddress
$this->assertArrayHasKey('city', $data); ->build(null)
$this->assertArrayHasKey('region', $data); ->getData();
$this->assertArrayHasKey('text', $data);
$this->assertEquals('', $data['index']); $this->assertInternalType('array', $addressData);
$this->assertEquals('', $data['city']); $this->assertEquals([], $addressData);
$this->assertEquals('', $data['region']);
$this->assertEquals('', $data['text']);
} }
} }

View File

@ -28,7 +28,7 @@ class WC_Retailcrm_Order_Item_Test extends WC_Retailcrm_Test_Case_Helper
/** @var WC_Order_Item_Product $item */ /** @var WC_Order_Item_Product $item */
foreach ($this->order->get_items() as $item) { foreach ($this->order->get_items() as $item) {
$data = $order_item->build($item)->get_data(); $data = $order_item->build($item)->getData();
$this->assertArrayHasKey('productName', $data); $this->assertArrayHasKey('productName', $data);
$this->assertArrayHasKey('initialPrice', $data); $this->assertArrayHasKey('initialPrice', $data);
@ -42,7 +42,7 @@ class WC_Retailcrm_Order_Item_Test extends WC_Retailcrm_Test_Case_Helper
$order_item = new WC_Retailcrm_Order_Item(['bind_by_sku' => 'yes']); $order_item = new WC_Retailcrm_Order_Item(['bind_by_sku' => 'yes']);
foreach ($this->order->get_items() as $item) { foreach ($this->order->get_items() as $item) {
$data = $order_item->build($item)->get_data(); $data = $order_item->build($item)->getData();
$this->assertArrayHasKey('offer', $data); $this->assertArrayHasKey('offer', $data);
$this->assertArrayHasKey('xmlId', $data['offer']); $this->assertArrayHasKey('xmlId', $data['offer']);

View File

@ -33,7 +33,7 @@ class WC_Retailcrm_Order_Payment_Test extends WC_Retailcrm_Test_Case_Helper
$settings = $this->getOptions(); $settings = $this->getOptions();
$order_payment = new WC_Retailcrm_Order_Payment($settings); $order_payment = new WC_Retailcrm_Order_Payment($settings);
$data = $order_payment->build($this->order, $externalId)->get_data(); $data = $order_payment->build($this->order, $externalId)->getData();
$this->assertNotEmpty($data); $this->assertNotEmpty($data);
@ -53,7 +53,7 @@ class WC_Retailcrm_Order_Payment_Test extends WC_Retailcrm_Test_Case_Helper
public function test_build_payment_type_not_exist($externalId) public function test_build_payment_type_not_exist($externalId)
{ {
$order_payment = new WC_Retailcrm_Order_Payment('test'); $order_payment = new WC_Retailcrm_Order_Payment('test');
$data = $order_payment->build($this->order, $externalId)->get_data(); $data = $order_payment->build($this->order, $externalId)->getData();
$this->assertEmpty($data); $this->assertEmpty($data);
} }
@ -70,7 +70,7 @@ class WC_Retailcrm_Order_Payment_Test extends WC_Retailcrm_Test_Case_Helper
$order_payment = new WC_Retailcrm_Order_Payment($settings); $order_payment = new WC_Retailcrm_Order_Payment($settings);
$order_payment->isNew = false; $order_payment->isNew = false;
$data = $order_payment->build($this->order, $externalId)->get_data(); $data = $order_payment->build($this->order, $externalId)->getData();
$this->assertEmpty($data); $this->assertEmpty($data);
} }
@ -88,7 +88,7 @@ class WC_Retailcrm_Order_Payment_Test extends WC_Retailcrm_Test_Case_Helper
$this->order->update_status('completed'); $this->order->update_status('completed');
$data = $order_payment->build($this->order, $externalId)->get_data(); $data = $order_payment->build($this->order, $externalId)->getData();
$this->assertNotEmpty($data); $this->assertNotEmpty($data);
@ -111,7 +111,7 @@ class WC_Retailcrm_Order_Payment_Test extends WC_Retailcrm_Test_Case_Helper
$settings = $this->getOptions(); $settings = $this->getOptions();
$order_payment = new WC_Retailcrm_Order_Payment($settings); $order_payment = new WC_Retailcrm_Order_Payment($settings);
$data = $order_payment->build($this->order, $externalId)->get_data(); $data = $order_payment->build($this->order, $externalId)->getData();
$this->assertNotEmpty($data); $this->assertNotEmpty($data);

View File

@ -21,22 +21,10 @@ class WC_Retailcrm_Order_Test extends WC_Retailcrm_Test_Case_Helper {
$this->order = WC_Helper_Order::create_order(); $this->order = WC_Helper_Order::create_order();
} }
public function test_reset_data()
{
$buildOrder = new WC_Retailcrm_Order($this->getOptions());
$data = $buildOrder->build($this->order)->get_data();
$this->assertNotEmpty($data);
$buildOrder->reset_data();
$this->assertEmpty(array_filter($buildOrder->get_data()));
}
public function test_empty_shipping_data() public function test_empty_shipping_data()
{ {
$buildOrder = new WC_Retailcrm_Order($this->getOptions()); $buildOrder = new WC_Retailcrm_Order($this->getOptions());
$data = $buildOrder->build($this->order)->get_data(); $data = $buildOrder->build($this->order)->getData();
$this->assertNotEmpty($data); $this->assertNotEmpty($data);
$this->assertArrayHasKey('firstName', $data); $this->assertArrayHasKey('firstName', $data);
@ -51,7 +39,7 @@ class WC_Retailcrm_Order_Test extends WC_Retailcrm_Test_Case_Helper {
$this->order->set_shipping_country(''); $this->order->set_shipping_country('');
$data = $buildOrder->build($this->order)->get_data(); $data = $buildOrder->build($this->order)->getData();
$this->assertNotEmpty($data); $this->assertNotEmpty($data);
$this->assertArrayHasKey('countryIso', $data); $this->assertArrayHasKey('countryIso', $data);

View File

@ -46,57 +46,59 @@ class WC_Retailcrm_History_Test extends WC_Retailcrm_Test_Case_Helper
if (!$wcOrder) { if (!$wcOrder) {
$this->fail('$order_added is null - no orders were added after receiving history'); $this->fail('$order_added is null - no orders were added after receiving history');
} }
$order_added_items = $wcOrder->get_items();
$order_added_item = reset($order_added_items);
$shipping_address = $wcOrder->get_address('shipping');
$billing_address = $wcOrder->get_address('billing');
$options = get_option(\WC_Retailcrm_Base::$option_key); $options = get_option(\WC_Retailcrm_Base::$option_key);
$orderItems = $wcOrder->get_items();
$orderItem = reset($orderItems);
$shippingAddress = $wcOrder->get_address('shipping');
$billingAddress = $wcOrder->get_address('billing');
$this->assertEquals('status1', $options[$wcOrder->get_status()]); $this->assertEquals('status1', $options[$wcOrder->get_status()]);
if (is_object($order_added_item)) { if (is_object($orderItem)) {
$this->assertEquals($product->get_id(), $order_added_item->get_product()->get_id()); $this->assertEquals($product->get_id(), $orderItem->get_product()->get_id());
} }
$this->assertNotEmpty($wcOrder->get_date_created()); $this->assertNotEmpty($wcOrder->get_date_created());
$this->assertNotEmpty($shipping_address['first_name']); $this->assertNotEmpty($shippingAddress['first_name']);
$this->assertNotEmpty($shipping_address['last_name']); $this->assertNotEmpty($shippingAddress['last_name']);
$this->assertNotEmpty($shipping_address['postcode']); $this->assertNotEmpty($shippingAddress['postcode']);
$this->assertNotEmpty($shipping_address['city']); $this->assertNotEmpty($shippingAddress['city']);
$this->assertNotEmpty($shipping_address['country']); $this->assertNotEmpty($shippingAddress['country']);
$this->assertNotEmpty($shipping_address['state']); $this->assertNotEmpty($shippingAddress['state']);
$this->assertEquals('Test_Name', $shipping_address['first_name']); $this->assertEquals('Test_Name', $shippingAddress['first_name']);
$this->assertEquals('Test_LastName', $shipping_address['last_name']); $this->assertEquals('Test_LastName', $shippingAddress['last_name']);
$this->assertEquals('City', $shipping_address['city']); $this->assertEquals('City', $shippingAddress['city']);
$this->assertEquals('Region', $shipping_address['state']); $this->assertEquals('Region', $shippingAddress['state']);
$this->assertEquals('ES', $shipping_address['country']); $this->assertEquals('ES', $shippingAddress['country']);
$this->assertEquals(123456, $shipping_address['postcode']); $this->assertEquals(123456, $shippingAddress['postcode']);
$this->assertEquals('Street', $shipping_address['address_1']); $this->assertEquals('Street1', $shippingAddress['address_1']);
$this->assertEquals('Street2', $shippingAddress['address_2']);
if (isset($billing_address['phone'])) { if (isset($billingAddress['phone'])) {
$this->assertNotEmpty($billing_address['phone']); $this->assertNotEmpty($billingAddress['phone']);
} }
if (isset($billing_address['email'])) { if (isset($billingAddress['email'])) {
$this->assertNotEmpty($billing_address['email']); $this->assertNotEmpty($billingAddress['email']);
} }
$this->assertNotEmpty($billing_address['first_name']); $this->assertNotEmpty($billingAddress['first_name']);
$this->assertNotEmpty($billing_address['last_name']); $this->assertNotEmpty($billingAddress['last_name']);
$this->assertNotEmpty($billing_address['postcode']); $this->assertNotEmpty($billingAddress['postcode']);
$this->assertNotEmpty($billing_address['city']); $this->assertNotEmpty($billingAddress['city']);
$this->assertNotEmpty($billing_address['country']); $this->assertNotEmpty($billingAddress['country']);
$this->assertNotEmpty($billing_address['state']); $this->assertNotEmpty($billingAddress['state']);
$this->assertEquals('Test_Name', $billing_address['first_name']); $this->assertEquals('Test_Name', $billingAddress['first_name']);
$this->assertEquals('Test_LastName', $billing_address['last_name']); $this->assertEquals('Test_LastName', $billingAddress['last_name']);
$this->assertEquals('City', $billing_address['city']); $this->assertEquals('City', $billingAddress['city']);
$this->assertEquals('Region', $billing_address['state']); $this->assertEquals('Region', $billingAddress['state']);
$this->assertEquals('ES', $billing_address['country']); $this->assertEquals('ES', $billingAddress['country']);
$this->assertEquals(123456, $billing_address['postcode']); $this->assertEquals(123456, $billingAddress['postcode']);
$this->assertEquals('Street', $billing_address['address_1']); $this->assertEquals('Street', $billingAddress['address_1']);
if ($wcOrder->get_payment_method()) { if ($wcOrder->get_payment_method()) {
$this->assertEquals('payment4', $options[$wcOrder->get_payment_method()]); $this->assertEquals('payment4', $options[$wcOrder->get_payment_method()]);