Automattic\WooCommerce\Blocks\Templates
SingleProductTemplate::render_block_template
Run template-specific logic when the query matches this template.
Method of the class: SingleProductTemplate{}
No Hooks.
Returns
null. Nothing (null).
Usage
$SingleProductTemplate = new SingleProductTemplate(); $SingleProductTemplate->render_block_template();
SingleProductTemplate::render_block_template() SingleProductTemplate::render block template code WC 10.8.1
public function render_block_template() {
if ( ! is_embed() && is_singular( 'product' ) ) {
global $post;
$compatibility_layer = new SingleProductTemplateCompatibility();
$compatibility_layer->init();
$valid_slugs = array( self::SLUG );
$single_product_slug = 'product' === $post->post_type && $post->post_name ? 'single-product-' . $post->post_name : '';
if ( $single_product_slug ) {
$valid_slugs[] = 'single-product-' . $post->post_name;
}
$templates = get_block_templates( array( 'slug__in' => $valid_slugs ) );
if ( count( $templates ) === 0 ) {
return;
}
// Use the first template by default.
$template = reset( $templates );
// Check if there is a template matching the slug `single-product-{post_name}`.
if ( count( $valid_slugs ) > 1 && count( $templates ) > 1 ) {
foreach ( $templates as $t ) {
if ( $single_product_slug === $t->slug ) {
$template = $t;
break;
}
}
}
if ( isset( $template ) && BlockTemplateUtils::template_has_legacy_template_block( $template ) ) {
add_filter( 'woocommerce_disable_compatibility_layer', '__return_true' );
}
$product = wc_get_product( $post->ID );
if ( $product ) {
$consent = 'I acknowledge that using experimental APIs means my theme or plugin will inevitably break in the next version of WooCommerce';
// Load the product data into the products store so derived
// state closures can resolve it during server-side rendering.
ProductsStore::load_product( $consent, $product->get_id() );
// Set the current product context. The derived state
// closures (mainProductInContext, productVariationInContext, productInContext)
// are registered by ProductsStore::register_state().
wp_interactivity_state(
'woocommerce/products',
array(
'productId' => $product->get_id(),
'variationId' => null,
)
);
}
}
}