Automattic\WooCommerce\Blocks\BlockTypes\AddToCartWithOptions
GroupedProductItem::get_product_row
Get product row HTML.
Method of the class: GroupedProductItem{}
No Hooks.
Returns
String. Row HTML
Usage
// private - for code of main (parent) class only $result = $this->get_product_row( $product_id, $attributes, $block ): string;
- $product_id(string) (required)
- Product ID.
- $attributes(array) (required)
- Block attributes.
- $block(WP_Block) (required)
- The Block.
GroupedProductItem::get_product_row() GroupedProductItem::get product row code WC 10.7.0
private function get_product_row( $product_id, $attributes, $block ): string {
global $post, $product;
$previous_post = $post;
$previous_product = $product;
// Since this template uses the core/post-title block to show the product name
// a temporally replacement of the global post is needed. This is reverted back
// to its initial post value that is stored in the $previous_post variable.
$post = get_post( $product_id ); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
$product = wc_get_product( $product_id );
add_filter( 'render_block_context', array( $this, 'set_is_descendant_of_grouped_product_selector_context' ), 10, 2 );
// Render the inner blocks of the Post Template block with `dynamic` set to `false` to prevent calling
// `render_callback` and ensure that no wrapper markup is included.
$block_content = AddToCartWithOptionsUtils::render_block_with_context(
$block,
array(
'postType' => 'product',
'postId' => $post->ID,
),
);
remove_filter( 'render_block_context', array( $this, 'set_is_descendant_of_grouped_product_selector_context' ) );
$post = $previous_post; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
$product = $previous_product;
return $block_content;
}