Redirect supported product types to the new experience based on the product template associated to it (#43341)

* Redirect supported product types to the new experience based on the product template associated to it

* Add changelog file

* Fix linter error

* Redirect using the product type first and then the associated product template

* Set the product template id also for unsupported product templates

* Add changelog file

* Remove supported_product_types from the RedirectionController since the product_templates is used instead

* Fix php linter error
This commit is contained in:
Maikel Perez 2024-01-09 18:31:25 -03:00 committed by GitHub
parent 548987d758
commit 1ad48760c9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 98 additions and 35 deletions

View File

@ -0,0 +1,4 @@
Significance: minor
Type: add
Set the product template id also for unsupported product templates

View File

@ -203,7 +203,10 @@ export function ProductDetailsSectionDescriptionBlockEdit( {
try {
if ( isSaving ) return;
await validate( unsupportedProductTemplate?.productData );
const { id: productTemplateId, productData } =
unsupportedProductTemplate as ProductTemplate;
await validate( productData );
const product = ( await saveEditedEntityRecord< Product >(
'postType',
@ -214,6 +217,8 @@ export function ProductDetailsSectionDescriptionBlockEdit( {
}
) ) ?? { id: productId };
const productMetaData = productData?.meta_data ?? [];
// Avoiding to save some changes that are not supported by the current product template.
// So in this case those changes are saved directly to the server.
await saveEntityRecord(
@ -221,7 +226,14 @@ export function ProductDetailsSectionDescriptionBlockEdit( {
'product',
{
...product,
...unsupportedProductTemplate?.productData,
...productData,
meta_data: [
...productMetaData,
{
key: '_product_template_id',
value: productTemplateId,
},
],
},
// @ts-expect-error Expected 3 arguments, but got 4.
{

View File

@ -0,0 +1,4 @@
Significance: minor
Type: add
Redirect supported product types to the new experience based on the product template associated to it

View File

@ -32,6 +32,13 @@ class Init {
*/
private $supported_product_types = array( 'simple' );
/**
* Registered product templates.
*
* @var array
*/
private $product_templates = array();
/**
* Redirection controller.
*
@ -55,7 +62,7 @@ class Init {
array_push( $this->supported_product_types, 'grouped' );
}
$this->redirection_controller = new RedirectionController( $this->supported_product_types );
$this->redirection_controller = new RedirectionController();
if ( \Automattic\WooCommerce\Utilities\FeaturesUtil::feature_is_enabled( 'product_block_editor' ) ) {
if ( ! Features::is_enabled( 'new-product-management-experience' ) ) {
@ -69,7 +76,7 @@ class Init {
add_action( 'current_screen', array( $this, 'set_current_screen_to_block_editor_if_wc_admin' ) );
add_action( 'rest_api_init', array( $this, 'register_product_editor_templates' ) );
add_action( 'rest_api_init', array( $this, 'register_layout_templates' ) );
// Make sure the block registry is initialized so that core blocks are registered.
BlockRegistry::get_instance();
@ -79,6 +86,9 @@ class Init {
// Make sure the block template logger is initialized before any templates are created.
BlockTemplateLogger::get_instance();
$this->register_layout_templates();
$this->register_product_templates();
}
}
@ -90,7 +100,6 @@ class Init {
return;
}
$this->register_product_editor_templates();
$editor_settings = $this->get_product_editor_settings();
$script_handle = 'wc-admin-edit-product';
@ -227,26 +236,11 @@ class Init {
$editor_settings['layoutTemplateEvents'][] = $layout_template_logger->get_formatted_template_events( $layout_template->get_id() );
}
/**
* Allows for new product template registration.
*
* @since 8.5.0
*/
$product_templates = apply_filters( 'woocommerce_product_editor_product_templates', $this->get_default_product_templates() );
$product_templates = $this->create_default_product_template_by_custom_product_type( $product_templates );
usort(
$product_templates,
function ( $a, $b ) {
return $a->get_order() - $b->get_order();
}
);
$editor_settings['productTemplates'] = array_map(
function ( $product_template ) {
return $product_template->to_json();
},
$product_templates
$this->product_templates
);
$block_editor_context = new WP_Block_Editor_Context( array( 'name' => self::EDITOR_CONTEXT_NAME ) );
@ -372,9 +366,9 @@ class Init {
}
/**
* Register product editor templates.
* Register layout templates.
*/
public function register_product_editor_templates() {
public function register_layout_templates() {
$layout_template_registry = wc_get_container()->get( LayoutTemplateRegistry::class );
if ( ! $layout_template_registry->is_registered( 'simple-product' ) ) {
@ -393,4 +387,26 @@ class Init {
);
}
}
/**
* Register product templates.
*/
public function register_product_templates() {
/**
* Allows for new product template registration.
*
* @since 8.5.0
*/
$this->product_templates = apply_filters( 'woocommerce_product_editor_product_templates', $this->get_default_product_templates() );
$this->product_templates = $this->create_default_product_template_by_custom_product_type( $this->product_templates );
usort(
$this->product_templates,
function ( $a, $b ) {
return $a->get_order() - $b->get_order();
}
);
$this->redirection_controller->set_product_templates( $this->product_templates );
}
}

View File

@ -12,22 +12,17 @@ use Automattic\WooCommerce\Internal\Admin\WCAdminAssets;
* Handle redirecting to the old or new editor based on features and support.
*/
class RedirectionController {
/**
* Supported product types.
* Registered product templates.
*
* @var array
*/
private $supported_product_types;
private $product_templates = array();
/**
* Set up the hooks used for redirection.
*
* @param array $supported_product_types Array of supported product types.
*/
public function __construct( $supported_product_types ) {
$this->supported_product_types = $supported_product_types;
public function __construct() {
if ( \Automattic\WooCommerce\Utilities\FeaturesUtil::feature_is_enabled( 'product_block_editor' ) ) {
add_action( 'current_screen', array( $this, 'maybe_redirect_to_new_editor' ), 30, 0 );
add_action( 'current_screen', array( $this, 'redirect_non_supported_product_types' ), 30, 0 );
@ -63,18 +58,50 @@ class RedirectionController {
*/
protected function is_product_supported( $product_id ): bool {
$product = $product_id ? wc_get_product( $product_id ) : null;
$digital_product = $product->is_downloadable() || $product->is_virtual();
if ( $product && in_array( $product->get_type(), $this->supported_product_types, true ) ) {
if ( is_null( $product ) ) {
return false;
}
$digital_product = $product->is_downloadable() || $product->is_virtual();
$product_template_id = $product->get_meta( '_product_template_id' );
foreach ( $this->product_templates as $product_template ) {
if ( is_null( $product_template->get_layout_template_id() ) ) {
continue;
}
$product_data = $product_template->get_product_data();
$product_type = $product_data['type'];
if ( isset( $product_type ) && $product_type !== $product->get_type() ) {
continue;
}
if ( isset( $product_template_id ) && $product_template_id === $product_template->get_id() ) {
return true;
}
if ( isset( $product_type ) ) {
if ( Features::is_enabled( 'product-virtual-downloadable' ) ) {
return true;
}
return ! $digital_product;
}
}
return false;
}
/**
* Check if a product is supported by the new experience.
*
* @param array $product_templates The registered product teamplates.
*/
public function set_product_templates( array $product_templates ): void {
$this->product_templates = $product_templates;
}
/**
* Redirects from old product form to the new product form if the
* feature `product_block_editor` is enabled.