From b446a370ef5a50748b888f7246b24404a73e79bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alba=20Rinc=C3=B3n?= Date: Thu, 28 Jul 2022 12:49:13 +0200 Subject: [PATCH] Use the archive-product template to render product attributes pages (#6776) --- src/BlockTemplatesController.php | 5 +++ src/Domain/Bootstrap.php | 8 +++++ src/Templates/ProductAttributeTemplate.php | 39 ++++++++++++++++++++++ 3 files changed, 52 insertions(+) create mode 100644 src/Templates/ProductAttributeTemplate.php diff --git a/src/BlockTemplatesController.php b/src/BlockTemplatesController.php index 38a10c96b49..16a02208b04 100644 --- a/src/BlockTemplatesController.php +++ b/src/BlockTemplatesController.php @@ -425,6 +425,11 @@ public function render_block_template() { $this->block_template_is_available( 'taxonomy-product_tag' ) ) { add_filter( 'woocommerce_has_block_template', '__return_true', 10, 0 ); + } elseif ( taxonomy_is_product_attribute( get_query_var( 'taxonomy' ) ) && + ! BlockTemplateUtils::theme_has_template( 'archive-product' ) && + $this->block_template_is_available( 'archive-product' ) + ) { + add_filter( 'woocommerce_has_block_template', '__return_true', 10, 0 ); } elseif ( ( is_post_type_archive( 'product' ) || is_page( wc_get_page_id( 'shop' ) ) ) && ! BlockTemplateUtils::theme_has_template( 'archive-product' ) && diff --git a/src/Domain/Bootstrap.php b/src/Domain/Bootstrap.php index 7dd7f3b8074..186c18c52cc 100644 --- a/src/Domain/Bootstrap.php +++ b/src/Domain/Bootstrap.php @@ -22,6 +22,7 @@ use Automattic\WooCommerce\Blocks\Payments\Integrations\PayPal; use Automattic\WooCommerce\Blocks\Payments\PaymentMethodRegistry; use Automattic\WooCommerce\Blocks\Registry\Container; +use Automattic\WooCommerce\Blocks\Templates\ProductAttributeTemplate; use Automattic\WooCommerce\StoreApi\StoreApi; use Automattic\WooCommerce\StoreApi\RoutesController; use Automattic\WooCommerce\StoreApi\SchemaController; @@ -116,6 +117,7 @@ function() { $this->container->get( BlockTypesController::class ); $this->container->get( BlockTemplatesController::class ); $this->container->get( ProductSearchResultsTemplate::class ); + $this->container->get( ProductAttributeTemplate::class ); $this->container->get( ClassicTemplatesCompatibility::class ); if ( $this->package->feature()->is_feature_plugin_build() ) { $this->container->get( PaymentsApi::class ); @@ -249,6 +251,12 @@ function () { return new ProductSearchResultsTemplate(); } ); + $this->container->register( + ProductAttributeTemplate::class, + function () { + return new ProductAttributeTemplate(); + } + ); $this->container->register( ClassicTemplatesCompatibility::class, function ( Container $container ) { diff --git a/src/Templates/ProductAttributeTemplate.php b/src/Templates/ProductAttributeTemplate.php new file mode 100644 index 00000000000..62dc2defb95 --- /dev/null +++ b/src/Templates/ProductAttributeTemplate.php @@ -0,0 +1,39 @@ +init(); + } + + /** + * Initialization method. + */ + protected function init() { + add_filter( 'taxonomy_template_hierarchy', array( $this, 'update_taxonomy_template_hierarchy' ), 10, 3 ); + } + + /** + * Render the Archive Product Template for product attributes. + * + * @param array $templates Templates that match the product attributes taxonomy. + */ + public function update_taxonomy_template_hierarchy( $templates ) { + if ( taxonomy_is_product_attribute( get_query_var( 'taxonomy' ) ) && wc_current_theme_is_fse_theme() ) { + array_unshift( $templates, self::SLUG ); + } + + return $templates; + } +}