Automattic\WooCommerce\Blocks\Utils

CartCheckoutUtils::is_page_typeprivate staticWC 1.0

Returns true if the current page is a specific page type (cart or checkout).

This is determined by looking at the global $post object and comparing it to the post ID defined in settings, or checking the page contents for a block or shortcode.

This function cannot be used accurately before the pre_get_posts has been run.

Method of the class: CartCheckoutUtils{}

No Hooks.

Returns

true|false|null.

Usage

$result = CartCheckoutUtils::is_page_type( $page_type ): ?bool;
$page_type(string) (required)
The page type to check for.

CartCheckoutUtils::is_page_type() code WC 10.7.0

private static function is_page_type( string $page_type ): ?bool {
	if ( ! did_action( 'pre_get_posts' ) ) {
		return null;
	}

	$page_id = wc_get_page_id( $page_type );

	if ( $page_id && is_page( $page_id ) ) {
		return true;
	}

	// If the is_page check returned false, check the page contents for a cart block or shortcode.
	global $post;

	if ( null === $post ) {
		return null;
	}

	if ( $post instanceof \WP_Post ) {
		return wc_post_content_has_shortcode( 'cart' === $page_type ? 'woocommerce_cart' : 'woocommerce_checkout' ) || self::has_block_variation( 'woocommerce/classic-shortcode', 'shortcode', $page_type, $post->post_content );
	}

	return false;
}