From a492a077edbc5ed190881d12c6ae8395cf1e2712 Mon Sep 17 00:00:00 2001 From: Tam Date: Fri, 23 Aug 2019 10:53:36 +0100 Subject: [PATCH] Fix issue syncing orders or carts that are missing addresses --- CHANGELOG.md | 5 +++++ composer.json | 2 +- src/services/OrdersService.php | 18 +++++++++++++----- 3 files changed, 19 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2989fb2..3ce4d06 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +## [Unreleased] 1.1.9 +### Fixed +- Fix error when syncing products without public urls (Fixes #9) +- Fix issue syncing orders or carts that are missing addresses + ## 1.1.8 - 2019-08-01 ### Fixed - Fix order sync error when address 2 isn't set (Fixes #7) diff --git a/composer.json b/composer.json index 555e6cf..ce20760 100644 --- a/composer.json +++ b/composer.json @@ -1,7 +1,7 @@ { "name": "ether/mailchimp-commerce", "description": "Mailchimp integration with Craft Commerce", - "version": "1.1.8", + "version": "1.1.9", "type": "craft-plugin", "keywords": [ "mailchimp", diff --git a/src/services/OrdersService.php b/src/services/OrdersService.php index 5d670bb..bf9b4c5 100644 --- a/src/services/OrdersService.php +++ b/src/services/OrdersService.php @@ -250,10 +250,12 @@ private function _buildOrderData ($orderId) 'last_name' => $order->billingAddress ? $order->billingAddress->lastName : '', 'orders_count' => (int) Order::find()->customer($order->customer)->isCompleted()->count(), 'total_spent' => (float) Order::find()->customer($order->customer)->isCompleted()->sum('[[commerce_orders.totalPaid]]') ?: 0, - 'address' => self::_address($order->billingAddress), ], ]; + if ($order->billingAddress) + $data['customer']['address'] = self::_address($order->billingAddress); + $cid = (new Query()) ->select('cid') ->from('{{%mc_orders_synced}}') @@ -281,16 +283,22 @@ private function _buildOrderData ($orderId) if ($order->isCompleted) { - $data = array_merge($data, [ + $completeData = [ 'financial_status' => $order->lastTransaction ? $order->lastTransaction->status : 'paid', 'discount_total' => (float) $order->getAdjustmentsTotalByType('discount'), 'tax_total' => (float) $order->getAdjustmentsTotalByType('tax'), 'shipping_total' => (float) $order->getAdjustmentsTotalByType('shipping'), 'processed_at_foreign' => $order->dateOrdered->format('c'), 'updated_at_foreign' => $order->dateUpdated->format('c'), - 'shipping_address' => self::_address($order->shippingAddress), - 'billing_address' => self::_address($order->billingAddress), - ]); + ]; + + if ($order->shippingAddress) + $completeData['shipping_address'] = self::_address($order->shippingAddress); + + if ($order->billingAddress) + $completeData['billing_address'] = self::_address($order->billingAddress); + + $data = array_merge($data, $completeData); if ($order->returnUrl) $data['order_url'] = UrlHelper::siteUrl($order->returnUrl);