diff --git a/plugins/woocommerce/changelog/fix-49978-default-product-attribute-pattern b/plugins/woocommerce/changelog/fix-49978-default-product-attribute-pattern new file mode 100644 index 00000000000..127d4cc6878 --- /dev/null +++ b/plugins/woocommerce/changelog/fix-49978-default-product-attribute-pattern @@ -0,0 +1,5 @@ +Significance: patch +Type: add +Comment: Add default product attribute pattern to dynamically show the default attribute in the Product Filters template part. + + diff --git a/plugins/woocommerce/src/Blocks/BlockTypes/ProductFilterAttribute.php b/plugins/woocommerce/src/Blocks/BlockTypes/ProductFilterAttribute.php index 352c708f8ea..d997799c772 100644 --- a/plugins/woocommerce/src/Blocks/BlockTypes/ProductFilterAttribute.php +++ b/plugins/woocommerce/src/Blocks/BlockTypes/ProductFilterAttribute.php @@ -31,6 +31,7 @@ final class ProductFilterAttribute extends AbstractBlock { add_filter( 'collection_filter_query_param_keys', array( $this, 'get_filter_query_param_keys' ), 10, 2 ); add_filter( 'collection_active_filters_data', array( $this, 'register_active_filters_data' ), 10, 2 ); add_action( 'deleted_transient', array( $this, 'delete_default_attribute_id_transient' ) ); + add_action( 'wp_loaded', array( $this, 'register_block_patterns' ) ); } /** @@ -44,7 +45,7 @@ final class ProductFilterAttribute extends AbstractBlock { parent::enqueue_data( $attributes ); if ( is_admin() ) { - $this->asset_data_registry->add( 'defaultProductFilterAttribute', $this->get_default_attribute() ); + $this->asset_data_registry->add( 'defaultProductFilterAttribute', $this->get_default_product_attribute() ); } } @@ -161,6 +162,11 @@ final class ProductFilterAttribute extends AbstractBlock { * @return string Rendered block type output. */ protected function render( $attributes, $content, $block ) { + if ( empty( $attributes['attributeId'] ) ) { + $default_product_attribute = $this->get_default_product_attribute(); + $attributes['attributeId'] = $default_product_attribute->attribute_id; + } + // don't render if its admin, or ajax in progress. if ( is_admin() || wp_doing_ajax() || empty( $attributes['attributeId'] ) ) { return ''; @@ -361,9 +367,17 @@ final class ProductFilterAttribute extends AbstractBlock { /** * Get the attribute if with most term but closest to 30 terms. * - * @return int + * @return object */ - private function get_default_attribute() { + private function get_default_product_attribute() { + // Cache this variable in memory to prevent repeated database queries to check + // for transient in the same request. + static $cached = null; + + if ( $cached ) { + return $cached; + } + $cached = get_transient( 'wc_block_product_filter_attribute_default_attribute' ); if ( $cached ) { @@ -403,9 +417,13 @@ final class ProductFilterAttribute extends AbstractBlock { } } - $default_attribute = array( - 'id' => 0, - 'label' => __( 'Attribute', 'woocommerce' ), + $default_attribute = (object) array( + 'attribute_id' => '0', + 'attribute_name' => 'attribute', + 'attribute_label' => __( 'Attribute', 'woocommerce' ), + 'attribute_type' => 'select', + 'attribute_orderby' => 'menu_order', + 'attribute_public' => 0, ); if ( $attribute_id ) { @@ -416,4 +434,52 @@ final class ProductFilterAttribute extends AbstractBlock { return $default_attribute; } + + /** + * Register pattern for default product attribute. + */ + public function register_block_patterns() { + $default_attribute = $this->get_default_product_attribute(); + register_block_pattern( + 'woocommerce/default-attribute-filter', + array( + 'title' => '', + 'inserter' => false, + 'content' => strtr( + ' + + +
+ +

{{attribute_label}}

+ + + + +
+ +
+ Clear +
+ +
+ + +
+ + + + + ', + array( + '{{attribute_id}}' => intval( $default_attribute->attribute_id ), + '{{attribute_label}}' => esc_html( $default_attribute->attribute_label ), + ) + ), + ) + ); + } } diff --git a/plugins/woocommerce/templates/parts/product-filters.html b/plugins/woocommerce/templates/parts/product-filters.html index d2caafbe412..0b2e5cae89f 100644 --- a/plugins/woocommerce/templates/parts/product-filters.html +++ b/plugins/woocommerce/templates/parts/product-filters.html @@ -47,23 +47,7 @@ - - -
-

Attribute

- - - - -
- -
- -
- - - - +