Automattic\WooCommerce\Blocks\Utils

CartCheckoutUtils::has_block_variationpublic staticWC 1.0

Check if the post content contains a block with a specific attribute value.

Method of the class: CartCheckoutUtils{}

No Hooks.

Returns

true|false.

Usage

$result = CartCheckoutUtils::has_block_variation( $block_id, $attribute, $value, $post_content );
$block_id(string) (required)
The block ID to check for.
$attribute(string) (required)
The attribute to check.
$value(string) (required)
The value to check for.
$post_content(string) (required)
The post content to check.

CartCheckoutUtils::has_block_variation() code WC 9.9.3

public static function has_block_variation( $block_id, $attribute, $value, $post_content ) {
	if ( ! $post_content ) {
		return false;
	}

	if ( has_block( $block_id, $post_content ) ) {
		$blocks = (array) parse_blocks( $post_content );

		foreach ( $blocks as $block ) {
			$block_name = $block['blockName'] ?? '';

			if ( $block_name !== $block_id ) {
				continue;
			}

			if ( isset( $block['attrs'][ $attribute ] ) && $value === $block['attrs'][ $attribute ] ) {
				return true;
			}

			// `Cart` is default for `woocommerce/classic-shortcode` so it will be empty in the block attributes.
			if ( 'woocommerce/classic-shortcode' === $block_id && 'shortcode' === $attribute && 'cart' === $value && ! isset( $block['attrs']['shortcode'] ) ) {
				return true;
			}
		}
	}

	return false;
}