Render notice about payment method (#47141)

* fetch notice from woocom for connected store

* show payment mehtod notice on setting page

* Combine missing payment method and expiring notice

* Shorter TTL for notices transient

---------

Co-authored-by: Akeda Bagus <akeda.bagus@automattic.com>
This commit is contained in:
Manzur Ahammed 2024-06-06 05:23:03 +02:00 committed by GitHub
parent 391ab0f81a
commit 22309f0484
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 69 additions and 0 deletions

View File

@ -0,0 +1,4 @@
Significance: minor
Type: add
Display an admin notice in Setting and Extension pages when there are expiring subscriptions and connected account doesn't have a payment method.

View File

@ -2261,6 +2261,42 @@ class WC_Helper {
return $woo_com_base_url . 'auto-install-init/';
}
/**
* Retrieve notice for connected store.
*
* @return array An array containing notice data.
*/
public static function get_notices() {
$cache_key = '_woocommerce_helper_notices';
$cached_data = get_transient( $cache_key );
if ( false !== $cached_data ) {
return $cached_data;
}
// Fetch notice data for connected store.
$request = WC_Helper_API::get(
'notices',
array(
'authenticated' => true,
)
);
if ( 200 !== wp_remote_retrieve_response_code( $request ) ) {
set_transient( $cache_key, array(), 15 * MINUTE_IN_SECONDS );
return array();
}
$data = json_decode( wp_remote_retrieve_body( $request ), true );
if ( empty( $data ) || ! is_array( $data ) ) {
$data = array();
}
set_transient( $cache_key, $data, 1 * HOUR_IN_SECONDS );
return $data;
}
}
WC_Helper::load();

View File

@ -821,6 +821,35 @@ class PluginsHelper {
$total_expiring_subscriptions = count( $expiring_subscriptions );
// When payment method is missing on WooCommerce.com.
$helper_notices = WC_Helper::get_notices();
if ( ! empty( $helper_notices['missing_payment_method_notice'] ) ) {
$description = $allowed_link
? sprintf(
/* translators: %s: WooCommerce.com URL to add payment method */
_n(
'Your WooCommerce extension subscription is missing a payment method for renewal. <a href="%s">Add a payment method</a> to ensure you continue receiving updates and streamlined support.',
'Your WooCommerce extension subscriptions are missing a payment method for renewal. <a href="%s">Add a payment method</a> to ensure you continue receiving updates and streamlined support.',
$total_expiring_subscriptions,
'woocommerce'
),
'https://woocommerce.com/my-account/add-payment-method/'
)
: _n(
'Your WooCommerce extension subscription is missing a payment method for renewal. Add a payment method to ensure you continue receiving updates and streamlined support.',
'Your WooCommerce extension subscriptions are missing a payment method for renewal. Add a payment method to ensure you continue receiving updates and streamlined support.',
$total_expiring_subscriptions,
'woocommerce'
);
return array(
'description' => $description,
'button_text' => __( 'Add payment method', 'woocommerce' ),
'button_link' => 'https://woocommerce.com/my-account/add-payment-method/',
);
}
// Payment method is available but there are expiring subscriptions.
$notice_data = self::get_subscriptions_notice_data(
$subscriptions,
$expiring_subscriptions,