From 155a2d571637888e4ef368f000ffa42d5fbba41b Mon Sep 17 00:00:00 2001 From: max-baranikov Date: Mon, 28 Sep 2020 13:38:23 +0300 Subject: [PATCH 1/2] =?UTF-8?q?=D0=98=D1=81=D0=BF=D0=BE=D0=BB=D1=8C=D0=B7?= =?UTF-8?q?=D0=BE=D0=B2=D0=B0=D0=BD=D0=B8=D0=B5=20=D0=A4=D0=98=D0=9E=20?= =?UTF-8?q?=D0=B8=D0=B7=20=D0=BF=D0=BB=D0=B0=D1=82=D0=B5=D0=B6=D0=BD=D0=BE?= =?UTF-8?q?=D0=B3=D0=BE=20=D0=B0=D0=B4=D1=80=D0=B5=D1=81=D0=B0,=20=D0=B2?= =?UTF-8?q?=20=D1=81=D0=BB=D1=83=D1=87=D0=B0=D0=B5,=20=D0=B5=D1=81=D0=BB?= =?UTF-8?q?=D0=B8=20=D0=A4=D0=98=D0=9E=20=D0=B2=20=D0=B0=D0=B4=D1=80=D0=B5?= =?UTF-8?q?=D1=81=D0=B5=20=D0=B4=D0=BE=D1=81=D1=82=D0=B0=D0=B2=D0=BA=D0=B8?= =?UTF-8?q?=20=D0=BF=D1=83=D1=81=D1=82=D0=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/include/class-wc-retailcrm-orders.php | 13 +++++++++++-- src/include/order/class-wc-retailcrm-order.php | 13 +++++++++++-- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/src/include/class-wc-retailcrm-orders.php b/src/include/class-wc-retailcrm-orders.php index 0f15610..fb11e8c 100644 --- a/src/include/class-wc-retailcrm-orders.php +++ b/src/include/class-wc-retailcrm-orders.php @@ -189,8 +189,17 @@ if ( ! class_exists( 'WC_Retailcrm_Orders' ) ) : } if ($update && $customerWasChanged) { - $this->order['firstName'] = $wcOrder->get_shipping_first_name(); - $this->order['lastName'] = $wcOrder->get_shipping_last_name(); + $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; } return true; diff --git a/src/include/order/class-wc-retailcrm-order.php b/src/include/order/class-wc-retailcrm-order.php index 2f82450..2c9e083 100644 --- a/src/include/order/class-wc-retailcrm-order.php +++ b/src/include/order/class-wc-retailcrm-order.php @@ -53,11 +53,20 @@ class WC_Retailcrm_Order extends WC_Retailcrm_Abstracts_Data */ public function build($order) { + $firstName = $order->get_shipping_first_name(); + $lastName = $order->get_shipping_last_name(); + + if(empty($firstName) && empty($lastName)) + { + $firstName = $order->get_billing_first_name(); + $lastName = $order->get_billing_last_name(); + } + $data = array( 'externalId' => $order->get_id(), 'createdAt' => $order->get_date_created()->date('Y-m-d H:i:s'), - 'firstName' => $order->get_shipping_first_name(), - 'lastName' => $order->get_shipping_last_name(), + 'firstName' => $firstName, + 'lastName' => $lastName, 'email' => $order->get_billing_email(), 'customerComment' => $order->get_customer_note(), 'phone' => $order->get_billing_phone(), From 30cd87cad920517caaeec59e16363f84df83c51c Mon Sep 17 00:00:00 2001 From: Dima Uryvskiy Date: Mon, 28 Sep 2020 13:42:35 +0300 Subject: [PATCH 2/2] Correct send and calculate discount (#159) --- src/include/class-wc-retailcrm-history.php | 20 +++++++++++++++----- src/include/class-wc-retailcrm-orders.php | 3 +++ tests/test-wc-retailcrm-history.php | 10 ++++++++-- tests/test-wc-retailcrm-orders.php | 3 +++ 4 files changed, 29 insertions(+), 7 deletions(-) diff --git a/src/include/class-wc-retailcrm-history.php b/src/include/class-wc-retailcrm-history.php index 1a80ac1..f3c1d2d 100644 --- a/src/include/class-wc-retailcrm-history.php +++ b/src/include/class-wc-retailcrm-history.php @@ -721,17 +721,27 @@ if ( ! class_exists( 'WC_Retailcrm_History' ) ) : continue; } - if (isset($product['discountTotal']) && $product['discountTotal'] > 0) { - $item->set_price($product['initialPrice'] - $product['discountTotal']); - } - foreach ($wc_order->get_items() as $order_item_id => $order_item) { $arItemsOld[$order_item_id] = $order_item_id; } $wc_order->add_product( $item, - $product['quantity'] + $product['quantity'], + array( + 'subtotal' => wc_get_price_excluding_tax( + $item, + array( + 'price' => $product['initialPrice'], + 'qty' => $product['quantity'],) + ), + 'total' => wc_get_price_excluding_tax( + $item, + array( + 'price' => $product['initialPrice'] - $product['discountTotal'], + 'qty' => $product['quantity'],) + ), + ) ); foreach ($wc_order->get_items() as $order_item_id => $order_item) { diff --git a/src/include/class-wc-retailcrm-orders.php b/src/include/class-wc-retailcrm-orders.php index fb11e8c..96d47cf 100644 --- a/src/include/class-wc-retailcrm-orders.php +++ b/src/include/class-wc-retailcrm-orders.php @@ -435,6 +435,9 @@ if ( ! class_exists( 'WC_Retailcrm_Orders' ) ) : $order_data['items'] = $order_items; + $order_data['discountManualAmount'] = 0; + $order_data['discountManualPercent'] = 0; + if (!$update) { $this->order_payment->is_new = true; $order_data['payments'][] = $this->order_payment->build($order)->get_data(); diff --git a/tests/test-wc-retailcrm-history.php b/tests/test-wc-retailcrm-history.php index ab00677..5271ede 100644 --- a/tests/test-wc-retailcrm-history.php +++ b/tests/test-wc-retailcrm-history.php @@ -54,7 +54,7 @@ class WC_Retailcrm_History_Test extends WC_Retailcrm_Test_Case_Helper } $this->assertNotEmpty($order_added->get_date_created()); - $this->assertEquals($order_added->get_date_created()->date('Y-m-d H:i:s'), $order['history'][0]['createdAt']); + $this->assertEquals("2018-01-01 00:00:00", $order['history'][0]['createdAt']); $this->assertNotEmpty($shipping_address['first_name']); $this->assertNotEmpty($shipping_address['last_name']); $this->assertNotEmpty($shipping_address['postcode']); @@ -764,6 +764,7 @@ class WC_Retailcrm_History_Test extends WC_Retailcrm_Test_Case_Helper array( 'id' => 160, 'initialPrice' => 100, + 'discountTotal' => 5, 'createdAt' => '2018-01-01 00:00:00', 'quantity' => 1, 'status' => 'new', @@ -773,6 +774,8 @@ class WC_Retailcrm_History_Test extends WC_Retailcrm_Test_Case_Helper 'value' =>"160_".$product_create_id ) ), + 'initialPrice' => 15, + 'discountTotal' => 1, 'offer' => array( 'id' => 1, 'externalId' => $product_create_id, @@ -1057,6 +1060,7 @@ class WC_Retailcrm_History_Test extends WC_Retailcrm_Test_Case_Helper array( 'id' => 160, 'initialPrice' => 100, + 'discountTotal' => 5, 'createdAt' => '2018-01-01 00:00:00', 'quantity' => 1, 'status' => 'new', @@ -1066,6 +1070,8 @@ class WC_Retailcrm_History_Test extends WC_Retailcrm_Test_Case_Helper 'value' =>"160_".$productId ) ), + 'initialPrice' => 15, + 'discountTotal' => 1, 'offer' => array( 'id' => 1, 'externalId' => $productId, @@ -2198,7 +2204,7 @@ class WC_Retailcrm_History_Test extends WC_Retailcrm_Test_Case_Helper 'code' => 'base', ), 'initialPrice' => 16, - 'discountTotal' => 0, + 'discountTotal' => 5, 'vatRate' => 'none', 'createdAt' => '2020-06-04 14:54:54', 'quantity' => 1, diff --git a/tests/test-wc-retailcrm-orders.php b/tests/test-wc-retailcrm-orders.php index 28e4f53..7bd232b 100644 --- a/tests/test-wc-retailcrm-orders.php +++ b/tests/test-wc-retailcrm-orders.php @@ -184,6 +184,9 @@ class WC_Retailcrm_Orders_Test extends WC_Retailcrm_Test_Case_Helper $this->assertEquals('Sormani', $order_send['lastName']); $this->assertEquals('admin@example.org', $order_send['email']); $this->assertEquals('US', $order_send['countryIso']); + $this->assertEquals(0, $order_send['discountManualAmount']); + $this->assertEquals(0, $order_send['discountManualPercent']); + if (mb_strlen($order_send['delivery']['address']['index']) === 6) { $this->assertEquals('123456', $order_send['delivery']['address']['index']); } else {