Automattic\WooCommerce\Blocks\BlockTypes
SingleProduct::replace_post_for_single_product_inner_block
Use the resolved product context for core blocks that read the global post.
Method of the class: SingleProduct{}
No Hooks.
Returns
null. Nothing (null).
Usage
// protected - for code of main (parent) or child class $result = $this->replace_post_for_single_product_inner_block( $block, $context );
- $block(array) (required)
- Block attributes.
- $context(array) (required) (passed by reference — &)
- Block context.
SingleProduct::replace_post_for_single_product_inner_block() SingleProduct::replace post for single product inner block code WC 11.3.0
protected function replace_post_for_single_product_inner_block( $block, &$context ) {
if ( ! in_array( $block['blockName'], array( 'core/post-title', 'core/post-excerpt' ), true ) ) {
return;
}
global $post;
if ( empty( $context['postId'] ) ) {
return;
}
$product_post = get_post( $context['postId'] );
if ( ! $product_post instanceof \WP_Post || 'product' !== $product_post->post_type ) {
return;
}
// If $post already holds $product_post (this might happen in Featured Product),
// do nothing.
if ( $post instanceof \WP_Post && (int) $post->ID === (int) $product_post->ID ) {
return;
}
$this->previous_post = $post;
// phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- Core Post Title reads get_the_title() without a post ID.
$post = $product_post;
}