Skip to content

Commit 628550f

Browse files
cesarcosta99Kristófer R
andauthored
Fix Subscriptions flows incompatible with HPOS (#2533)
* Replace post_meta methods with instance calls * Add missing save calls * Add changelog entry * Fix wc_get_orders call in WC_Stripe_Helper The previous implementation didn't actually filter the meta data, we were just lucky that the first order in the list fetched happened to be the one we needed. This commit fixes that by making sure the metadata filter is added correctly. * Fix the rest of the wc_get_orders calls --------- Co-authored-by: Kristófer R <[email protected]>
1 parent eddc99c commit 628550f

File tree

4 files changed

+47
-31
lines changed

4 files changed

+47
-31
lines changed

changelog.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
*** Changelog ***
22

3-
= 7.1.0 - 2022-xx-xx =
3+
= 7.1.0 - 2023-xx-xx =
4+
* Fix - Replace some post meta methods with equivalent methods compatible with HPOS.
45

56
= 7.0.2 - 2023-01-11 =
67
* Fix - Expand charges object from incoming webhooks using Stripe API version 2022-11-15.

includes/class-wc-stripe-helper.php

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -380,8 +380,10 @@ public static function get_order_by_source_id( $source_id ) {
380380
[
381381
'limit' => 1,
382382
'meta_query' => [
383-
'key' => '_stripe_source_id',
384-
'value' => $source_id,
383+
[
384+
'key' => '_stripe_source_id',
385+
'value' => $source_id,
386+
],
385387
],
386388
]
387389
);
@@ -445,8 +447,10 @@ public static function get_order_by_intent_id( $intent_id ) {
445447
[
446448
'limit' => 1,
447449
'meta_query' => [
448-
'key' => '_stripe_intent_id',
449-
'value' => $intent_id,
450+
[
451+
'key' => '_stripe_intent_id',
452+
'value' => $intent_id,
453+
],
450454
],
451455
]
452456
);
@@ -477,8 +481,10 @@ public static function get_order_by_setup_intent_id( $intent_id ) {
477481
[
478482
'limit' => 1,
479483
'meta_query' => [
480-
'key' => '_stripe_setup_intent',
481-
'value' => $intent_id,
484+
[
485+
'key' => '_stripe_setup_intent',
486+
'value' => $intent_id,
487+
],
482488
],
483489
]
484490
);

includes/compat/trait-wc-stripe-subscriptions.php

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -381,13 +381,15 @@ public function maybe_update_source_on_subscription_order( $order, $source ) {
381381

382382
foreach ( $subscriptions as $subscription ) {
383383
$subscription_id = $subscription->get_id();
384-
update_post_meta( $subscription_id, '_stripe_customer_id', $source->customer );
384+
$subscription->update_meta_data( '_stripe_customer_id', $source->customer );
385385

386386
if ( ! empty( $source->payment_method ) ) {
387-
update_post_meta( $subscription_id, '_stripe_source_id', $source->payment_method );
387+
$subscription->update_meta_data( '_stripe_source_id', $source->payment_method );
388388
} else {
389-
update_post_meta( $subscription_id, '_stripe_source_id', $source->source );
389+
$subscription->update_meta_data( '_stripe_source_id', $source->source );
390390
}
391+
392+
$subscription->save();
391393
}
392394
}
393395

@@ -397,13 +399,14 @@ public function maybe_update_source_on_subscription_order( $order, $source ) {
397399
* @param int $resubscribe_order The order created for the customer to resubscribe to the old expired/cancelled subscription
398400
*/
399401
public function delete_resubscribe_meta( $resubscribe_order ) {
400-
delete_post_meta( $resubscribe_order->get_id(), '_stripe_customer_id' );
401-
delete_post_meta( $resubscribe_order->get_id(), '_stripe_source_id' );
402+
$resubscribe_order->delete_meta_data( '_stripe_customer_id' );
403+
$resubscribe_order->delete_meta_data( '_stripe_source_id' );
402404
// For BW compat will remove in future.
403-
delete_post_meta( $resubscribe_order->get_id(), '_stripe_card_id' );
405+
$resubscribe_order->delete_meta_data( '_stripe_card_id' );
404406
// Delete payment intent ID.
405-
delete_post_meta( $resubscribe_order->get_id(), '_stripe_intent_id' );
407+
$resubscribe_order->delete_meta_data( '_stripe_intent_id' );
406408
$this->delete_renewal_meta( $resubscribe_order );
409+
$resubscribe_order->save();
407410
}
408411

409412
/**
@@ -416,7 +419,7 @@ public function delete_renewal_meta( $renewal_order ) {
416419
WC_Stripe_Helper::delete_stripe_net( $renewal_order );
417420

418421
// Delete payment intent ID.
419-
delete_post_meta( $renewal_order->get_id(), '_stripe_intent_id' );
422+
$renewal_order->delete_meta_data( '_stripe_intent_id' );
420423

421424
return $renewal_order;
422425
}
@@ -430,8 +433,9 @@ public function delete_renewal_meta( $renewal_order ) {
430433
* @return void
431434
*/
432435
public function update_failing_payment_method( $subscription, $renewal_order ) {
433-
update_post_meta( $subscription->get_id(), '_stripe_customer_id', $renewal_order->get_meta( '_stripe_customer_id', true ) );
434-
update_post_meta( $subscription->get_id(), '_stripe_source_id', $renewal_order->get_meta( '_stripe_source_id', true ) );
436+
$subscription->update_meta_data( '_stripe_customer_id', $renewal_order->get_meta( '_stripe_customer_id', true ) );
437+
$subscription->update_meta_data( '_stripe_source_id', $renewal_order->get_meta( '_stripe_source_id', true ) );
438+
$subscription->save();
435439
}
436440

437441
/**
@@ -446,21 +450,22 @@ public function update_failing_payment_method( $subscription, $renewal_order ) {
446450
*/
447451
public function add_subscription_payment_meta( $payment_meta, $subscription ) {
448452
$subscription_id = $subscription->get_id();
449-
$source_id = get_post_meta( $subscription_id, '_stripe_source_id', true );
453+
$source_id = $subscription->get_meta( '_stripe_source_id', true );
450454

451455
// For BW compat will remove in future.
452456
if ( empty( $source_id ) ) {
453-
$source_id = get_post_meta( $subscription_id, '_stripe_card_id', true );
457+
$source_id = $subscription->get_meta( '_stripe_card_id', true );
454458

455459
// Take this opportunity to update the key name.
456-
update_post_meta( $subscription_id, '_stripe_source_id', $source_id );
457-
delete_post_meta( $subscription_id, '_stripe_card_id', $source_id );
460+
$subscription->update_meta_data( '_stripe_source_id', $source_id );
461+
$subscription->delete_meta_data( '_stripe_card_id' );
462+
$subscription->save();
458463
}
459464

460465
$payment_meta[ $this->id ] = [
461466
'post_meta' => [
462467
'_stripe_customer_id' => [
463-
'value' => get_post_meta( $subscription_id, '_stripe_customer_id', true ),
468+
'value' => $subscription->get_meta( '_stripe_customer_id', true ),
464469
'label' => 'Stripe Customer ID',
465470
],
466471
'_stripe_source_id' => [
@@ -527,18 +532,19 @@ public function maybe_render_subscription_payment_method( $payment_method_to_dis
527532
return $payment_method_to_display;
528533
}
529534

530-
$stripe_source_id = get_post_meta( $subscription->get_id(), '_stripe_source_id', true );
535+
$stripe_source_id = $subscription->get_meta( '_stripe_source_id', true );
531536

532537
// For BW compat will remove in future.
533538
if ( empty( $stripe_source_id ) ) {
534-
$stripe_source_id = get_post_meta( $subscription->get_id(), '_stripe_card_id', true );
539+
$stripe_source_id = $subscription->get_meta( '_stripe_card_id', true );
535540

536541
// Take this opportunity to update the key name.
537-
update_post_meta( $subscription->get_id(), '_stripe_source_id', $stripe_source_id );
542+
$subscription->update_meta_data( '_stripe_source_id', $stripe_source_id );
543+
$subscription->save();
538544
}
539545

540546
$stripe_customer = new WC_Stripe_Customer();
541-
$stripe_customer_id = get_post_meta( $subscription->get_id(), '_stripe_customer_id', true );
547+
$stripe_customer_id = $subscription->get_meta( '_stripe_customer_id', true );
542548

543549
// If we couldn't find a Stripe customer linked to the subscription, fallback to the user meta data.
544550
if ( ! $stripe_customer_id || ! is_string( $stripe_customer_id ) ) {
@@ -557,15 +563,17 @@ public function maybe_render_subscription_payment_method( $payment_method_to_dis
557563

558564
// If we couldn't find a Stripe customer linked to the account, fallback to the order meta data.
559565
if ( ( ! $stripe_customer_id || ! is_string( $stripe_customer_id ) ) && false !== $subscription->get_parent() ) {
560-
$stripe_customer_id = get_post_meta( $subscription->get_parent_id(), '_stripe_customer_id', true );
561-
$stripe_source_id = get_post_meta( $subscription->get_parent_id(), '_stripe_source_id', true );
566+
$parent_order = wc_get_order( $subscription->get_parent_id() );
567+
$stripe_customer_id = $parent_order->get_meta( '_stripe_customer_id', true );
568+
$stripe_source_id = $parent_order->get_meta( '_stripe_source_id', true );
562569

563570
// For BW compat will remove in future.
564571
if ( empty( $stripe_source_id ) ) {
565-
$stripe_source_id = get_post_meta( $subscription->get_parent_id(), '_stripe_card_id', true );
572+
$stripe_source_id = $parent_order->get_meta( '_stripe_card_id', true );
566573

567574
// Take this opportunity to update the key name.
568-
update_post_meta( $subscription->get_parent_id(), '_stripe_source_id', $stripe_source_id );
575+
$parent_order->update_meta_data( '_stripe_source_id', $stripe_source_id );
576+
$parent_order->save();
569577
}
570578
}
571579

readme.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,8 @@ If you get stuck, you can ask for help in the Plugin Forum.
128128

129129
== Changelog ==
130130

131-
= 7.1.0 - 2022-xx-xx =
131+
= 7.1.0 - 2023-xx-xx =
132+
* Fix - Replace some post meta methods with equivalent methods compatible with HPOS.
132133

133134

134135
[See changelog for all versions](https://raw.githubusercontent.com/woocommerce/woocommerce-gateway-stripe/trunk/changelog.txt).

0 commit comments

Comments
 (0)