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
ProductSearchResultsTemplate.php
75 lines
| 1 | <?php |
| 2 | namespace Automattic\WooCommerce\Blocks\Templates; |
| 3 | |
| 4 | use Automattic\WooCommerce\Blocks\Templates\ArchiveProductTemplatesCompatibility; |
| 5 | use Automattic\WooCommerce\Blocks\Utils\BlockTemplateUtils; |
| 6 | |
| 7 | /** |
| 8 | * ProductSearchResultsTemplate class. |
| 9 | * |
| 10 | * @internal |
| 11 | */ |
| 12 | class ProductSearchResultsTemplate extends AbstractTemplate { |
| 13 | |
| 14 | /** |
| 15 | * The slug of the template. |
| 16 | * |
| 17 | * @var string |
| 18 | */ |
| 19 | const SLUG = 'product-search-results'; |
| 20 | |
| 21 | /** |
| 22 | * Initialization method. |
| 23 | */ |
| 24 | public function init() { |
| 25 | add_action( 'template_redirect', array( $this, 'render_block_template' ) ); |
| 26 | add_filter( 'search_template_hierarchy', array( $this, 'update_search_template_hierarchy' ), 10, 3 ); |
| 27 | } |
| 28 | |
| 29 | /** |
| 30 | * Returns the title of the template. |
| 31 | * |
| 32 | * @return string |
| 33 | */ |
| 34 | public function get_template_title() { |
| 35 | return _x( 'Product Search Results', '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 search results for your store.', 'woocommerce' ); |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Run template-specific logic when the query matches this template. |
| 49 | */ |
| 50 | public function render_block_template() { |
| 51 | if ( ! is_embed() && is_post_type_archive( 'product' ) && is_search() ) { |
| 52 | $compatibility_layer = new ArchiveProductTemplatesCompatibility(); |
| 53 | $compatibility_layer->init(); |
| 54 | |
| 55 | $templates = get_block_templates( array( 'slug__in' => array( self::SLUG ) ) ); |
| 56 | |
| 57 | if ( isset( $templates[0] ) && BlockTemplateUtils::template_has_legacy_template_block( $templates[0] ) ) { |
| 58 | add_filter( 'woocommerce_disable_compatibility_layer', '__return_true' ); |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * When the search is for products and a block theme is active, render the Product Search Template. |
| 65 | * |
| 66 | * @param array $templates Templates that match the search hierarchy. |
| 67 | */ |
| 68 | public function update_search_template_hierarchy( $templates ) { |
| 69 | if ( ( is_search() && is_post_type_archive( 'product' ) ) && wp_is_block_theme() ) { |
| 70 | array_unshift( $templates, self::SLUG ); |
| 71 | } |
| 72 | return $templates; |
| 73 | } |
| 74 | } |
| 75 |