Commit ef1d1a03353 for woocommerce

commit ef1d1a033530514701578cdc0e7433034199defd
Author: Raluca Stan <ralucastn@gmail.com>
Date:   Thu Nov 26 17:26:19 2026 +0100

    Fix order Update doing nothing when an item has a decimal quantity (#71457)

    * Fix order Update being blocked by a quantity that fails its step

    The order editor renders a hidden quantity input for each line item.
    When the stored quantity does not match the input's step, the browser
    fails the form's validation on Update, cannot focus the hidden field,
    and drops the submit without a message.

    Print step="any" when the stored quantity does not match the step, and
    when the step is the default 1 on a store that allows decimal
    quantities. The step filter is called as before; only the printed
    attribute changes.

    Co-authored-by: Anuj-Rathore24 <anujsingh2409@gmail.com>

    * Add changelog entry for the order item quantity step fix

    * Return a nullable float from the order item quantity step helper

    The helper returned the HTML attribute value: a numeric string or 'any'.
    It now returns the step as a float, or null when the input accepts any
    quantity, and the template prints 'any' for null. The method is renamed
    to get_quantity_input_step() to match. No behaviour change.

    * Add a comment on why a default step of 1 is left out on decimal stores

    * Add decimal quantity cases to the order item step tests

    * Document that the admin quantity step filter can carry a string

    Callbacks on woocommerce_quantity_input_step_admin return strings in practice, such as 'any' or a numeric string, so a later callback can receive one. Say that 'any' makes the input accept any quantity.

    ---------

    Co-authored-by: Anuj-Rathore24 <anujsingh2409@gmail.com>

diff --git a/plugins/woocommerce/changelog/fix-68170-order-update-quantity-step b/plugins/woocommerce/changelog/fix-68170-order-update-quantity-step
new file mode 100644
index 00000000000..c0de28ecf51
--- /dev/null
+++ b/plugins/woocommerce/changelog/fix-68170-order-update-quantity-step
@@ -0,0 +1,4 @@
+Significance: patch
+Type: fix
+
+Fix the Update button doing nothing on orders that have an item with a decimal quantity.
diff --git a/plugins/woocommerce/includes/admin/meta-boxes/views/html-order-item.php b/plugins/woocommerce/includes/admin/meta-boxes/views/html-order-item.php
index dc6634ffbdc..97d131bd320 100644
--- a/plugins/woocommerce/includes/admin/meta-boxes/views/html-order-item.php
+++ b/plugins/woocommerce/includes/admin/meta-boxes/views/html-order-item.php
@@ -110,10 +110,14 @@ $item_name = apply_filters( 'woocommerce_order_item_name', $item->get_name(), $i
 			/**
 			* Filter to change the product quantity stepping in the order editor of the admin area.
 			*
+			* In the 'edit' context the input accepts any quantity, instead of the returned
+			* step, when the item's stored quantity does not match that step, or when the
+			* step is 1 and the store allows decimal quantities.
+			*
 			* @since   5.8.0
-			* @param   string      $step    The current step amount to be used in the quantity editor.
-			* @param   WC_Product  $product The product that is being edited.
-			* @param   string      $context The context in which the quantity editor is shown, 'edit' or 'refund'.
+			* @param   int|float|string $step    The current step amount to be used in the quantity editor, or 'any' to accept any quantity.
+			* @param   WC_Product       $product The product that is being edited.
+			* @param   string           $context The context in which the quantity editor is shown, 'edit' or 'refund'.
 			*/
 			$step_edit   = apply_filters( 'woocommerce_quantity_input_step_admin', $step, $product, 'edit' );
 			$step_refund = apply_filters( 'woocommerce_quantity_input_step_admin', $step, $product, 'refund' );
@@ -133,9 +137,10 @@ $item_name = apply_filters( 'woocommerce_order_item_name', $item->get_name(), $i
 			*/
 			$min_refund = apply_filters( 'woocommerce_quantity_input_min_admin', '0', $product, 'refund' );
 			$min_edit   = wc_get_container()->get( ItemQuantityLimits::class )->get_quantity_input_min( $item, $product );
+			$step_edit  = wc_get_container()->get( ItemQuantityLimits::class )->get_quantity_input_step( $step_edit, (float) $item->get_quantity(), $min_edit );
 		?>
 		<div class="edit" style="display: none;">
-			<input type="number" step="<?php echo esc_attr( $step_edit ); ?>" min="<?php echo esc_attr( $min_edit ); ?>" autocomplete="off" name="order_item_qty[<?php echo absint( $item_id ); ?>]" placeholder="0" value="<?php echo esc_attr( $item->get_quantity() ); ?>" data-qty="<?php echo esc_attr( $item->get_quantity() ); ?>" size="4" class="quantity" />
+			<input type="number" step="<?php echo esc_attr( null === $step_edit ? 'any' : (string) $step_edit ); ?>" min="<?php echo esc_attr( $min_edit ); ?>" autocomplete="off" name="order_item_qty[<?php echo absint( $item_id ); ?>]" placeholder="0" value="<?php echo esc_attr( $item->get_quantity() ); ?>" data-qty="<?php echo esc_attr( $item->get_quantity() ); ?>" size="4" class="quantity" />
 		</div>
 		<div class="refund" style="display: none;">
 			<input type="number" step="<?php echo esc_attr( $step_refund ); ?>" min="<?php echo esc_attr( $min_refund ); ?>" max="<?php echo esc_attr( (string) max( 0, NumberUtil::round( abs( $item->get_quantity() ) - abs( $refunded_qty ), WC_ROUNDING_PRECISION ) ) ); ?>" autocomplete="off" name="refund_order_item_qty[<?php echo absint( $item_id ); ?>]" placeholder="0" size="4" class="refund_order_item_qty" data-can-restock="<?php echo esc_attr( wc_bool_to_string( $can_restock ) ); ?>" />
diff --git a/plugins/woocommerce/src/Internal/Admin/Orders/ItemQuantityLimits.php b/plugins/woocommerce/src/Internal/Admin/Orders/ItemQuantityLimits.php
index 1e25c65f44e..da3545372b1 100644
--- a/plugins/woocommerce/src/Internal/Admin/Orders/ItemQuantityLimits.php
+++ b/plugins/woocommerce/src/Internal/Admin/Orders/ItemQuantityLimits.php
@@ -13,9 +13,10 @@ use WC_Product;
  *
  * The admin order editor renders quantity inputs with a minimum of 0, so
  * merchants cannot enter negative quantities. Orders created through the
- * REST API or by extensions may already contain negative quantities, so for
- * existing items the minimum is floored at the stored quantity to keep those
- * orders editable.
+ * REST API or by extensions may already contain negative quantities, or
+ * quantities that do not match the product's step. For existing items the
+ * minimum is floored at the stored quantity and the step is dropped when the
+ * stored quantity does not match it, to keep those orders editable.
  *
  * Covers product line items only: fee and shipping lines have no quantity
  * input in the admin editor, and posted quantities for them are ignored.
@@ -49,6 +50,43 @@ class ItemQuantityLimits {
 		return is_numeric( $min ) ? (string) $min : $default;
 	}

+	/**
+	 * Get the step for an existing order item's quantity input in the admin editor.
+	 *
+	 * The input is hidden until the line is edited, so a stored quantity that does not
+	 * match the step would make the browser block the whole order form. Such an input,
+	 * and one with the default step of 1 on a store that allows decimal quantities,
+	 * has no step, so it accepts any quantity.
+	 *
+	 * @since 11.5.0
+	 * @param mixed  $step     The step returned by 'woocommerce_quantity_input_step_admin'.
+	 * @param float  $quantity The item's stored quantity.
+	 * @param string $min      The input's min attribute. Browsers count steps from it.
+	 * @return float|null The step, or null when the input accepts any quantity.
+	 */
+	public function get_quantity_input_step( $step, float $quantity, string $min ): ?float {
+		if ( is_string( $step ) && 'any' === strtolower( trim( $step ) ) ) {
+			return null;
+		}
+
+		// A callback can return anything; browsers read a non-numeric or non-positive step as 1.
+		$step = is_numeric( $step ) && (float) $step > 0 ? (float) $step : 1.0;
+
+		// A step of 1 only accepts whole numbers. It is the default step, so on a store that allows
+		// decimal quantities it is left out and the input accepts decimals too.
+		if ( abs( $step - 1.0 ) < 0.000001 && ! wc_is_stock_amount_integer() ) {
+			return null;
+		}
+
+		$steps_from_min = ( $quantity - (float) $min ) / $step;
+
+		if ( abs( $steps_from_min - round( $steps_from_min ) ) > 0.000001 ) {
+			return null;
+		}
+
+		return $step;
+	}
+
 	/**
 	 * Validate the quantity requested for a product being added to an order.
 	 *
diff --git a/plugins/woocommerce/tests/php/src/Internal/Admin/Orders/ItemQuantityLimitsTest.php b/plugins/woocommerce/tests/php/src/Internal/Admin/Orders/ItemQuantityLimitsTest.php
index 09f61c54625..dee8e7ad17d 100644
--- a/plugins/woocommerce/tests/php/src/Internal/Admin/Orders/ItemQuantityLimitsTest.php
+++ b/plugins/woocommerce/tests/php/src/Internal/Admin/Orders/ItemQuantityLimitsTest.php
@@ -219,4 +219,103 @@ class ItemQuantityLimitsTest extends WC_Unit_Test_Case {
 			)
 		);
 	}
+
+	/**
+	 * @testdox get_quantity_input_step keeps a step the stored quantity matches and returns null otherwise, on a store with whole quantities.
+	 *
+	 * @testWith [1, 2, "0", 1.0]
+	 *           [1, 2.5, "0", null]
+	 *           [2, 4, "0", 2.0]
+	 *           [2, 3, "0", null]
+	 *           [2, 3, "1", 2.0]
+	 *           [1, -5, "-5", 1.0]
+	 *           ["0.5", 1.5, "0", 0.5]
+	 *           [0.5, 1.3, "0", null]
+	 *           ["any", 2, "0", null]
+	 *           ["ANY", 2, "0", null]
+	 *           [0, 2, "0", 1.0]
+	 *           ["not a step", 2, "0", 1.0]
+	 *           [["not a step"], 2, "0", 1.0]
+	 *           [0.1, 0.3, "0", 0.1]
+	 *           [0.1, 0.7, "0", 0.1]
+	 *           [0.1, 1.2, "0", 0.1]
+	 *           [0.5, 0.9, "0", null]
+	 *           [0.01, 1234.56, "0", 0.01]
+	 *
+	 * @param mixed      $step     Step returned by the step filter.
+	 * @param float      $quantity Stored quantity.
+	 * @param string     $min      Min attribute.
+	 * @param float|null $expected Expected step.
+	 */
+	public function test_step_on_integer_store( $step, float $quantity, string $min, ?float $expected ): void {
+		$this->assertSame( $expected, $this->sut->get_quantity_input_step( $step, $quantity, $min ) );
+	}
+
+	/**
+	 * @testdox get_quantity_input_step drops the default step of 1 and keeps other steps, on a store with decimal quantities.
+	 *
+	 * @testWith [1, 2, "0", null]
+	 *           [1.0, 2.5, "0", null]
+	 *           [0.5, 1.5, "0", 0.5]
+	 *           [0.5, 1.3, "0", null]
+	 *           [2, 4, "0", 2.0]
+	 *
+	 * @param mixed      $step     Step returned by the step filter.
+	 * @param float      $quantity Stored quantity.
+	 * @param string     $min      Min attribute.
+	 * @param float|null $expected Expected step.
+	 */
+	public function test_step_on_decimal_store( $step, float $quantity, string $min, ?float $expected ): void {
+		remove_filter( 'woocommerce_stock_amount', 'intval' );
+		add_filter( 'woocommerce_stock_amount', 'floatval' );
+
+		$this->assertSame( $expected, $this->sut->get_quantity_input_step( $step, $quantity, $min ) );
+	}
+
+	/**
+	 * @testdox The order item template renders a quantity input the stored quantity is valid for.
+	 *
+	 * @testWith [false, null, 4, "1"]
+	 *           [true, null, 2.5, "any"]
+	 *           [false, 2, 4, "2"]
+	 *           [false, 2, 3, "any"]
+	 *
+	 * @param bool           $decimal_store Whether the store allows decimal quantities.
+	 * @param int|float|null $filtered_step Step returned by the admin step filter, or null to leave it alone.
+	 * @param float          $quantity      Stored quantity.
+	 * @param string         $expected      Expected step attribute.
+	 */
+	public function test_order_item_template_renders_step_the_stored_quantity_matches( bool $decimal_store, $filtered_step, float $quantity, string $expected ): void {
+		if ( $decimal_store ) {
+			remove_filter( 'woocommerce_stock_amount', 'intval' );
+			add_filter( 'woocommerce_stock_amount', 'floatval' );
+		}
+		if ( null !== $filtered_step ) {
+			add_filter(
+				'woocommerce_quantity_input_step_admin',
+				function () use ( $filtered_step ) {
+					return $filtered_step;
+				}
+			);
+		}
+
+		$order = wc_create_order();
+		$order->add_product( \WC_Helper_Product::create_simple_product(), $quantity );
+		$order->save();
+		$items = array_values( $order->get_items() );
+		$item  = $items[0];
+
+		$item_id     = $item->get_id();
+		$order_taxes = array();
+		ob_start();
+		include WC_ABSPATH . 'includes/admin/meta-boxes/views/html-order-item.php';
+		$html = ob_get_clean();
+
+		$this->assertSame(
+			1,
+			preg_match( '/<input type="number" step="([^"]*)"[^>]*name="order_item_qty\[' . $item_id . '\]"/', $html, $matches ),
+			'The quantity input should be rendered.'
+		);
+		$this->assertSame( $expected, $matches[1] );
+	}
 }