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": {
"type": "string",
"__experimentalRole": "content"
},
"checkedValue": {
"type": "object"
},
"uncheckedValue": {
"type": "object"
}
},
"supports": {

View File

@ -18,19 +18,41 @@ export function Edit( {
context: { postType },
}: ProductEditorBlockEditProps< ToggleBlockAttributes > ) {
const blockProps = useWooBlockProps( attributes );
const { label, property, disabled, disabledCopy } = attributes;
const {
label,
property,
disabled,
disabledCopy,
checkedValue,
uncheckedValue,
} = attributes;
const [ value, setValue ] = useProductEntityProp< boolean >( property, {
postType,
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 (
<div { ...blockProps }>
<ToggleControl
label={ label }
checked={ value }
checked={ isChecked() }
disabled={ disabled }
onChange={ setValue }
onChange={ handleChange }
/>
{ disabled && (
<p

View File

@ -7,4 +7,6 @@ export interface ToggleBlockAttributes extends BlockAttributes {
label: string;
property: string;
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_fee_and_dimensions_section = $shipping_group->add_section(
[
'id' => 'product-variation-fee-and-dimensions-section',
'order' => 20,
'order' => 30,
'attributes' => [
'title' => __( 'Fees & dimensions', 'woocommerce' ),
'description' => sprintf(

View File

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