Skip to content

Commit 0756cce

Browse files
haszariRua Haszard
andauthored
Use recommended order currency API for multicurrency subscription renewal | switch | resubscribe (#4228)
* use recommended `WC_Order::get_currency()` for getting renewal currency: - previously used raw `get_post_meta()` - this is no longer recommended, and could break on stores using (forthcoming) custom order tables * add changelog * convert id to order/subscription object before calling get_currency: - fix logic issue in previous commit * fix fatal error on resubscribe – get subscription from ID * fix override_selected_currency test for renewal case: - Make a real order with non-default currency - integration test style. - Enhance mock_wcs_cart_contains_renewal so test can customise product and renewal order ids. - Mock the real order instead of hacking post meta (legacy implementation detail). - Fix up all uses of mock_wcs_cart_contains_renewal() for new signature. * Repair multicurrency sub switch unit test: - Rename / update comment to clarify test scope. - Mock up a real order (aka subscription) with custom currency. - Mock that order idea in $_GET request params. * repair multicurrency subs switch in cart unit test: - use WC_Order for mock subscription - mock subscription and cart APIs as needed * refactor mock_wcs_get_order_type_cart_items so can pass a real order id * repair multicurrency sub resubscribe unit test: - parameterise wcs_get_order_type_cart_items so can use real order id - mock sub (order) and wcs_get_subscription to return mocked sub - it's fine right * remove a bunch of extraneous whitespace / php linter fixes * remove extraneous whitespace / php linter fixes * add local wrapper for wcs_get_subscription() – attempt psalm fix: - ERROR: UndefinedFunction $switch_subscription = wcs_get_subscription( $switch_cart_item['subscription_switch']['subscription_id'] ); * fix mismatched param name in docblock for get_subscription wrapper * remove incorrect return type (array) from get_subscription test helper + + clarify true type in docblock comment * remove whitespace offending the linter * rename subscription variable – it's resubscribe not switch Co-authored-by: Rua Haszard <[email protected]>
1 parent 21c653e commit 0756cce

File tree

3 files changed

+126
-57
lines changed

3 files changed

+126
-57
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Significance: minor
2+
Type: fix
3+
4+
Use high-level order currency API for multicurrency subscription renewal orders (get_post_meta is not recommended for orders).

includes/multi-currency/Compatibility/WooCommerceSubscriptions.php

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,8 @@ public function override_selected_currency( $return ) {
148148

149149
$subscription_renewal = $this->cart_contains_renewal();
150150
if ( $subscription_renewal ) {
151-
return get_post_meta( $subscription_renewal['subscription_renewal']['renewal_order_id'], '_order_currency', true );
151+
$order = wc_get_order( $subscription_renewal['subscription_renewal']['renewal_order_id'] );
152+
return $order ? $order->get_currency() : $return;
152153
}
153154

154155
$switch_id = $this->get_subscription_switch_id_from_superglobal();
@@ -158,13 +159,15 @@ public function override_selected_currency( $return ) {
158159

159160
$switch_cart_items = $this->get_subscription_switch_cart_items();
160161
if ( 0 < count( $switch_cart_items ) ) {
161-
$switch_cart_item = array_shift( $switch_cart_items );
162-
return get_post_meta( $switch_cart_item['subscription_switch']['subscription_id'], '_order_currency', true );
162+
$switch_cart_item = array_shift( $switch_cart_items );
163+
$switch_subscription = $this->get_subscription( $switch_cart_item['subscription_switch']['subscription_id'] );
164+
return $switch_subscription ? $switch_subscription->get_currency() : $return;
163165
}
164166

165167
$subscription_resubscribe = $this->cart_contains_resubscribe();
166168
if ( $subscription_resubscribe ) {
167-
return get_post_meta( $subscription_resubscribe['subscription_resubscribe']['subscription_id'], '_order_currency', true );
169+
$subscription = $this->get_subscription( $subscription_resubscribe['subscription_resubscribe']['subscription_id'] );
170+
return $subscription ? $subscription->get_currency() : $return;
168171
}
169172

170173
return $return;
@@ -293,6 +296,21 @@ private function get_subscription_switch_cart_items(): array {
293296
return wcs_get_order_type_cart_items( 'switch' );
294297
}
295298

299+
/**
300+
* Getter for subscription objects.
301+
*
302+
* @param mixed $the_subscription Post object or post ID of the order.
303+
* @return mixed The subscription object, or false if it cannot be found.
304+
* Note: this is WC_Subscription|bool in normal use, but in tests
305+
* we use WC_Order to simulate a subscription (hence `mixed`).
306+
*/
307+
private function get_subscription( $the_subscription ) {
308+
if ( ! function_exists( 'wcs_get_subscription' ) ) {
309+
return false;
310+
}
311+
return wcs_get_subscription( $the_subscription );
312+
}
313+
296314
/**
297315
* Checks $_GET superglobal for a switch id and returns it if found.
298316
*

0 commit comments

Comments
 (0)