Manage single variation as virtual (#40809)

* Add virtual section to the product variation template

* Add changelog file

* Let the toogle block to use inverted value to be checked

* Fix virtual toggle to be inverted

* Add changelog file
This commit is contained in:
Maikel David Pérez Gómez 2023-10-18 09:47:33 -04:00 committed by GitHub
parent a44386a70b
commit 39019d2750
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 65 additions and 6 deletions

View File

@ -0,0 +1,4 @@
Significance: minor
Type: add
Let the toogle block to use inverted value to be checked

View File

@ -22,6 +22,12 @@
"disabledCopy": { "disabledCopy": {
"type": "string", "type": "string",
"__experimentalRole": "content" "__experimentalRole": "content"
},
"checkedValue": {
"type": "object"
},
"uncheckedValue": {
"type": "object"
} }
}, },
"supports": { "supports": {

View File

@ -18,19 +18,41 @@ export function Edit( {
context: { postType }, context: { postType },
}: ProductEditorBlockEditProps< ToggleBlockAttributes > ) { }: ProductEditorBlockEditProps< ToggleBlockAttributes > ) {
const blockProps = useWooBlockProps( attributes ); const blockProps = useWooBlockProps( attributes );
const { label, property, disabled, disabledCopy } = attributes; const {
label,
property,
disabled,
disabledCopy,
checkedValue,
uncheckedValue,
} = attributes;
const [ value, setValue ] = useProductEntityProp< boolean >( property, { const [ value, setValue ] = useProductEntityProp< boolean >( property, {
postType, postType,
fallbackValue: false, fallbackValue: false,
} ); } );
function isChecked() {
if ( checkedValue !== undefined ) {
return checkedValue === value;
}
return value as boolean;
}
function handleChange( checked: boolean ) {
if ( checked ) {
setValue( checkedValue !== undefined ? checkedValue : checked );
} else {
setValue( uncheckedValue !== undefined ? uncheckedValue : checked );
}
}
return ( return (
<div { ...blockProps }> <div { ...blockProps }>
<ToggleControl <ToggleControl
label={ label } label={ label }
checked={ value } checked={ isChecked() }
disabled={ disabled } disabled={ disabled }
onChange={ setValue } onChange={ handleChange }
/> />
{ disabled && ( { disabled && (
<p <p

View File

@ -7,4 +7,6 @@ export interface ToggleBlockAttributes extends BlockAttributes {
label: string; label: string;
property: string; property: string;
disabled?: boolean; disabled?: boolean;
checkedValue?: never;
uncheckedValue?: never;
} }

View File

@ -0,0 +1,4 @@
Significance: minor
Type: add
Add virtual section to the product variation template

View File

@ -475,11 +475,30 @@ class ProductVariationTemplate extends AbstractProductFormTemplate implements Pr
], ],
] ]
); );
// Virtual section.
$shipping_group->add_section(
[
'id' => 'product-variation-virtual-section',
'order' => 20,
]
)->add_block(
[
'id' => 'product-variation-virtual',
'blockName' => 'woocommerce/product-toggle-field',
'order' => 10,
'attributes' => [
'property' => 'virtual',
'checkedValue' => false,
'uncheckedValue' => true,
'label' => __( 'This variation requires shipping or pickup', 'woocommerce' ),
],
]
);
// Product Shipping Section. // Product Shipping Section.
$product_fee_and_dimensions_section = $shipping_group->add_section( $product_fee_and_dimensions_section = $shipping_group->add_section(
[ [
'id' => 'product-variation-fee-and-dimensions-section', 'id' => 'product-variation-fee-and-dimensions-section',
'order' => 20, 'order' => 30,
'attributes' => [ 'attributes' => [
'title' => __( 'Fees & dimensions', 'woocommerce' ), 'title' => __( 'Fees & dimensions', 'woocommerce' ),
'description' => sprintf( 'description' => sprintf(

View File

@ -776,8 +776,10 @@ class SimpleProductTemplate extends AbstractProductFormTemplate implements Produ
'blockName' => 'woocommerce/product-toggle-field', 'blockName' => 'woocommerce/product-toggle-field',
'order' => 10, 'order' => 10,
'attributes' => [ 'attributes' => [
'property' => 'virtual', 'property' => 'virtual',
'label' => __( 'This product requires shipping or pickup', 'woocommerce' ), 'checkedValue' => false,
'uncheckedValue' => true,
'label' => __( 'This product requires shipping or pickup', 'woocommerce' ),
], ],
] ]
); );