Skip to content

Commit

Permalink
Add isSelectableByUser attribute to product templates (#46394)
Browse files Browse the repository at this point in the history
* Add selectable attribute in product template to allow creation of product templates that do not appear on the "Change product type" UI

* Always use productTemplateId when available, otherwise fallback to standard-product-template

* Rename attribute

* Use editedRecord in hook useEntityRecord

* Revert "Always use productTemplateId when available, otherwise fallback to standard-product-template"

This reverts commit 2960fcd.

* Revert "Use editedRecord in hook useEntityRecord"

This reverts commit c28e005.

* Add changelogs

* Fix unit tests

* Increment changelog
  • Loading branch information
nathanss committed Apr 10, 2024
1 parent 76bbd95 commit 3fb7443
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: add

Use isSelectableByUser product template attribute to show/hide product templates in the list
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,12 @@ export function ProductDetailsSectionDescriptionBlockEdit( {
const [ supportedProductTemplates, unsupportedProductTemplates ] =
productTemplates.reduce< [ ProductTemplate[], ProductTemplate[] ] >(
( [ supported, unsupported ], productTemplate ) => {
if ( productTemplate.layoutTemplateId ) {
supported.push( productTemplate );
} else {
unsupported.push( productTemplate );
if ( productTemplate.isSelectableByUser ) {
if ( productTemplate.layoutTemplateId ) {
supported.push( productTemplate );
} else {
unsupported.push( productTemplate );
}
}
return [ supported, unsupported ];
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ describe( 'useProductTemplate', () => {
icon: 'icon',
order: 1,
layoutTemplateId: 'layout-template-1',
isSelectableByUser: true,
productData: {
type: 'simple',
},
Expand All @@ -32,6 +33,7 @@ describe( 'useProductTemplate', () => {
description: 'Template 2 description',
icon: 'icon',
layoutTemplateId: 'layout-template-2',
isSelectableByUser: true,
order: 2,
productData: {
type: 'grouped',
Expand All @@ -43,6 +45,7 @@ describe( 'useProductTemplate', () => {
description: 'Template 3 description',
icon: 'icon',
layoutTemplateId: 'layout-template-3',
isSelectableByUser: true,
order: 3,
productData: {
type: 'simple',
Expand All @@ -54,6 +57,7 @@ describe( 'useProductTemplate', () => {
description: 'Standard Product Template description',
icon: 'icon',
layoutTemplateId: 'layout-template-4',
isSelectableByUser: true,
order: 4,
productData: {
type: 'simple',
Expand All @@ -65,6 +69,7 @@ describe( 'useProductTemplate', () => {
description: 'Gift CardProduct Template description',
icon: 'icon',
layoutTemplateId: 'layout-template-5',
isSelectableByUser: true,
order: 5,
productData: {
type: 'simple',
Expand Down
1 change: 1 addition & 0 deletions packages/js/product-editor/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export type ProductTemplate = {
icon: string | null;
order: number;
layoutTemplateId: string;
isSelectableByUser: boolean;
productData: Partial< Product >;
};

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: add

Add isSelectableByUser attribute to product templates
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,13 @@ class ProductTemplate {
*/
private $icon = null;

/**
* If the template is directly selectable through the UI.
*
* @var boolean
*/
private $is_selectable_by_user = true;

/**
* ProductTemplate constructor
*
Expand All @@ -86,6 +93,10 @@ public function __construct( array $data ) {
if ( isset( $data['icon'] ) ) {
$this->icon = $data['icon'];
}

if ( isset( $data['is_selectable_by_user'] ) ) {
$this->is_selectable_by_user = $data['is_selectable_by_user'];
}
}

/**
Expand Down Expand Up @@ -180,6 +191,15 @@ public function get_order() {
return $this->order;
}

/**
* Get the selectable attribute.
*
* @return boolean Selectable.
*/
public function get_is_selectable_by_user() {
return $this->is_selectable_by_user;
}

/**
* Set the template order.
*
Expand All @@ -196,13 +216,14 @@ public function set_order( int $order ) {
*/
public function to_json() {
return array(
'id' => $this->get_id(),
'title' => $this->get_title(),
'description' => $this->get_description(),
'icon' => $this->get_icon(),
'order' => $this->get_order(),
'layoutTemplateId' => $this->get_layout_template_id(),
'productData' => $this->get_product_data(),
'id' => $this->get_id(),
'title' => $this->get_title(),
'description' => $this->get_description(),
'icon' => $this->get_icon(),
'order' => $this->get_order(),
'layoutTemplateId' => $this->get_layout_template_id(),
'productData' => $this->get_product_data(),
'isSelectableByUser' => $this->get_is_selectable_by_user(),
);
}
}

0 comments on commit 3fb7443

Please sign in to comment.