Provide product filter blocks a method to know when to display. (https://github.com/woocommerce/woocommerce-blocks/pull/6270)

* Add has_filterable_products data to the AllProducts block to be used by product filter blocks

* Add has_filterable_products data to the ClassicTemplate block to be used by product filter blocks

* Add ClassicTemplatesCompatibility class which will add necessary data to the front-end of core PHP templates of Classic Themes for filter blocks being used as widgets.

* Update comment of set_php_template_data method on ClassicTemplatesCompatibility class

* Move conditional checking if the current theme is a block theme in ClassicTemplatesCompatibility class
This commit is contained in:
Tom Cafferkey 2022-04-19 08:14:09 +01:00 committed by GitHub
parent 3021cc8de6
commit 0d9348f618
4 changed files with 89 additions and 6 deletions

View File

@ -21,6 +21,9 @@ class AllProducts extends AbstractBlock {
*/
protected function enqueue_data( array $attributes = [] ) {
parent::enqueue_data( $attributes );
// Set this so filter blocks being used as widgets know when to render.
$this->asset_data_registry->add( 'has_filterable_products', true, null );
$this->asset_data_registry->add( 'min_columns', wc_get_theme_support( 'product_blocks::min_columns', 1 ), true );
$this->asset_data_registry->add( 'max_columns', wc_get_theme_support( 'product_blocks::max_columns', 6 ), true );
$this->asset_data_registry->add( 'default_columns', wc_get_theme_support( 'product_blocks::default_columns', 3 ), true );

View File

@ -61,12 +61,11 @@ class ClassicTemplate extends AbstractDynamicBlock {
if ( 'single-product' === $attributes['template'] ) {
return $this->render_single_product();
} elseif ( in_array( $attributes['template'], $archive_templates, true ) ) {
// We need to set this so that our product filters can detect if it's a PHP template.
$this->asset_data_registry->add(
'is_rendering_php_template',
true,
null
);
// Set this so that our product filters can detect if it's a PHP template.
$this->asset_data_registry->add( 'is_rendering_php_template', true, null );
// Set this so filter blocks being used as widgets know when to render.
$this->asset_data_registry->add( 'has_filterable_products', true, null );
$this->asset_data_registry->add(
'page_url',

View File

@ -13,6 +13,7 @@ use Automattic\WooCommerce\Blocks\Domain\Services\GoogleAnalytics;
use Automattic\WooCommerce\Blocks\InboxNotifications;
use Automattic\WooCommerce\Blocks\Installer;
use Automattic\WooCommerce\Blocks\Templates\ProductSearchResultsTemplate;
use Automattic\WooCommerce\Blocks\Templates\ClassicTemplatesCompatibility;
use Automattic\WooCommerce\Blocks\Payments\Api as PaymentsApi;
use Automattic\WooCommerce\Blocks\Payments\Integrations\BankTransfer;
use Automattic\WooCommerce\Blocks\Payments\Integrations\CashOnDelivery;
@ -97,6 +98,7 @@ class Bootstrap {
$this->container->get( BlockTypesController::class );
$this->container->get( BlockTemplatesController::class );
$this->container->get( ProductSearchResultsTemplate::class );
$this->container->get( ClassicTemplatesCompatibility::class );
if ( $this->package->feature()->is_feature_plugin_build() ) {
$this->container->get( PaymentsApi::class );
}
@ -229,6 +231,13 @@ class Bootstrap {
return new ProductSearchResultsTemplate();
}
);
$this->container->register(
ClassicTemplatesCompatibility::class,
function ( Container $container ) {
$asset_data_registry = $container->get( AssetDataRegistry::class );
return new ClassicTemplatesCompatibility( $asset_data_registry );
}
);
$this->container->register(
DraftOrders::class,
function( Container $container ) {

View File

@ -0,0 +1,72 @@
<?php
namespace Automattic\WooCommerce\Blocks\Templates;
use Automattic\WooCommerce\Blocks\Assets\AssetDataRegistry;
/**
* ClassicTemplatesCompatibility class.
*
* To bridge the gap on compatibility with widget blocks and classic PHP core templates.
*
* @internal
*/
class ClassicTemplatesCompatibility {
/**
* Instance of the asset data registry.
*
* @var AssetDataRegistry
*/
protected $asset_data_registry;
/**
* Constructor.
*
* @param AssetDataRegistry $asset_data_registry Instance of the asset data registry.
*/
public function __construct( AssetDataRegistry $asset_data_registry ) {
$this->asset_data_registry = $asset_data_registry;
$this->init();
}
/**
* Initialization method.
*/
protected function init() {
if ( ! wc_current_theme_is_fse_theme() ) {
add_action( 'template_redirect', array( $this, 'set_classic_template_data' ) );
}
}
/**
* Executes the methods which set the necessary data needed for filter blocks to work correctly as widgets in Classic templates.
*
* @return void
*/
public function set_classic_template_data() {
$this->set_filterable_product_data();
$this->set_php_template_data();
}
/**
* This method passes the value `has_filterable_products` to the front-end for product archive pages,
* so that widget product filter blocks are aware of the context they are in and can render accordingly.
*
* @return void
*/
public function set_filterable_product_data() {
if ( is_shop() || is_product_taxonomy() ) {
$this->asset_data_registry->add( 'has_filterable_products', true, null );
}
}
/**
* This method passes the value `is_rendering_php_template` to the front-end of Classic themes,
* so that widget product filter blocks are aware of how to filter the products.
*
* @return void
*/
public function set_php_template_data() {
$this->asset_data_registry->add( 'is_rendering_php_template', true, null );
}
}