Automattic\WooCommerce\EmailEditor\Integrations\WooCommerce\Renderer\Blocks

Abstract_Product_Block_Renderer::get_product_from_contextprotectedWC 1.0

Get product from block context.

Method of the class: Abstract_Product_Block_Renderer{}

No Hooks.

Returns

\WC_Product|null.

Usage

// protected - for code of main (parent) or child class
$result = $this->get_product_from_context( $parsed_block ): ?\WC_Product;
$parsed_block(array) (required)
Parsed block.

Abstract_Product_Block_Renderer::get_product_from_context() code WC 10.4.3

protected function get_product_from_context( array $parsed_block ): ?\WC_Product {
	$post_id = $parsed_block['context']['postId'] ?? 0;

	if ( ! $post_id ) {
		global $product;
		if ( $product && is_a( $product, 'WC_Product' ) ) {
			$post_id = $product->get_id();
		}
	}

	if ( ! $post_id ) {
		global $post;
		if ( $post && get_post_type( $post->ID ) === 'product' ) {
			$post_id = $post->ID;
		}
	}

	$product = $post_id ? wc_get_product( $post_id ) : null;
	return $product ? $product : null;
}