From d0450fa43c0d2b13158a9cd7c2baca8255065538 Mon Sep 17 00:00:00 2001 From: Fernando Marichal Date: Mon, 20 Mar 2023 09:32:38 -0300 Subject: [PATCH] Show `Stock status` options as radio buttons (#37278) * Add check to show Stock status as radiobuttons * Add changelog * Fix lint * Fix typo * Fix flaky e2e tests * Rename hook * Fix validation to show radio --------- Co-authored-by: Fernando Marichal --- ...v-37116_show_stock_status_as_radio_buttons | 4 +++ .../views/html-product-data-inventory.php | 35 +++++++++++++------ .../merchant/create-variable-product.spec.js | 18 +++++----- 3 files changed, 39 insertions(+), 18 deletions(-) create mode 100644 plugins/woocommerce/changelog/dev-37116_show_stock_status_as_radio_buttons diff --git a/plugins/woocommerce/changelog/dev-37116_show_stock_status_as_radio_buttons b/plugins/woocommerce/changelog/dev-37116_show_stock_status_as_radio_buttons new file mode 100644 index 00000000000..3d2554ce4db --- /dev/null +++ b/plugins/woocommerce/changelog/dev-37116_show_stock_status_as_radio_buttons @@ -0,0 +1,4 @@ +Significance: minor +Type: dev + +Show "Stock status" as a collection of radio buttons diff --git a/plugins/woocommerce/includes/admin/meta-boxes/views/html-product-data-inventory.php b/plugins/woocommerce/includes/admin/meta-boxes/views/html-product-data-inventory.php index 952e063cd07..1e0b358ad7d 100644 --- a/plugins/woocommerce/includes/admin/meta-boxes/views/html-product-data-inventory.php +++ b/plugins/woocommerce/includes/admin/meta-boxes/views/html-product-data-inventory.php @@ -115,18 +115,33 @@ if ( ! defined( 'ABSPATH' ) ) { } - woocommerce_wp_select( - array( - 'id' => '_stock_status', - 'value' => $product_object->get_stock_status( 'edit' ), - 'wrapper_class' => 'stock_status_field hide_if_variable hide_if_external hide_if_grouped', - 'label' => __( 'Stock status', 'woocommerce' ), - 'options' => wc_get_product_stock_status_options(), - 'desc_tip' => true, - 'description' => __( 'Controls whether or not the product is listed as "in stock" or "out of stock" on the frontend.', 'woocommerce' ), - ) + $stock_status_options = wc_get_product_stock_status_options(); + $stock_status_count = count( $stock_status_options ); + $common_stock_status_args = array( + 'id' => '_stock_status', + 'value' => $product_object->get_stock_status( 'edit' ), + 'wrapper_class' => 'stock_status_field hide_if_variable hide_if_external hide_if_grouped', + 'label' => __( 'Stock status', 'woocommerce' ), + 'options' => $stock_status_options, ); + /** + * Allow 3rd parties to control whether the "Stock status" option will use radio buttons or a select. + * + * @since 7.6.0 + * + * @param bool If false, the "Stock status" will be shown as a select. Default: it will use radio buttons. + */ + if ( apply_filters( 'woocommerce_product_stock_status_use_radio', $stock_status_count <= 3 && $stock_status_count >= 1 ) ) { + woocommerce_wp_radio( $common_stock_status_args ); + } else { + $select_input_args = array( + 'desc_tip' => true, + 'description' => __( 'Controls whether or not the product is listed as "in stock" or "out of stock" on the frontend.', 'woocommerce' ), + ); + woocommerce_wp_select( array_merge( $common_stock_status_args, $select_input_args ) ); + } + do_action( 'woocommerce_product_options_stock_status' ); ?> diff --git a/plugins/woocommerce/tests/e2e-pw/tests/merchant/create-variable-product.spec.js b/plugins/woocommerce/tests/e2e-pw/tests/merchant/create-variable-product.spec.js index 22409e42599..0e52a26e8fd 100644 --- a/plugins/woocommerce/tests/e2e-pw/tests/merchant/create-variable-product.spec.js +++ b/plugins/woocommerce/tests/e2e-pw/tests/merchant/create-variable-product.spec.js @@ -55,10 +55,10 @@ test.describe.serial( 'Add New Variable Product Page', () => { if ( i > 0 ) { await page.click( 'button.add_attribute' ); } - await page.fill( - `input[name="attribute_names[${ i }]"]`, - `attr #${ i + 1 }` - ); + const input = `input[name="attribute_names[${ i }]"]`; + + await page.waitForSelector( input, { timeout: 1000 } ); // Wait for up to 1 second + await page.fill( input, `attr #${ i + 1 }` ); await page.fill( `textarea[name="attribute_values[${ i }]"]`, 'val1 | val2' @@ -67,6 +67,7 @@ test.describe.serial( 'Add New Variable Product Page', () => { await page.keyboard.press( 'ArrowUp' ); await page.click( 'text=Save attributes' ); + await page.waitForTimeout( 1000 ); // Wait for 1 second // Save before going to the Variations tab to prevent variations from all attributes to be automatically created await page.locator( '#save-post' ).click(); await expect( @@ -205,10 +206,10 @@ test.describe.serial( 'Add New Variable Product Page', () => { if ( i > 0 ) { await page.click( 'button.add_attribute' ); } - await page.fill( - `input[name="attribute_names[${ i }]"]`, - `attr #${ i + 1 }` - ); + const input = `input[name="attribute_names[${ i }]"]`; + + await page.waitForSelector( input, { timeout: 1000 } ); // Wait for up to 1 seconds + await page.fill( input, `attr #${ i + 1 }` ); await page.fill( `textarea[name="attribute_values[${ i }]"]`, 'val1 | val2' @@ -222,6 +223,7 @@ test.describe.serial( 'Add New Variable Product Page', () => { ).toBeVisible(); } + await page.waitForTimeout( 1000 ); // Wait for 1 second // Save before going to the Variations tab to prevent variations from all attributes to be automatically created await page.locator( '#save-post' ).click(); await expect(