Skip to content

Commit

Permalink
Merge pull request #111 from KMityai/master
Browse files Browse the repository at this point in the history
fixed customer transmission
  • Loading branch information
gwinn authored Aug 28, 2019
2 parents db56c05 + 1b2f0d0 commit 55a5266
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 20 deletions.
4 changes: 2 additions & 2 deletions src/include/class-wc-retailcrm-customers.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,8 @@ protected function processCustomer($customer)
*/
public function searchCustomer($filter)
{
if (isset($filter['id'])) {
$search = $this->retailcrm->customersGet($filter['id']);
if (isset($filter['externalId'])) {
$search = $this->retailcrm->customersGet($filter['externalId']);
} elseif (isset($filter['email'])) {
$search = $this->retailcrm->customersList(array('email' => $filter['email']));
}
Expand Down
43 changes: 25 additions & 18 deletions src/include/class-wc-retailcrm-orders.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,38 +125,45 @@ public function orderCreate($order_id)
return null;
}

$order = wc_get_order($order_id);
$this->processOrder($order);
$customer = $order->get_user();
$wcOrder = wc_get_order($order_id);
$this->processOrder($wcOrder);
$wpUser = $wcOrder->get_user();

if ($wpUser instanceof WP_User) {
$wpUserId = (int)$wpUser->get('ID');
$foundCustomer = $this->customers->searchCustomer(array(
'externalId' => $wpUserId
));

if ($customer != false) {
$search = $this->customers->searchCustomer(array('id' => $customer->get('ID')));
if (empty($foundCustomer)) {
$customerId = $this->customers->createCustomer($wpUserId);

if (!$search) {
$this->customers->createCustomer($customer);
if (!empty($customerId)) {
$this->order['customer']['id'] = $customerId;
}
} else {
$this->order['customer']['externalId'] = $search['externalId'];
$this->order['customer']['externalId'] = $foundCustomer['externalId'];
}
} else {
$search = $this->customers->searchCustomer(array('email' => $order->get_billing_email()));
$foundCustomer = $this->customers->searchCustomer(array(
'email' => $wcOrder->get_billing_email()
));

if (!$search) {
$new_customer = $this->customers->buildCustomerFromOrderData($order);
$id = $this->customers->createCustomer($new_customer);
if (empty($foundCustomer)) {
$wcCustomer = $this->customers->buildCustomerFromOrderData($wcOrder);
$customerId = $this->customers->createCustomer($wcCustomer);

if ($id !== null) {
$this->order['customer']['id'] = $id;
if (!empty($customerId)) {
$this->order['customer']['id'] = $customerId;
}
} else {
$this->order['customer']['externalId'] = $search['externalId'];
$this->order['customer']['externalId'] = $foundCustomer['externalId'];
}

unset($new_customer);
}

$this->retailcrm->ordersCreate($this->order);

return $order;
return $wcOrder;
}

/**
Expand Down

0 comments on commit 55a5266

Please sign in to comment.