Merge pull request #31610 from woocommerce/fix/allow-block-taxonomy-product-templates
Allow Blocks for Custom Product taxonomies
This commit is contained in:
commit
d5f32ee514
|
@ -159,6 +159,7 @@ class WC_Template_Loader {
|
|||
* @since 3.0.0
|
||||
* @since 5.5.0 If a block template with the same name exists, return an
|
||||
* empty string.
|
||||
* @since 6.3.0 It checks custom product taxonomies
|
||||
* @return string
|
||||
*/
|
||||
private static function get_template_loader_default_file() {
|
||||
|
@ -170,14 +171,16 @@ class WC_Template_Loader {
|
|||
} elseif ( is_product_taxonomy() ) {
|
||||
$object = get_queried_object();
|
||||
|
||||
if ( is_tax( 'product_cat' ) || is_tax( 'product_tag' ) ) {
|
||||
if ( self::has_block_template( 'taxonomy-' . $object->taxonomy ) ) {
|
||||
$default_file = '';
|
||||
} else {
|
||||
if ( self::has_block_template( 'taxonomy-' . $object->taxonomy ) ) {
|
||||
$default_file = '';
|
||||
} else {
|
||||
if ( is_tax( 'product_cat' ) || is_tax( 'product_tag' ) ) {
|
||||
$default_file = 'taxonomy-' . $object->taxonomy . '.php';
|
||||
} elseif ( ! self::has_block_template( 'archive-product' ) ) {
|
||||
$default_file = 'archive-product.php';
|
||||
} else {
|
||||
$default_file = '';
|
||||
}
|
||||
} elseif ( ! self::has_block_template( 'archive-product' ) ) {
|
||||
$default_file = 'archive-product.php';
|
||||
}
|
||||
} elseif (
|
||||
( is_post_type_archive( 'product' ) || is_page( wc_get_page_id( 'shop' ) ) ) &&
|
||||
|
|
|
@ -0,0 +1,139 @@
|
|||
<?php
|
||||
/**
|
||||
* Unit tests for the WC_Template_Loader class.
|
||||
*
|
||||
* @package WooCommerce\Tests\WC_Template_Loader.
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* Class WC_Template_Loader
|
||||
* @group WC_Template_Loader
|
||||
*/
|
||||
class WC_Template_Loader_Test extends \WC_Unit_Test_Case {
|
||||
|
||||
public function test_wc_template_loader_loads_default_file_without_blocks() {
|
||||
|
||||
global $wp_taxonomies;
|
||||
|
||||
$this->initialize_template_loader();
|
||||
|
||||
// forcing has_block_template to be false
|
||||
add_filter( 'woocommerce_has_block_template', '__return_false', 10, 2 );
|
||||
|
||||
// Check Single Product
|
||||
$this->load_product_in_query();
|
||||
$this->assertDefaultTemplateFileName( 'single-product' );
|
||||
|
||||
// Check Woo Taxonomy Product
|
||||
$this->load_tax_in_query( 'product_cat' );
|
||||
$this->assertDefaultTemplateFileName( 'taxonomy-product-cat' );
|
||||
|
||||
$this->load_tax_in_query( 'product_tag' );
|
||||
$this->assertDefaultTemplateFileName( 'taxonomy-product-tag' );
|
||||
|
||||
// Check Custom Product Taxonomies
|
||||
$wp_taxonomies['product_tax'] = new WP_Taxonomy( 'product_tax', 'product' );
|
||||
$this->load_tax_in_query( 'product_tax' );
|
||||
$this->assertDefaultTemplateFileName( 'archive-product' );
|
||||
|
||||
// Check shop page
|
||||
$this->load_shop_page();
|
||||
$this->assertDefaultTemplateFileName( 'archive-product' );
|
||||
|
||||
}
|
||||
|
||||
public function test_wc_template_loader_loads_template_with_blocks() {
|
||||
|
||||
global $wp_taxonomies;
|
||||
|
||||
$this->initialize_template_loader();
|
||||
|
||||
// forcing has_block_template to be false
|
||||
add_filter( 'woocommerce_has_block_template', '__return_true', 10, 2 );
|
||||
|
||||
// Check Single Product
|
||||
$this->load_product_in_query();
|
||||
$this->assertDefaultTemplateFileName();
|
||||
|
||||
// Check Woo Taxonomy Product
|
||||
$this->load_tax_in_query( 'product_cat' );
|
||||
$this->assertDefaultTemplateFileName();
|
||||
|
||||
$this->load_tax_in_query( 'product_tag' );
|
||||
$this->assertDefaultTemplateFileName();
|
||||
|
||||
// Check Custom Product Taxonomies
|
||||
$wp_taxonomies['product_tax'] = new WP_Taxonomy( 'product_tax', 'product' );
|
||||
$this->load_tax_in_query( 'product_tax' );
|
||||
$this->assertDefaultTemplateFileName();
|
||||
|
||||
// Check shop page
|
||||
$this->load_shop_page();
|
||||
$this->assertDefaultTemplateFileName();
|
||||
|
||||
}
|
||||
|
||||
|
||||
private function initialize_template_loader() {
|
||||
// be sure shop is always returning same id doesn't matter the test setup environment
|
||||
add_filter( 'woocommerce_get_shop_page_id', function ( $page ) {
|
||||
return 5;
|
||||
}, 10, 1 );
|
||||
|
||||
if ( ! function_exists( 'wp_is_block_theme' ) ) {
|
||||
function wp_is_block_theme() {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
WC_Template_Loader::init();
|
||||
}
|
||||
|
||||
private function load_product_in_query() {
|
||||
global $wp_query;
|
||||
$wp_query->is_tax = false;
|
||||
$wp_query->is_singular = true;
|
||||
$wp_query->is_page = false;
|
||||
$wp_query->queried_object = (object) array(
|
||||
'post_type' => 'product',
|
||||
'post_name' => 'test',
|
||||
);
|
||||
}
|
||||
|
||||
private function load_shop_page() {
|
||||
global $wp_query;
|
||||
$wp_query->is_tax = false;
|
||||
$wp_query->is_singular = false;
|
||||
$wp_query->is_page = true;
|
||||
$wp_query->queried_object = (object) array(
|
||||
'post_type' => 'page',
|
||||
'post_name' => 'shop',
|
||||
'post_title' => 'shop',
|
||||
'ID' => 5,
|
||||
);
|
||||
}
|
||||
|
||||
private function load_tax_in_query( $taxonomy ) {
|
||||
global $wp_query;
|
||||
|
||||
$wp_query->is_singular = false;
|
||||
$wp_query->is_tax = true;
|
||||
$wp_query->is_page = false;
|
||||
$wp_query->queried_object = (object) array(
|
||||
'taxonomy' => $taxonomy,
|
||||
'slug' => 'test',
|
||||
);
|
||||
}
|
||||
|
||||
private function assertDefaultTemplateFileName( $expected = '' ) {
|
||||
|
||||
$default_file = WC_Template_Loader::template_loader( 'test' );
|
||||
|
||||
if ( ! $expected ) {
|
||||
$this->assertEquals( 'test', $default_file );
|
||||
} else {
|
||||
$this->assertEquals( WC()->plugin_path() . '/templates/' . $expected . '.php', $default_file );
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue