← All changes
|
includes/controllers/WooMailerLiteOrderController.php
+31
-16
3.1.18
→
trunk
View file →
| @@ -14,9 +14,9 @@ | ||
| 14 | 14 | if (!$order->get_billing_email()) { |
| 15 | 15 | return true; |
| 16 | 16 | } |
| 17 | 17 | |
| 18 | - $this->persistLanguageToOrder($order); | |
| 18 | + WooMailerLiteLanguageService::persistToOrder($order); | |
| 19 | 19 | |
| 20 | 20 | if (WooMailerLiteSession::getMLCartHash()) { |
| 21 | 21 | $order->add_meta_data('_woo_ml_cart_hash', WooMailerLiteSession::getMLCartHash()); |
| 22 | 22 | } |
| @@ -27,8 +27,18 @@ | ||
| 27 | 27 | ->where("email", $order->get_billing_email()) |
| 28 | 28 | ->whereIn("status", WooMailerLiteOptions::allSupportedOrderStatuses()) |
| 29 | 29 | ->first(); |
| 30 | 30 | } |
| 31 | + // Query live order stats directly to avoid stale wc_order_stats reads when | |
| 32 | + // multiple orders are placed in quick succession for the same email. | |
| 33 | + $liveOrders = wc_get_orders([ | |
| 34 | + 'billing_email' => $order->get_billing_email(), | |
| 35 | + 'status' => WooMailerLiteOptions::COMPLETE_ORDER_STATUSES, | |
| 36 | + 'limit' => -1, | |
| 37 | + ]); | |
| 38 | + $liveOrdersCount = count($liveOrders); | |
| 39 | + $liveTotalSpent = (float) array_sum(array_map(fn($o) => $o->get_total(), $liveOrders)); | |
| 40 | + | |
| 31 | 41 | $cart = WooMailerLiteCart::where('email', $order->get_billing_email())->first(); |
| 32 | 42 | if (!$cart) { |
| 33 | 43 | $cart = WooMailerLiteCart::where('hash', WooMailerLiteSession::getMLCartHash()) |
| 34 | 44 | ->withoutPrefix(function($query) use ($order) { |
| @@ -71,12 +81,19 @@ | ||
| 71 | 81 | $syncFields[] = 'name'; |
| 72 | 82 | } |
| 73 | 83 | |
| 74 | 84 | $customerFields = array_intersect_key($filteredCustomerData, array_flip($syncFields)); |
| 75 | - if (WooMailerLiteOptions::get('settings.languageField')) { | |
| 76 | - $customerFields['subscriber_language'] = $order->get_meta('_woo_ml_language'); | |
| 85 | + | |
| 86 | + if (in_array('phone', $syncFields) && empty($customerFields['phone']) && $order->get_billing_phone()) { | |
| 87 | + $customerFields['phone'] = $order->get_billing_phone(); | |
| 77 | 88 | } |
| 78 | 89 | |
| 90 | + if (in_array('company', $syncFields) && empty($customerFields['company']) && $order->get_billing_company()) { | |
| 91 | + $customerFields['company'] = $order->get_billing_company(); | |
| 92 | + } | |
| 93 | + | |
| 94 | + $customerFields = WooMailerLiteLanguageService::applyTo($customerFields, $order); | |
| 95 | + | |
| 79 | 96 | $subscribe = false; |
| 80 | 97 | $email = $customer->email ?? $order->get_billing_email(); |
| 81 | 98 | $subscribeCacheKey = 'woo_ml_subscribe_checkbox:'.$email; |
| 82 | 99 | if ($cart && isset($cart->subscribe)) { |
| @@ -99,10 +116,10 @@ | ||
| 99 | 116 | 'email' => $customer->email ?? $order->get_billing_email(), |
| 100 | 117 | 'create_subscriber' => $subscribe, |
| 101 | 118 | 'accepts_marketing' => $subscribe, |
| 102 | 119 | 'subscriber_fields' => $customerFields, |
| 103 | - 'total_spent' => ($customer->total_spent ?? $order->get_total()), | |
| 104 | - 'orders_count' => ($customer->orders_count ?? 1), | |
| 120 | + 'total_spent' => $liveTotalSpent, | |
| 121 | + 'orders_count' => $liveOrdersCount, | |
| 105 | 122 | 'last_order_id' => $customer->last_order_id ?? null, |
| 106 | 123 | 'last_order' => $customer->last_order ?? null |
| 107 | 124 | ]; |
| 108 | 125 | $items = []; |
| @@ -110,8 +127,9 @@ | ||
| 110 | 127 | foreach ($order->get_items() as $item) { |
| 111 | 128 | if ($item->get_product_id() !== 0) { |
| 112 | 129 | $items[] = [ |
| 113 | 130 | 'product_resource_id' => (string)$item->get_product_id(), |
| 131 | + 'ecommerce_product_variant_id' => (string) $item->get_variation_id(), | |
| 114 | 132 | 'variant' => $item->get_name(), |
| 115 | 133 | 'quantity' => $item->get_quantity(), |
| 116 | 134 | 'price' => (float)$item->get_product()->get_price() |
| 117 | 135 | ]; |
| @@ -124,10 +142,10 @@ | ||
| 124 | 142 | if ($this->apiClient()->isClassic()) { |
| 125 | 143 | $orderData['order'] = $order->get_data(); |
| 126 | 144 | $orderData['checkout_id'] = $cart->data['checkout_id'] ?? null; |
| 127 | 145 | $orderData['order_url'] = home_url() . "/wp-admin/post.php?post=" . $orderId . "&action=edit"; |
| 128 | - $customerFields['woo_total_spent'] = ($customer->total_spent ?? $order->get_total()); | |
| 129 | - $customerFields['woo_orders_count'] = ($customer->orders_count ?? 1); | |
| 146 | + $customerFields['woo_total_spent'] = $liveTotalSpent; | |
| 147 | + $customerFields['woo_orders_count'] = $liveOrdersCount; | |
| 130 | 148 | $customerFields['woo_last_order_id'] = $customer->last_order_id ?? null; |
| 131 | 149 | $customerFields['woo_last_order'] = $customer->last_order ?? nulL; |
| 132 | 150 | $data = [ |
| 133 | 151 | 'email' => $customer->email, |
| @@ -231,16 +249,13 @@ | ||
| 231 | 249 | return true; |
| 232 | 250 | } |
| 233 | 251 | } |
| 234 | 252 | |
| 235 | - private function persistLanguageToOrder($order): void | |
| 253 | + /** | |
| 254 | + * Snapshots the language while the order is being created, so gateway | |
| 255 | + * callbacks and later status changes do not need the shopper's session. | |
| 256 | + */ | |
| 257 | + public function persistLanguageForOrder($order): void | |
| 236 | 258 | { |
| 237 | - $key = '_woo_ml_language'; | |
| 238 | - if (isset(WC()->session)) { | |
| 239 | - $language = WC()->session->get($key); | |
| 240 | - if ($language && !$order->get_meta($key)) { | |
| 241 | - $order->add_meta_data($key, $language); | |
| 242 | - $order->save(); | |
| 243 | - } | |
| 244 | - } | |
| 259 | + WooMailerLiteLanguageService::persistToOrder($order); | |
| 245 | 260 | } |
| 246 | 261 | } |