-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(subscriptions): add cancellation reason metadata (#3568)
This PR adds meta data to cancelled woo subscriptions and uses this new meta to sync cancellation reason to ESPs for cancelled subscriptions.
- Loading branch information
1 parent
cb764e3
commit de83e02
Showing
9 changed files
with
211 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
includes/plugins/woocommerce-subscriptions/class-subscriptions-meta.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<?php | ||
/** | ||
* WooCommerce Subscriptions meta class. | ||
* | ||
* @package Newspack | ||
*/ | ||
|
||
namespace Newspack; | ||
|
||
defined( 'ABSPATH' ) || exit; | ||
|
||
/** | ||
* Main class. | ||
*/ | ||
class Subscriptions_Meta { | ||
const CANCELLATION_REASON_META_KEY = 'newspack_subscriptions_cancellation_reason'; | ||
const CANCELLATION_REASON_ADMIN_CANCELLED = 'manually-cancelled'; | ||
const CANCELLATION_REASON_ADMIN_PENDING_CANCEL = 'manually-pending-cancel'; | ||
const CANCELLATION_REASON_USER_CANCELLED = 'user-cancelled'; | ||
const CANCELLATION_REASON_USER_PENDING_CANCEL = 'user-pending-cancel'; | ||
|
||
/** | ||
* Initialize hooks and filters. | ||
*/ | ||
public static function init() { | ||
if ( ! WooCommerce_Subscriptions::is_enabled() ) { | ||
return; | ||
} | ||
|
||
add_action( 'woocommerce_subscription_status_updated', array( __CLASS__, 'maybe_record_cancelled_subscription_meta' ), 10, 3 ); | ||
} | ||
|
||
/** | ||
* Record woo custom field for cancelled subscriptions. | ||
* | ||
* @param WC_Subscription $subscription The subscription object. | ||
* @param string $to_status The status the subscription is changing to. | ||
* @param string $from_status The status the subscription is changing from. | ||
*/ | ||
public static function maybe_record_cancelled_subscription_meta( $subscription, $to_status, $from_status ) { | ||
// We only care about active, cancelled, and pending statuses. | ||
if ( ! in_array( $to_status, [ 'active', 'cancelled', 'pending-cancel' ], true ) || in_array( $from_status, [ 'cancelled', 'expired' ], true ) ) { | ||
return; | ||
} | ||
|
||
remove_action( 'woocommerce_subscription_status_updated', array( __CLASS__, 'maybe_record_cancelled_subscription_meta' ) ); | ||
|
||
$meta_value = $subscription->get_meta( self::CANCELLATION_REASON_META_KEY, true ); | ||
if ( 'active' === $to_status && $meta_value ) { | ||
$subscription->delete_meta_data( self::CANCELLATION_REASON_META_KEY ); | ||
$subscription->save(); | ||
} | ||
if ( 'cancelled' === $to_status ) { | ||
if ( self::CANCELLATION_REASON_USER_PENDING_CANCEL === $meta_value ) { | ||
$subscription->update_meta_data( self::CANCELLATION_REASON_META_KEY, self::CANCELLATION_REASON_USER_CANCELLED ); | ||
} elseif ( self::CANCELLATION_REASON_ADMIN_PENDING_CANCEL === $meta_value ) { | ||
$subscription->update_meta_data( self::CANCELLATION_REASON_META_KEY, self::CANCELLATION_REASON_ADMIN_CANCELLED ); | ||
} else { | ||
$meta_value = is_admin() ? self::CANCELLATION_REASON_ADMIN_CANCELLED : self::CANCELLATION_REASON_USER_CANCELLED; | ||
$subscription->update_meta_data( self::CANCELLATION_REASON_META_KEY, $meta_value ); | ||
} | ||
$subscription->save(); | ||
} | ||
if ( 'pending-cancel' === $to_status ) { | ||
$meta_value = is_admin() ? self::CANCELLATION_REASON_ADMIN_PENDING_CANCEL : self::CANCELLATION_REASON_USER_PENDING_CANCEL; | ||
$subscription->update_meta_data( self::CANCELLATION_REASON_META_KEY, $meta_value ); | ||
$subscription->save(); | ||
} | ||
|
||
add_action( 'woocommerce_subscription_status_updated', array( __CLASS__, 'maybe_record_cancelled_subscription_meta' ), 10, 3 ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
tests/unit-tests/plugins/woocommerce-subscriptions/class-subscriptions-meta.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
/** | ||
* Tests the WooCommerce Subscriptions integration class. | ||
* | ||
* @package Newspack\Tests | ||
*/ | ||
|
||
use Newspack\Subscriptions_Meta; | ||
use Newspack\WooCommerce_Subscriptions; | ||
|
||
/** | ||
* Test WooCommerce Subscriptions integration functionality. | ||
* | ||
* @group WooCommerce_Subscriptions_Integration | ||
*/ | ||
class Newspack_Test_Subscriptions_Meta extends WP_UnitTestCase { | ||
/** | ||
* Setup for the tests. | ||
*/ | ||
public static function set_up_before_class() { | ||
if ( ! defined( 'NEWSPACK_SUBSCRIPTIONS_EXPIRATION' ) ) { | ||
define( 'NEWSPACK_SUBSCRIPTIONS_EXPIRATION', true ); | ||
} | ||
} | ||
|
||
/** | ||
* Test Subscriptions_Meta::maybe_record_cancelled_subscription_meta. | ||
*/ | ||
public function test_maybe_record_cancelled_subscription_meta() { | ||
$subscription = wcs_create_subscription(); | ||
$this->assertEquals( | ||
'', | ||
$subscription->get_meta( Subscriptions_Meta::CANCELLATION_REASON_META_KEY ), | ||
'Cancellation reason meta should be empty before any updates.' | ||
); | ||
Subscriptions_Meta::maybe_record_cancelled_subscription_meta( $subscription, 'pending-cancel', 'cancelled' ); | ||
$this->assertEquals( | ||
'', | ||
$subscription->get_meta( Subscriptions_Meta::CANCELLATION_REASON_META_KEY ), | ||
'Cancellation reason meta should be empty when subscription from-status is cancelled.' | ||
); | ||
Subscriptions_Meta::maybe_record_cancelled_subscription_meta( $subscription, 'pending-cancel', 'active' ); | ||
$this->assertEquals( | ||
Subscriptions_Meta::CANCELLATION_REASON_USER_PENDING_CANCEL, | ||
$subscription->get_meta( Subscriptions_Meta::CANCELLATION_REASON_META_KEY ), | ||
'Cancellation reason meta should be user-pending-cancel when subscription is updated to pending-cancel from active status.' | ||
); | ||
Subscriptions_Meta::maybe_record_cancelled_subscription_meta( $subscription, 'active', 'pending-cancel' ); | ||
$this->assertEquals( | ||
'', | ||
$subscription->get_meta( Subscriptions_Meta::CANCELLATION_REASON_META_KEY ), | ||
'Cancellation reason meta should be reset when subscription is updated to active from pending-cancel status.' | ||
); | ||
Subscriptions_Meta::maybe_record_cancelled_subscription_meta( $subscription, 'cancelled', 'pending-cancel', $subscription ); | ||
$this->assertEquals( | ||
Subscriptions_Meta::CANCELLATION_REASON_USER_CANCELLED, | ||
$subscription->get_meta( Subscriptions_Meta::CANCELLATION_REASON_META_KEY ), | ||
'Cancellation reason meta should be set to user-cancelled when subscription is cancelled from pending-cancel status.' | ||
); | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
tests/unit-tests/plugins/woocommerce-subscriptions/class-woocommerce-subscriptions.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
/** | ||
* Tests the WooCommerce Subscriptions integration class. | ||
* | ||
* @package Newspack\Tests | ||
*/ | ||
|
||
use Newspack\WooCommerce_Subscriptions; | ||
use Newspack\Reader_Activation; | ||
|
||
/** | ||
* Test WooCommerce Subscriptions integration functionality. | ||
* | ||
* @group WooCommerce_Subscriptions_Integration | ||
*/ | ||
class Newspack_Test_WooCommerce_Subscriptions extends WP_UnitTestCase { | ||
/** | ||
* Setup for the tests. | ||
*/ | ||
public static function set_up_before_class() { | ||
if ( ! defined( 'NEWSPACK_SUBSCRIPTIONS_EXPIRATION' ) ) { | ||
define( 'NEWSPACK_SUBSCRIPTIONS_EXPIRATION', true ); | ||
} | ||
} | ||
|
||
/** | ||
* Test WooCommerce_Subscriptions::is_active. | ||
*/ | ||
public function test_is_active() { | ||
$is_active = WooCommerce_Subscriptions::is_active(); | ||
$this->assertTrue( $is_active, 'WooCommerce Subscriptions integration should be active when WC_Subscriptions class exists.' ); | ||
} | ||
|
||
/** | ||
* Test WooCommerce_Subscriptions::is_enabled. | ||
*/ | ||
public function test_is_enabled() { | ||
$is_enabled = WooCommerce_Subscriptions::is_enabled(); | ||
$this->assertTrue( $is_enabled, 'WooCommerce Subscriptions integration should be enabled when Feature Flag is present.' ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters