From 119ab3c3bbc29bf7f8b251126982faa600e051e9 Mon Sep 17 00:00:00 2001 From: Matt Sherman Date: Mon, 31 Jul 2023 18:11:13 -0400 Subject: [PATCH 1/8] Only set attribute as used for variation by default if product type is variable --- plugins/woocommerce/includes/class-wc-ajax.php | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/plugins/woocommerce/includes/class-wc-ajax.php b/plugins/woocommerce/includes/class-wc-ajax.php index 6d66ab1224ffb..e0edbc3a84d65 100644 --- a/plugins/woocommerce/includes/class-wc-ajax.php +++ b/plugins/woocommerce/includes/class-wc-ajax.php @@ -611,6 +611,8 @@ public static function add_attribute() { wp_die( -1 ); } + $product_type = sanitize_text_field( wp_unslash( $_POST['product_type'] ) ); + $i = absint( $_POST['i'] ); $metabox_class = array(); $attribute = new WC_Product_Attribute(); @@ -619,7 +621,13 @@ public static function add_attribute() { $attribute->set_name( sanitize_text_field( wp_unslash( $_POST['taxonomy'] ) ) ); /* phpcs:disable WooCommerce.Commenting.CommentHooks.MissingHookComment */ $attribute->set_visible( apply_filters( 'woocommerce_attribute_default_visibility', 1 ) ); - $attribute->set_variation( apply_filters( 'woocommerce_attribute_default_is_variation', 1 ) ); + $attribute->set_variation( + apply_filters( + 'woocommerce_attribute_default_is_variation', + 'variable' === $product_type ? 1 : 0, + $product_type + ) + ); /* phpcs: enable */ if ( $attribute->is_taxonomy() ) { From 8f4b123d802b0a8ab1508350b9be4f353f0c702e Mon Sep 17 00:00:00 2001 From: Matt Sherman Date: Mon, 31 Jul 2023 18:11:52 -0400 Subject: [PATCH 2/8] Trigger woocommerce_tab_shown when a tab is shown --- plugins/woocommerce/client/legacy/js/admin/meta-boxes.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/plugins/woocommerce/client/legacy/js/admin/meta-boxes.js b/plugins/woocommerce/client/legacy/js/admin/meta-boxes.js index 34b42f3d0eca6..766249c4c38ce 100644 --- a/plugins/woocommerce/client/legacy/js/admin/meta-boxes.js +++ b/plugins/woocommerce/client/legacy/js/admin/meta-boxes.js @@ -126,7 +126,9 @@ jQuery( function ( $ ) { $( 'ul.wc-tabs li', panel_wrap ).removeClass( 'active' ); $( this ).parent().addClass( 'active' ); $( 'div.panel', panel_wrap ).hide(); - $( $( this ).attr( 'href' ) ).show(); + $( $( this ).attr( 'href' ) ).show( 0, function () { + $( this ).trigger( 'woocommerce_tab_shown' ); + } ); } ); $( 'div.panel-wrap' ).each( function () { $( this ) From 850acd0ce26ace66b38e5d9b4287584332da7949 Mon Sep 17 00:00:00 2001 From: Matt Sherman Date: Mon, 31 Jul 2023 18:13:58 -0400 Subject: [PATCH 3/8] Add the empty attribute when the attributes tab is shown, to account for product type switching --- .../client/legacy/js/admin/meta-boxes-product.js | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/plugins/woocommerce/client/legacy/js/admin/meta-boxes-product.js b/plugins/woocommerce/client/legacy/js/admin/meta-boxes-product.js index 8e1be2c6ee715..2ef8a20135a95 100644 --- a/plugins/woocommerce/client/legacy/js/admin/meta-boxes-product.js +++ b/plugins/woocommerce/client/legacy/js/admin/meta-boxes-product.js @@ -436,12 +436,17 @@ jQuery( function ( $ ) { // Set up attributes, if current page has the attributes list. const $product_attributes = $( '.product_attributes' ); if ( $product_attributes.length === 1 ) { - var woocommerce_attribute_items = $product_attributes.find( '.woocommerce_attribute' ).get(); + // When the attributes tab is shown, add an empty attribute to be filled out by the user. + $( '#product_attributes' ).on( 'woocommerce_tab_shown', function() { + remove_blank_custom_attribute_if_no_other_attributes(); - // If the product has no attributes, add an empty attribute to be filled out by the user. - if ( woocommerce_attribute_items.length === 0 ) { - add_custom_attribute_to_list(); - } + var woocommerce_attribute_items = $product_attributes.find( '.woocommerce_attribute' ).get(); + + // If the product has no attributes, add an empty attribute to be filled out by the user. + if ( woocommerce_attribute_items.length === 0 ) { + add_custom_attribute_to_list(); + } + } ); // Sort the attributes by their position. woocommerce_attribute_items.sort( function ( a, b ) { @@ -500,6 +505,7 @@ jQuery( function ( $ ) { url: woocommerce_admin_meta_boxes.ajax_url, data: { action: 'woocommerce_add_attribute', + product_type: $( '#product-type' ).val(), taxonomy: globalAttributeId ? globalAttributeId : '', i: indexInList, security: woocommerce_admin_meta_boxes.add_attribute_nonce, From a78d4d678890256058a222be0a86799e353ccd24 Mon Sep 17 00:00:00 2001 From: Matt Sherman Date: Mon, 31 Jul 2023 18:21:32 -0400 Subject: [PATCH 4/8] Do not disable Used for variations checkbox by default --- .../admin/meta-boxes/views/html-product-attribute-inner.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/woocommerce/includes/admin/meta-boxes/views/html-product-attribute-inner.php b/plugins/woocommerce/includes/admin/meta-boxes/views/html-product-attribute-inner.php index d94e353542eed..30db0fac065a6 100644 --- a/plugins/woocommerce/includes/admin/meta-boxes/views/html-product-attribute-inner.php +++ b/plugins/woocommerce/includes/admin/meta-boxes/views/html-product-attribute-inner.php @@ -93,7 +93,7 @@ class="multiselect attribute_values wc-taxonomy-term-search"
- +
From a38a867f201582b197617cf472ee1d2cc92f778e Mon Sep 17 00:00:00 2001 From: Matt Sherman Date: Mon, 31 Jul 2023 18:22:03 -0400 Subject: [PATCH 5/8] Remove disable_or_enable_fields() implementation --- .../legacy/js/admin/meta-boxes-product.js | 33 ------------------- 1 file changed, 33 deletions(-) diff --git a/plugins/woocommerce/client/legacy/js/admin/meta-boxes-product.js b/plugins/woocommerce/client/legacy/js/admin/meta-boxes-product.js index 2ef8a20135a95..9c5e87e05107c 100644 --- a/plugins/woocommerce/client/legacy/js/admin/meta-boxes-product.js +++ b/plugins/woocommerce/client/legacy/js/admin/meta-boxes-product.js @@ -126,34 +126,6 @@ jQuery( function ( $ ) { return false; } ); - function disable_or_enable_fields() { - var product_type = $( 'select#product-type' ).val(); - $( `.enable_if_simple` ).each( function () { - $( this ).addClass( 'disabled' ); - if ( $( this ).is( 'input' ) ) { - $( this ).prop( 'disabled', true ); - } - } ); - $( `.enable_if_external` ).each( function () { - $( this ).addClass( 'disabled' ); - if ( $( this ).is( 'input' ) ) { - $( this ).prop( 'disabled', true ); - } - } ); - $( `.enable_if_variable` ).each( function () { - $( this ).addClass( 'disabled' ); - if ( $( this ).is( 'input' ) ) { - $( this ).prop( 'disabled', true ); - } - } ); - $( `.enable_if_${ product_type }` ).each( function () { - $( this ).removeClass( 'disabled' ); - if ( $( this ).is( 'input' ) ) { - $( this ).prop( 'disabled', false ); - } - } ); - } - // Product type specific options. $( 'select#product-type' ) .on( 'change', function () { @@ -173,7 +145,6 @@ jQuery( function ( $ ) { } show_and_hide_panels(); - disable_or_enable_fields(); change_product_type_tip( get_product_tip_content( select_val ) ); $( 'ul.wc-tabs li:visible' ).eq( 0 ).find( 'a' ).trigger( 'click' ); @@ -574,8 +545,6 @@ jQuery( function ( $ ) { toggle_expansion_of_attribute_list_item( $attributeListItem ); - disable_or_enable_fields(); - jQuery.maybe_disable_save_button(); } catch ( error ) { if ( isPageUnloading ) { @@ -920,8 +889,6 @@ jQuery( function ( $ ) { // Hide the 'Used for variations' checkbox if not viewing a variable product show_and_hide_panels(); - disable_or_enable_fields(); - // Make sure the dropdown is not disabled for empty value attributes. $( 'select.attribute_taxonomy' ) .find( 'option' ) From 956aa7b84fc403f4182cc2845a90d78676c72ba7 Mon Sep 17 00:00:00 2001 From: Matt Sherman Date: Mon, 31 Jul 2023 18:58:03 -0400 Subject: [PATCH 6/8] Restore woocommerce_added_attribute trigger --- .../client/legacy/js/admin/meta-boxes-product.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/plugins/woocommerce/client/legacy/js/admin/meta-boxes-product.js b/plugins/woocommerce/client/legacy/js/admin/meta-boxes-product.js index 9c5e87e05107c..c23ed507e29b4 100644 --- a/plugins/woocommerce/client/legacy/js/admin/meta-boxes-product.js +++ b/plugins/woocommerce/client/legacy/js/admin/meta-boxes-product.js @@ -411,7 +411,7 @@ jQuery( function ( $ ) { $( '#product_attributes' ).on( 'woocommerce_tab_shown', function() { remove_blank_custom_attribute_if_no_other_attributes(); - var woocommerce_attribute_items = $product_attributes.find( '.woocommerce_attribute' ).get(); + const woocommerce_attribute_items = $product_attributes.find( '.woocommerce_attribute' ).get(); // If the product has no attributes, add an empty attribute to be filled out by the user. if ( woocommerce_attribute_items.length === 0 ) { @@ -419,6 +419,8 @@ jQuery( function ( $ ) { } } ); + const woocommerce_attribute_items = $product_attributes.find( '.woocommerce_attribute' ).get(); + // Sort the attributes by their position. woocommerce_attribute_items.sort( function ( a, b ) { var compA = parseInt( $( a ).attr( 'rel' ), 10 ); @@ -545,6 +547,8 @@ jQuery( function ( $ ) { toggle_expansion_of_attribute_list_item( $attributeListItem ); + $( document.body ).trigger( 'woocommerce_added_attribute' ); + jQuery.maybe_disable_save_button(); } catch ( error ) { if ( isPageUnloading ) { From 479c4ed0ebd3edc20529bb79cef45fd58a77e683 Mon Sep 17 00:00:00 2001 From: Matt Sherman Date: Mon, 31 Jul 2023 19:00:08 -0400 Subject: [PATCH 7/8] Changelog --- .../changelog/fix-used-for-variations-checkbox-disabled | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 plugins/woocommerce/changelog/fix-used-for-variations-checkbox-disabled diff --git a/plugins/woocommerce/changelog/fix-used-for-variations-checkbox-disabled b/plugins/woocommerce/changelog/fix-used-for-variations-checkbox-disabled new file mode 100644 index 0000000000000..d12c6ae4256bf --- /dev/null +++ b/plugins/woocommerce/changelog/fix-used-for-variations-checkbox-disabled @@ -0,0 +1,4 @@ +Significance: patch +Type: fix + +Do not disable "Used for variations" checkbox on attribute. From 144606f1fb0e5aed031e4853a86622175948be76 Mon Sep 17 00:00:00 2001 From: Matt Sherman Date: Mon, 31 Jul 2023 19:10:36 -0400 Subject: [PATCH 8/8] Check if product_type query arg is set before using --- plugins/woocommerce/includes/class-wc-ajax.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/woocommerce/includes/class-wc-ajax.php b/plugins/woocommerce/includes/class-wc-ajax.php index e0edbc3a84d65..68c31da398217 100644 --- a/plugins/woocommerce/includes/class-wc-ajax.php +++ b/plugins/woocommerce/includes/class-wc-ajax.php @@ -611,7 +611,7 @@ public static function add_attribute() { wp_die( -1 ); } - $product_type = sanitize_text_field( wp_unslash( $_POST['product_type'] ) ); + $product_type = isset( $_POST['product_type'] ) ? sanitize_text_field( wp_unslash( $_POST['product_type'] ) ) : 'simple'; $i = absint( $_POST['i'] ); $metabox_class = array();