Automattic\WooCommerce\Internal\Admin\Notes

WooSubscriptionsNotes::add_or_update_subscription_expiringpublicWC 1.0

Adds or updates a note for an expiring subscription.

Method of the class: WooSubscriptionsNotes{}

No Hooks.

Returns

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 9.9.5

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 );

	// Note: There is no reason this property should not exist. This is just defensive programming.
	if ( $note && property_exists( $note->get_content_data(), 'days_until_expiration' ) ) {
		$note_days_until_expiration = intval( $note->get_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.
		foreach ( (array) self::BUMP_THRESHOLDS as $bump_threshold ) {
			if ( ( $note_days_until_expiration > $bump_threshold ) && ( $days_until_expiration <= $bump_threshold ) ) {
				$note->delete();
				$note = false;
				break;
			}
		}
	}

	$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://woocommerce.com/my-account/my-subscriptions/?utm_medium=product'
	);
	$note->set_content( $note_content );
	$note->set_content_data( $note_content_data );
	$note->save();
}