Automattic\WooCommerce\Admin
PluginsHelper::get_subscriptions_notice_data
Construct the subscription notice data based on user subscriptions data.
Method of the class: PluginsHelper{}
No Hooks.
Returns
Array. notice data to return. Contains type, parsed_message and product_id (can be a single value or an array).
Usage
$result = PluginsHelper::get_subscriptions_notice_data( $all_subs, $subs_to_show, $total, $messages, $type );
- $all_subs(array) (required)
- all subscription data.
- $subs_to_show(array) (required)
- filtered subscriptions as condition.
- $total(int) (required)
- total subscription count.
- $messages(array) (required)
- message.
- $type(string) (required)
- type of notice, whether it is for expiring or expired subscription.
PluginsHelper::get_subscriptions_notice_data() PluginsHelper::get subscriptions notice data code WC 10.6.2
public static function get_subscriptions_notice_data( array $all_subs, array $subs_to_show, int $total, array $messages, string $type ) {
$utm_campaign = 'expired' === $type ?
'pu_settings_screen_renew' :
( 'missing' === $type ? 'pu_settings_screen_purchase' : 'pu_settings_screen_enable_autorenew' );
if ( 1 < $total ) {
$hyperlink_url = add_query_arg(
array(
'utm_source' => 'pu',
'utm_campaign' => $utm_campaign,
),
self::WOO_SUBSCRIPTION_PAGE_URL
);
$parsed_message = sprintf(
$messages['different_subscriptions'],
esc_attr( $total ),
esc_url( $hyperlink_url ),
esc_attr( $total ),
);
// All product ids.
$product_ids = array_map(
function ( $sub ) {
return $sub['product_id'];
},
$subs_to_show
);
return array(
'type' => 'different_subscriptions',
'parsed_message' => $parsed_message,
'product_id' => $product_ids,
);
}
$subscription = reset( $subs_to_show );
$product_id = $subscription['product_id'];
// check if $all_subs has multiple subs for this product.
$has_multiple_subs_for_product = 1 < count(
array_filter(
$all_subs,
function ( $sub ) use ( $product_id ) {
return $product_id === $sub['product_id'];
}
)
);
$message_key = $has_multiple_subs_for_product ? 'multiple_manage' : 'single_manage';
$renew_string = __( 'Renew', 'woocommerce' );
$subscribe_string = __( 'Subscribe', 'woocommerce' );
if ( isset( $subscription['product_regular_price'] ) ) {
/* translators: 1: Product price */
$renew_string = sprintf( __( 'Renew for %1$s', 'woocommerce' ), $subscription['product_regular_price'] );
}
$expiry_date = date_i18n( 'F jS', $subscription['expires'] );
$hyperlink_url = add_query_arg(
array(
'product_id' => $product_id,
'type' => $type,
'utm_source' => 'pu',
'utm_campaign' => $utm_campaign,
),
self::WOO_SUBSCRIPTION_PAGE_URL
);
// Construct message based on template for multiple_manage or single_manage, parameter used:
// 1. Product name
// 2. Expiry date
// 3. URL to My Subscriptions page with extra params
// 4. Renew string.
if ( isset( $messages[ $message_key ] ) ) {
$parsed_message = sprintf(
$messages[ $message_key ],
esc_attr( $subscription['product_name'] ),
esc_attr( $expiry_date ),
esc_url( $hyperlink_url ),
// Show subscribe for missing subscriptions, renew otherwise.
'missing' === $type ? esc_attr( $subscribe_string ) : esc_attr( $renew_string ),
);
return array(
'type' => $message_key,
'parsed_message' => $parsed_message,
'product_id' => $product_id,
);
}
return array(
'type' => 'invalid',
'parsed_message' => '',
'product_id' => '',
);
}