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 00000000000..d12c6ae4256 --- /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. 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 8e1be2c6ee7..c23ed507e29 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' ); @@ -436,12 +407,19 @@ 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(); - } + 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 ) { + add_custom_attribute_to_list(); + } + } ); + + const woocommerce_attribute_items = $product_attributes.find( '.woocommerce_attribute' ).get(); // Sort the attributes by their position. woocommerce_attribute_items.sort( function ( a, b ) { @@ -500,6 +478,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, @@ -568,7 +547,7 @@ jQuery( function ( $ ) { toggle_expansion_of_attribute_list_item( $attributeListItem ); - disable_or_enable_fields(); + $( document.body ).trigger( 'woocommerce_added_attribute' ); jQuery.maybe_disable_save_button(); } catch ( error ) { @@ -914,8 +893,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' ) diff --git a/plugins/woocommerce/client/legacy/js/admin/meta-boxes.js b/plugins/woocommerce/client/legacy/js/admin/meta-boxes.js index 34b42f3d0ec..766249c4c38 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 ) 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 d94e353542e..30db0fac065 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 @@ if ( ! defined( 'ABSPATH' ) ) {
- +
diff --git a/plugins/woocommerce/includes/class-wc-ajax.php b/plugins/woocommerce/includes/class-wc-ajax.php index 6d66ab1224f..68c31da3982 100644 --- a/plugins/woocommerce/includes/class-wc-ajax.php +++ b/plugins/woocommerce/includes/class-wc-ajax.php @@ -611,6 +611,8 @@ class WC_AJAX { wp_die( -1 ); } + $product_type = isset( $_POST['product_type'] ) ? sanitize_text_field( wp_unslash( $_POST['product_type'] ) ) : 'simple'; + $i = absint( $_POST['i'] ); $metabox_class = array(); $attribute = new WC_Product_Attribute(); @@ -619,7 +621,13 @@ class WC_AJAX { $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() ) {