Automattic\WooCommerce\Blocks\BlockTypes
SingleProduct::replace_post_for_single_product_inner_block()
Replace the global post for the Single Product inner blocks and reset it after.
This is needed because some of the inner blocks may use the global post instead of fetching the product through the productId attribute, so even if the productId is passed to the inner block, it will still use the global post.
Метод класса: SingleProduct{}
Хуков нет.
Возвращает
null
. Ничего (null).
Использование
// protected - в коде основоного (родительского) или дочернего класса $result = $this->replace_post_for_single_product_inner_block( $block, $context );
- $block(массив) (обязательный)
- Block attributes.
- $context(массив) (обязательный) (передается по ссылке — &)
- Block context.
Код SingleProduct::replace_post_for_single_product_inner_block() SingleProduct::replace post for single product inner block WC 9.4.2
protected function replace_post_for_single_product_inner_block( $block, &$context ) { if ( $this->single_product_inner_blocks_names ) { $block_name = array_pop( $this->single_product_inner_blocks_names ); if ( $block_name === $block['blockName'] ) { /** * This is a temporary fix to ensure the Post Title and Excerpt blocks work as expected * until Gutenberg versions 15.2 and 15.6 are included in the core of WordPress. * * Important: the original post data is restored in the restore_global_post method. * * @see https://github.com/WordPress/gutenberg/pull/48001 * @see https://github.com/WordPress/gutenberg/pull/49495 */ if ( 'core/post-excerpt' === $block_name || 'core/post-title' === $block_name ) { global $post; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited $post = get_post( $this->product_id ); if ( $post instanceof \WP_Post ) { setup_postdata( $post ); } } $context['postId'] = $this->product_id; $context['singleProduct'] = true; } } }