AbstractPageTemplate.php
1 year ago
AbstractTemplate.php
10 months ago
AbstractTemplateCompatibility.php
1 year ago
AbstractTemplatePart.php
2 years ago
AbstractTemplateWithFallback.php
7 months ago
ArchiveProductTemplatesCompatibility.php
1 year ago
CartTemplate.php
10 months ago
CheckoutHeaderTemplate.php
2 years ago
CheckoutTemplate.php
10 months ago
ClassicTemplatesCompatibility.php
1 year ago
ComingSoonSocialLinksTemplate.php
1 year ago
ComingSoonTemplate.php
1 year ago
ExternalProductAddToCartWithOptionsTemplate.php
1 year ago
GroupedProductAddToCartWithOptionsTemplate.php
1 year ago
MiniCartTemplate.php
2 years ago
OrderConfirmationTemplate.php
1 year ago
ProductAttributeTemplate.php
7 months ago
ProductBrandTemplate.php
7 months ago
ProductCatalogTemplate.php
7 months ago
ProductCategoryTemplate.php
7 months ago
ProductSearchResultsTemplate.php
7 months ago
ProductTagTemplate.php
7 months ago
SimpleProductAddToCartWithOptionsTemplate.php
1 year ago
SingleProductTemplate.php
2 months ago
SingleProductTemplateCompatibility.php
10 months ago
VariableProductAddToCartWithOptionsTemplate.php
1 year ago
ProductAttributeTemplate.php
96 lines
| 1 | <?php |
| 2 | declare( strict_types=1 ); |
| 3 | namespace Automattic\WooCommerce\Blocks\Templates; |
| 4 | |
| 5 | use Automattic\WooCommerce\Blocks\Templates\ArchiveProductTemplatesCompatibility; |
| 6 | use Automattic\WooCommerce\Blocks\Utils\BlockTemplateUtils; |
| 7 | |
| 8 | /** |
| 9 | * ProductAttributeTemplate class. |
| 10 | * |
| 11 | * @internal |
| 12 | */ |
| 13 | class ProductAttributeTemplate extends AbstractTemplateWithFallback { |
| 14 | |
| 15 | /** |
| 16 | * The slug of the template. |
| 17 | * |
| 18 | * @var string |
| 19 | */ |
| 20 | const SLUG = 'taxonomy-product_attribute'; |
| 21 | |
| 22 | /** |
| 23 | * The template used as a fallback if that one is customized. |
| 24 | * |
| 25 | * @var string |
| 26 | */ |
| 27 | public string $fallback_template = ProductCatalogTemplate::SLUG; |
| 28 | |
| 29 | /** |
| 30 | * Returns the title of the template. |
| 31 | * |
| 32 | * @return string |
| 33 | */ |
| 34 | public function get_template_title() { |
| 35 | return _x( 'Products by Attribute', 'Template name', 'woocommerce' ); |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * Returns the description of the template. |
| 40 | * |
| 41 | * @return string |
| 42 | */ |
| 43 | public function get_template_description() { |
| 44 | return __( 'Displays products filtered by an attribute.', 'woocommerce' ); |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Run template-specific logic when the query matches this template. |
| 49 | */ |
| 50 | public function render_block_template() { |
| 51 | $queried_object = get_queried_object(); |
| 52 | if ( is_null( $queried_object ) ) { |
| 53 | return; |
| 54 | } |
| 55 | |
| 56 | if ( isset( $queried_object->taxonomy ) && taxonomy_is_product_attribute( $queried_object->taxonomy ) ) { |
| 57 | $compatibility_layer = new ArchiveProductTemplatesCompatibility(); |
| 58 | $compatibility_layer->init(); |
| 59 | |
| 60 | $templates = get_block_templates( array( 'slug__in' => array( self::SLUG ) ) ); |
| 61 | |
| 62 | if ( isset( $templates[0] ) && BlockTemplateUtils::template_has_legacy_template_block( $templates[0] ) ) { |
| 63 | add_filter( 'woocommerce_disable_compatibility_layer', '__return_true' ); |
| 64 | } |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Renders the Product by Attribute template for product attributes taxonomy pages. |
| 70 | * |
| 71 | * @param array $templates Templates that match the product attributes taxonomy. |
| 72 | */ |
| 73 | public function template_hierarchy( $templates ) { |
| 74 | $queried_object = get_queried_object(); |
| 75 | |
| 76 | if ( ! is_null( $queried_object ) && taxonomy_is_product_attribute( $queried_object->taxonomy ) && wp_is_block_theme() ) { |
| 77 | // If Products by Attribute template has been customized or it's in the |
| 78 | // theme, we load it first, otherwise we only load the fallback template. |
| 79 | // If we don't do that, the WC core template would always have priority |
| 80 | // over the fallback template. |
| 81 | $slugs = array( $this->fallback_template ); |
| 82 | |
| 83 | if ( |
| 84 | BlockTemplateUtils::theme_has_template( self::SLUG ) || |
| 85 | BlockTemplateUtils::get_block_templates_from_db( array( self::SLUG ) ) |
| 86 | ) { |
| 87 | $slugs = array( self::SLUG, $this->fallback_template ); |
| 88 | } |
| 89 | |
| 90 | array_splice( $templates, count( $templates ) - 1, 0, $slugs ); |
| 91 | } |
| 92 | |
| 93 | return $templates; |
| 94 | } |
| 95 | } |
| 96 |