Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not disable "Used for variations" checkbox #39502

Merged
merged 8 commits into from Aug 1, 2023
@@ -0,0 +1,4 @@
Significance: patch
Type: fix

Do not disable "Used for variations" checkbox on attribute.
51 changes: 14 additions & 37 deletions plugins/woocommerce/client/legacy/js/admin/meta-boxes-product.js
Expand Up @@ -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 () {
Expand All @@ -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' );
Expand Down Expand Up @@ -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 ) {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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 ) {
Expand Down Expand Up @@ -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' )
Expand Down
4 changes: 3 additions & 1 deletion plugins/woocommerce/client/legacy/js/admin/meta-boxes.js
Expand Up @@ -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 )
Expand Down
Expand Up @@ -93,7 +93,7 @@ class="multiselect attribute_values wc-taxonomy-term-search"
<tr>
<td>
<div class="enable_variation show_if_variable">
<label><input type="checkbox" disabled class="woocommerce_attribute_used_for_variations checkbox enable_if_variable" <?php checked( $attribute->get_variation(), true ); ?> name="attribute_variation[<?php echo esc_attr( $i ); ?>]" value="1" /> <?php esc_html_e( 'Used for variations', 'woocommerce' ); ?></label>
<label><input type="checkbox" class="woocommerce_attribute_used_for_variations checkbox" <?php checked( $attribute->get_variation(), true ); ?> name="attribute_variation[<?php echo esc_attr( $i ); ?>]" value="1" /> <?php esc_html_e( 'Used for variations', 'woocommerce' ); ?></label>
</div>
</td>
</tr>
Expand Down
10 changes: 9 additions & 1 deletion plugins/woocommerce/includes/class-wc-ajax.php
Expand Up @@ -611,6 +611,8 @@ public static function add_attribute() {
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();
Expand All @@ -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(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would reverting to the previous functionality be sufficient?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would also fix the test-- setting a simple product as the default could create more edge cases in the future 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AnnaMag I don't see any failing test. What test are you referring to?

I'd prefer not to revert to the previous functionality, since Jarek has been quite set that this change will help prevent confusion based on his user research.

'woocommerce_attribute_default_is_variation',
'variable' === $product_type ? 1 : 0,
$product_type
)
);
/* phpcs: enable */

if ( $attribute->is_taxonomy() ) {
Expand Down