After Variations are created, disable the fields that don’t do anything (#45838)

* Disable woocommerce/product-regular-price-field when product type is variable

* Disable woocommerce/product-sale-price-field when product type is variable

* Disable woocommerce/product-sku-field when product type is variable

* Disable Track stock quantity toggle when product type is variable

* Disable woocommerce/product-radio-field when product type is variable

* Disable woocommerce/product-shipping-class-field when product type is variable

* Disable woocommerce/product-shipping-dimensions-fields when product type is variable

* Add changelog files

* Fix php linter
This commit is contained in:
Maikel Perez 2024-03-22 13:52:42 -03:00 committed by GitHub
parent 27124828e9
commit 54476cb7f3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
23 changed files with 129 additions and 46 deletions

View File

@ -0,0 +1,4 @@
Significance: minor
Type: add
Disable the fields that are not required in variable products because they are set in each variation

View File

@ -8,24 +8,28 @@ Radio button field for the product editor.
### title
- **Type:** `String`
- **Required:** `Yes`
- **Type:** `String`
- **Required:** `Yes`
### description
- **Type:** `String`
- **Required:** `No`
- **Type:** `String`
- **Required:** `No`
### property
- **Type:** `String`
- **Required:** `Yes`
- **Type:** `String`
- **Required:** `Yes`
### options
- **Type:** `Array`
- **Required:** `Yes`
- **Type:** `Array`
- **Required:** `Yes`
### disabled
- **Type:** `Boolean`
- **Required:** `No`
## Usage

View File

@ -24,6 +24,10 @@
},
"default": [],
"__experimentalRole": "content"
},
"disabled": {
"type": "boolean",
"default": false
}
},
"supports": {

View File

@ -17,7 +17,7 @@ export function Edit( {
context: { postType },
}: ProductEditorBlockEditProps< RadioBlockAttributes > ) {
const blockProps = useWooBlockProps( attributes );
const { description, options, property, title } = attributes;
const { description, options, property, title, disabled } = attributes;
const [ value, setValue ] = useProductEntityProp< string >( property, {
postType,
fallbackValue: '',
@ -31,6 +31,7 @@ export function Edit( {
selected={ value }
options={ options }
onChange={ ( selected ) => setValue( selected || '' ) }
disabled={ disabled }
/>
</div>
);

View File

@ -8,4 +8,5 @@ export interface RadioBlockAttributes extends BlockAttributes {
description: string;
property: string;
options: [];
disabled?: boolean;
}

View File

@ -11,6 +11,10 @@
"name": {
"type": "string",
"__experimentalRole": "content"
},
"disabled": {
"type": "boolean",
"default": false
}
},
"supports": {

View File

@ -57,6 +57,7 @@ export function Edit( {
name={ 'woocommerce-product-sku' }
onChange={ setSku }
value={ sku || '' }
disabled={ attributes.disabled }
/>
</BaseControl>
</div>

View File

@ -21,6 +21,10 @@
},
"tooltip": {
"type": "string"
},
"disabled": {
"type": "boolean",
"default": false
}
},
"supports": {

View File

@ -30,7 +30,7 @@ export function Edit( {
context,
}: ProductEditorBlockEditProps< SalePriceBlockAttributes > ) {
const blockProps = useWooBlockProps( attributes );
const { label, help, isRequired, tooltip } = attributes;
const { label, help, isRequired, tooltip, disabled } = attributes;
const [ regularPrice, setRegularPrice ] = useEntityProp< string >(
'postType',
context.postType || 'product',
@ -120,6 +120,7 @@ export function Edit( {
label
)
}
disabled={ disabled }
onBlur={ validateRegularPrice }
/>
</BaseControl>

View File

@ -7,4 +7,5 @@ export interface SalePriceBlockAttributes extends BlockAttributes {
label: string;
help?: string;
isRequired?: boolean;
disabled?: boolean;
}

View File

@ -17,6 +17,10 @@
},
"tooltip": {
"type": "string"
},
"disabled": {
"type": "boolean",
"default": false
}
},
"supports": {

View File

@ -29,7 +29,7 @@ export function Edit( {
context,
}: ProductEditorBlockEditProps< SalePriceBlockAttributes > ) {
const blockProps = useWooBlockProps( attributes );
const { label, help, tooltip } = attributes;
const { label, help, tooltip, disabled } = attributes;
const [ regularPrice ] = useEntityProp< string >(
'postType',
context.postType || 'product',
@ -102,6 +102,7 @@ export function Edit( {
label
)
}
disabled={ disabled }
onBlur={ validateSalePrice }
/>
</BaseControl>

View File

@ -6,4 +6,5 @@ import { BlockAttributes } from '@wordpress/blocks';
export interface SalePriceBlockAttributes extends BlockAttributes {
label: string;
help?: string;
disabled?: boolean;
}

View File

@ -11,6 +11,10 @@
"title": {
"type": "string",
"__experimentalRole": "content"
},
"disabled": {
"type": "boolean",
"default": false
}
},
"supports": {

View File

@ -161,7 +161,7 @@ export function Edit( {
shippingClasses ?? []
),
] }
disabled={ virtual }
disabled={ attributes.disabled || virtual }
help={ createInterpolateElement(
__(
'Manage shipping classes and rates in <Link>global settings</Link>.',

View File

@ -5,4 +5,5 @@ import { BlockAttributes } from '@wordpress/blocks';
export interface ShippingClassBlockAttributes extends BlockAttributes {
title: string;
disabled?: boolean;
}

View File

@ -11,6 +11,10 @@
"__contentEditable": {
"type": "string",
"__experimentalRole": "content"
},
"disabled": {
"type": "boolean",
"default": false
}
},
"supports": {

View File

@ -90,7 +90,7 @@ export function Edit( {
onFocus: () => setHighlightSide( side ),
onBlur: () => setHighlightSide( undefined ),
suffix: dimensionUnit,
disabled: virtual,
disabled: attributes.disabled || virtual,
};
}
@ -185,7 +185,7 @@ export function Edit( {
suffix: weightUnit,
ref: weightRef,
onBlur: validateWeight,
disabled: virtual,
disabled: attributes.disabled || virtual,
};
return (

View File

@ -5,4 +5,5 @@ import { BlockAttributes } from '@wordpress/blocks';
export interface ShippingDimensionsBlockAttributes extends BlockAttributes {
__contentEditable: string;
disabled?: boolean;
}

View File

@ -6,4 +6,5 @@ import { RadioControl } from '@wordpress/components';
export type RadioFieldProps< T > = Omit< RadioControl.Props< T >, 'label' > & {
title: string;
description?: string;
disabled?: boolean;
};

View File

@ -0,0 +1,4 @@
Significance: minor
Type: add
Disable the fields that are not required in variable products because they are set in each variation

View File

@ -610,10 +610,10 @@ class SimpleProductTemplate extends AbstractProductFormTemplate implements Produ
);
$pricing_column_1->add_block(
array(
'id' => 'product-pricing-regular-price',
'blockName' => 'woocommerce/product-regular-price-field',
'order' => 10,
'attributes' => array(
'id' => 'product-pricing-regular-price',
'blockName' => 'woocommerce/product-regular-price-field',
'order' => 10,
'attributes' => array(
'name' => 'regular_price',
'label' => __( 'List price', 'woocommerce' ),
'help' => $is_calc_taxes_enabled ? null : sprintf(
@ -623,6 +623,11 @@ class SimpleProductTemplate extends AbstractProductFormTemplate implements Produ
'</a>'
),
),
'disableConditions' => array(
array(
'expression' => 'editedProduct.type === "variable"',
),
),
)
);
$pricing_column_2 = $pricing_columns->add_block(
@ -637,12 +642,17 @@ class SimpleProductTemplate extends AbstractProductFormTemplate implements Produ
);
$pricing_column_2->add_block(
array(
'id' => 'product-pricing-sale-price',
'blockName' => 'woocommerce/product-sale-price-field',
'order' => 10,
'attributes' => array(
'id' => 'product-pricing-sale-price',
'blockName' => 'woocommerce/product-sale-price-field',
'order' => 10,
'attributes' => array(
'label' => __( 'Sale price', 'woocommerce' ),
),
'disableConditions' => array(
array(
'expression' => 'editedProduct.type === "variable"',
),
),
)
);
$product_pricing_section->add_block(
@ -788,32 +798,44 @@ class SimpleProductTemplate extends AbstractProductFormTemplate implements Produ
);
$product_inventory_inner_section->add_block(
array(
'id' => 'product-sku-field',
'blockName' => 'woocommerce/product-sku-field',
'order' => 10,
'id' => 'product-sku-field',
'blockName' => 'woocommerce/product-sku-field',
'order' => 10,
'disableConditions' => array(
array(
'expression' => 'editedProduct.type === "variable"',
),
),
)
);
$manage_stock = 'yes' === get_option( 'woocommerce_manage_stock' );
$product_inventory_inner_section->add_block(
array(
'id' => 'product-track-stock',
'blockName' => 'woocommerce/product-toggle-field',
'order' => 20,
'attributes' => array(
'id' => 'product-track-stock',
'blockName' => 'woocommerce/product-toggle-field',
'order' => 20,
'attributes' => array(
'label' => __( 'Track inventory', 'woocommerce' ),
'property' => 'manage_stock',
'disabled' => 'yes' !== get_option( 'woocommerce_manage_stock' ),
'disabledCopy' => sprintf(
'disabled' => ! $manage_stock,
'disabledCopy' => ! $manage_stock ? sprintf(
/* translators: %1$s: Learn more link opening tag. %2$s: Learn more link closing tag.*/
__( 'Per your %1$sstore settings%2$s, inventory management is <strong>disabled</strong>.', 'woocommerce' ),
'<a href="' . admin_url( 'admin.php?page=wc-settings&tab=products&section=inventory' ) . '" target="_blank" rel="noreferrer">',
'</a>'
),
) : null,
),
'hideConditions' => Features::is_enabled( 'product-external-affiliate' ) || Features::is_enabled( 'product-grouped' ) ? array(
'hideConditions' => Features::is_enabled( 'product-external-affiliate' ) || Features::is_enabled( 'product-grouped' ) ? array(
array(
'expression' => 'editedProduct.type === "external" || editedProduct.type === "grouped"',
),
) : null,
'disableConditions' => array(
array(
'expression' => 'editedProduct.type === "variable"',
),
),
)
);
$product_inventory_quantity_hide_conditions = array(
@ -846,10 +868,10 @@ class SimpleProductTemplate extends AbstractProductFormTemplate implements Produ
}
$product_inventory_section->add_block(
array(
'id' => 'product-stock-status',
'blockName' => 'woocommerce/product-radio-field',
'order' => 10,
'attributes' => array(
'id' => 'product-stock-status',
'blockName' => 'woocommerce/product-radio-field',
'order' => 10,
'attributes' => array(
'title' => __( 'Stock status', 'woocommerce' ),
'property' => 'stock_status',
'options' => array(
@ -867,7 +889,12 @@ class SimpleProductTemplate extends AbstractProductFormTemplate implements Produ
),
),
),
'hideConditions' => $product_stock_status_hide_conditions,
'hideConditions' => $product_stock_status_hide_conditions,
'disableConditions' => array(
array(
'expression' => 'editedProduct.type === "variable"',
),
),
)
);
@ -1042,16 +1069,26 @@ class SimpleProductTemplate extends AbstractProductFormTemplate implements Produ
);
$product_fee_and_dimensions_section->add_block(
array(
'id' => 'product-shipping-class',
'blockName' => 'woocommerce/product-shipping-class-field',
'order' => 10,
'id' => 'product-shipping-class',
'blockName' => 'woocommerce/product-shipping-class-field',
'order' => 10,
'disableConditions' => array(
array(
'expression' => 'editedProduct.type === "variable"',
),
),
)
);
$product_fee_and_dimensions_section->add_block(
array(
'id' => 'product-shipping-dimensions',
'blockName' => 'woocommerce/product-shipping-dimensions-fields',
'order' => 20,
'id' => 'product-shipping-dimensions',
'blockName' => 'woocommerce/product-shipping-dimensions-fields',
'order' => 20,
'disableConditions' => array(
array(
'expression' => 'editedProduct.type === "variable"',
),
),
)
);
}

View File

@ -91,7 +91,7 @@ test( 'can update stock status', async ( { page, product } ) => {
test( 'can track stock quantity', async ( { page, product } ) => {
await test.step( 'enable track stock quantity', async () => {
await page.getByLabel( 'Track stock quantity for this' ).check();
await page.getByLabel( 'Track inventory' ).check();
// await closeTourModal( { page, timeout: 2000 } );
await page.getByRole( 'button', { name: 'Advanced' } ).click();
await page.getByLabel( "Don't allow purchases" ).check();