From a2a0c8b408e13062cdd4b19d30c0199306868106 Mon Sep 17 00:00:00 2001 From: Marcus Dahl Date: Mon, 23 Dec 2024 16:40:57 +0100 Subject: [PATCH] fix: agreements cancelled in app are now properly reflected in Woo --- README.txt | 1 + includes/wc-gateway-vipps-recurring.php | 33 ++++++++++++++----------- 2 files changed, 19 insertions(+), 15 deletions(-) diff --git a/README.txt b/README.txt index baad43f..7f5af24 100755 --- a/README.txt +++ b/README.txt @@ -164,6 +164,7 @@ Alternatively you could look into using WooCommerce "Early renewals": [https://d = 2.1.2 = * Fixed: Cleaning up old cancelled orders that were initiated via Checkout now works as intended. +* Fixed: Old agreements that customers now cancelled in the Vipps or MobilePay apps are now cancelled properly in WooCommerce. = 2.1.1 = * Fixed: Added two new hosts to `allowed_redirect_hosts`: `pay.mobilepay.dk` and `pay.mobilepay.fi`. This fixes a bug where customers were not able to switch to MobilePay. diff --git a/includes/wc-gateway-vipps-recurring.php b/includes/wc-gateway-vipps-recurring.php index 541fd10..57a3636 100755 --- a/includes/wc-gateway-vipps-recurring.php +++ b/includes/wc-gateway-vipps-recurring.php @@ -186,8 +186,8 @@ public function __construct() { ] ); add_action( 'woocommerce_order_status_pending_to_cancelled', [ $this, 'maybe_delete_order' ], 99999 ); - add_action( 'woocommerce_new_order', [$this, 'maybe_delete_order_later'] ); - add_action( 'woocommerce_vipps_recurring_delete_pending_order', [$this, 'maybe_delete_order'] ); + add_action( 'woocommerce_new_order', [ $this, 'maybe_delete_order_later' ] ); + add_action( 'woocommerce_vipps_recurring_delete_pending_order', [ $this, 'maybe_delete_order' ] ); add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, [ $this, @@ -2425,26 +2425,29 @@ private function maybe_get_subscription_id_from_agreement_webhook( array $webhoo if ( empty( $order_id ) && isset( $webhook_data['agreementId'] ) ) { $agreement_id = $webhook_data['agreementId']; - $options = [ - 'limit' => 1, - 'type' => 'shop_subscription', - 'meta_key' => WC_Vipps_Recurring_Helper::META_AGREEMENT_ID, - 'meta_compare' => '=', - 'meta_value' => $agreement_id, - 'return' => 'ids', - 'payment_method' => $this->id, - 'order_by' => 'post_date' - ]; + $subscription_ids = get_posts( [ + 'limit' => 1, + 'post_type' => 'shop_subscription', + 'post_status' => [ 'wc-active', 'wc-pending', 'wc-on-hold' ], + 'meta_key' => WC_Vipps_Recurring_Helper::META_AGREEMENT_ID, + 'meta_compare' => '=', + 'meta_value' => $agreement_id, + 'fields' => 'ids', + ] ); - $order_ids = wc_get_orders( $options ); - $order_id = array_pop( $order_ids ); + if ( ! empty( $subscription_ids ) ) { + return array_pop( $subscription_ids ); + } } // If the order id is not a subscription, we can get the subscription from the order if ( ! empty( $order_id ) && ! wcs_is_subscription( $order_id ) ) { $order = wc_get_order( $order_id ); $subscriptions = wcs_get_subscriptions_for_order( $order ); - $order_id = array_pop( $subscriptions ); + + if ( ! empty( $subscriptions ) ) { + return array_pop( $subscriptions ); + } } // Otherwise the order_id is either empty, or a subscription