Automattic\WooCommerce\Internal\Admin\Notes

WooSubscriptionsNotes::add_or_update_subscription_expiring()publicWC 1.0

Adds or updates a note for an expiring subscription.

Method of the class: WooSubscriptionsNotes{}

No Hooks.

Return

null. Nothing (null).

Usage

$WooSubscriptionsNotes = new WooSubscriptionsNotes();
$WooSubscriptionsNotes->add_or_update_subscription_expiring( $subscription );
$subscription(array) (required)
The subscription to work with.

WooSubscriptionsNotes::add_or_update_subscription_expiring() code WC 8.6.1

public function add_or_update_subscription_expiring( $subscription ) {
	$product_id            = $subscription['product_id'];
	$product_name          = $subscription['product_name'];
	$expires               = intval( $subscription['expires'] );
	$time_now_gmt          = current_time( 'timestamp', 0 );
	$days_until_expiration = intval( ceil( ( $expires - $time_now_gmt ) / DAY_IN_SECONDS ) );

	$note = $this->find_note_for_product_id( $product_id );

	if ( $note ) {
		$content_data = $note->get_content_data();
		if ( property_exists( $content_data, 'days_until_expiration' ) ) {
			// Note: There is no reason this property should not exist. This is just defensive programming.
			$note_days_until_expiration = intval( $content_data->days_until_expiration );
			if ( $days_until_expiration === $note_days_until_expiration ) {
				// Note is already up to date. Bail.
				return;
			}

			// If we have a note and we are at or have crossed a threshold, we should delete
			// the old note and create a new one, thereby "bumping" the note to the top of the inbox.
			$bump_thresholds    = $this->get_bump_thresholds();
			$crossing_threshold = false;

			foreach ( (array) $bump_thresholds as $bump_threshold ) {
				if ( ( $note_days_until_expiration > $bump_threshold ) && ( $days_until_expiration <= $bump_threshold ) ) {
					$note->delete();
					$note = false;
					continue;
				}
			}
		}
	}

	$note_title = sprintf(
		/* translators: name of the extension subscription expiring soon */
		__( '%s subscription expiring soon', 'woocommerce' ),
		$product_name
	);

	$note_content = sprintf(
		/* translators: number of days until the subscription expires */
		__( 'Your subscription expires in %d days. Enable autorenew to avoid losing updates and access to support.', 'woocommerce' ),
		$days_until_expiration
	);

	$note_content_data = (object) array(
		'product_id'            => $product_id,
		'product_name'          => $product_name,
		'expired'               => false,
		'days_until_expiration' => $days_until_expiration,
	);

	if ( ! $note ) {
		$note = new Note();
	}

	// Reset everything in case we are repurposing an expired note as an expiring note.
	$note->set_title( $note_title );
	$note->set_type( Note::E_WC_ADMIN_NOTE_WARNING );
	$note->set_name( self::SUBSCRIPTION_NOTE_NAME );
	$note->set_source( 'woocommerce-admin' );
	$note->clear_actions();
	$note->add_action(
		'enable-autorenew',
		__( 'Enable Autorenew', 'woocommerce' ),
		'https://woo.com/my-account/my-subscriptions/?utm_medium=product'
	);
	$note->set_content( $note_content );
	$note->set_content_data( $note_content_data );
	$note->save();
}