Automattic\WooCommerce\Internal\StockNotifications\Frontend

MyAccountEndpoint::get_current_user_notifications_page │ public │ WC 1.0

Return one page of the current user's active notifications, newest first.

Always scopes to get_current_user_id() the caller is never trusted.

Method of the class: MyAccountEndpoint{}

No Hooks.

Returns

array{notifications:array<Notification>, current_page:int, total_pages:int, total_items:int}.

Usage

$MyAccountEndpoint = new MyAccountEndpoint();
$MyAccountEndpoint->get_current_user_notifications_page( $current_page, $per_page ): array;
$current_page(int) (required)
1-indexed page number.
$per_page(int) (required)
Page size.

MyAccountEndpoint::get_current_user_notifications_page() code WC 11.2.1

public function get_current_user_notifications_page( int $current_page, int $per_page ): array {
	$current_page = max( 1, $current_page );
	$per_page     = max( 1, $per_page );

	$user_id = get_current_user_id();
	if ( $user_id <= 0 ) {
		return array(
			'notifications' => array(),
			'current_page'  => 1,
			'total_pages'   => 0,
			'total_items'   => 0,
		);
	}

	// Only confirmed sign-ups. PENDING rows render in their own table above this
	// one ({@see self::get_current_user_pending_notifications()}); SENT and
	// CANCELLED rows are noise here, and merchants who want a full history can
	// build their own view via {@see NotificationQuery::get_notifications()}.
	$statuses = array( NotificationStatus::ACTIVE );

	$total_items = NotificationQuery::count_notifications(
		array(
			'user_id' => $user_id,
			'status'  => $statuses,
		)
	);

	$total_pages = (int) ceil( $total_items / $per_page );

	// Clamp out-of-range pages to the last available page so a stale link
	// doesn't render an empty table.
	if ( $total_items > 0 && $current_page > $total_pages ) {
		$current_page = $total_pages;
	}

	$notifications = $total_items > 0 ? NotificationQuery::get_notifications(
		array(
			'user_id'  => $user_id,
			'status'   => $statuses,
			'order_by' => array( 'id' => 'DESC' ),
			'return'   => 'objects',
			'limit'    => $per_page,
			'offset'   => ( $current_page - 1 ) * $per_page,
		)
	) : array();

	return array(
		'notifications' => $notifications,
		'current_page'  => $current_page,
		'total_pages'   => $total_pages,
		'total_items'   => $total_items,
	);
}