Commit 18af39174dc for woocommerce
commit 18af39174dc5877934f5cb41ab1618851b0b3a1c
Author: Luis Felipe Zaguini <26530524+zaguiini@users.noreply.github.com>
Date: Mon Nov 30 15:15:51 2026 -0300
Fire woocommerce_new_order_item with the real order ID in order-create (#71611)
* Fire woocommerce_new_order_item with the real order ID in order-create
The order-create ability added each product line with
WC_Order::add_product() before the new order was saved. That method
saves the line at once, so woocommerce_new_order_item fired with order
ID 0, and a listener could not load the order.
The ability now attaches the lines to the order and lets the order save
write them, as the REST API does. The lines carry the same values as
before. order-update is unchanged, because its order already has an ID.
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
* Keep the default of a line value that the line refuses in order-create
set_product() throws when a product reports a tax class that does not
exist, for example through a filter. WC_Order::add_product() sets the
line values with set_props(), which keeps the default of a refused
value and creates the line. order-create now sets the product through
set_props() too, so it behaves the same without a list of its own.
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
* Give a new order-create line its order before the backorder check
set_backorder_meta() reads the product through get_product(), which passes
the line's order to woocommerce_get_product_from_item. Checkout and the admin
set the order on the line first; order-create now does the same.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
---------
Co-authored-by: Claude Fable 5.2 <noreply@anthropic.com>
diff --git a/plugins/woocommerce/changelog/wooai-1863-order-create-line-order-id b/plugins/woocommerce/changelog/wooai-1863-order-create-line-order-id
new file mode 100644
index 00000000000..1b1393758d3
--- /dev/null
+++ b/plugins/woocommerce/changelog/wooai-1863-order-create-line-order-id
@@ -0,0 +1,3 @@
+Significance: patch
+Type: fix
+Comment: Fix the order ID that a hook gets in the order-create ability, which is new in 11.4 and not released yet.
diff --git a/plugins/woocommerce/src/Internal/Abilities/Domain/OrderCreate.php b/plugins/woocommerce/src/Internal/Abilities/Domain/OrderCreate.php
index 85be72ae869..f380e31083c 100644
--- a/plugins/woocommerce/src/Internal/Abilities/Domain/OrderCreate.php
+++ b/plugins/woocommerce/src/Internal/Abilities/Domain/OrderCreate.php
@@ -199,20 +199,14 @@ class OrderCreate extends ActionableAbility {
* @return null|\WP_Error
*/
public function save( $subject ) {
- $added = $this->add_queued_products( $subject );
+ $this->attach_queued_products( $subject );
$saved = parent::save( $subject );
if ( is_wp_error( $saved ) ) {
- foreach ( $added as $item ) {
- $item->delete();
- }
return $saved;
}
$recalculated = $this->recalculate_saved_order( $subject );
if ( is_wp_error( $recalculated ) ) {
- foreach ( $added as $item ) {
- $item->delete();
- }
return $recalculated;
}
@@ -228,6 +222,38 @@ class OrderCreate extends ActionableAbility {
);
}
+ /**
+ * Attach the product lines that add_product_line() left for the save, with the values that
+ * WC_Order::add_product() gives a line. The order save writes them, so each line is stored with the order ID.
+ * set_props() keeps the default of a value that the line refuses, such as a tax class that does not exist.
+ *
+ * @param \WC_Order $order New order.
+ */
+ private function attach_queued_products( \WC_Order $order ): void {
+ foreach ( $this->products_to_add as list( $product, $quantity ) ) {
+ $total = wc_get_price_excluding_tax(
+ $product,
+ array(
+ 'qty' => $quantity,
+ 'order' => $order,
+ )
+ );
+
+ $item = new \WC_Order_Item_Product();
+ $item->set_props(
+ array(
+ 'product' => $product,
+ 'quantity' => $quantity,
+ 'subtotal' => $total,
+ 'total' => $total,
+ 'order' => $order,
+ )
+ );
+ $item->set_backorder_meta();
+ $order->add_item( $item );
+ }
+ }
+
/**
* The ability output for the saved order. It includes the lines, so the client gets their IDs.
*
diff --git a/plugins/woocommerce/src/Internal/Abilities/Domain/Traits/OrderEditTrait.php b/plugins/woocommerce/src/Internal/Abilities/Domain/Traits/OrderEditTrait.php
index 00fbc877d83..2ac51a1de3c 100644
--- a/plugins/woocommerce/src/Internal/Abilities/Domain/Traits/OrderEditTrait.php
+++ b/plugins/woocommerce/src/Internal/Abilities/Domain/Traits/OrderEditTrait.php
@@ -37,7 +37,8 @@ trait OrderEditTrait {
private ?string $recalculate_after_save = null;
/**
- * Product lines for add_queued_products(), which adds them with WC_Order::add_product().
+ * Product lines that wait for the save. order-update adds them with WC_Order::add_product(), and order-create
+ * attaches them to the new order.
*
* @var array<int, array{0: \WC_Product, 1: int}>
*/
@@ -226,7 +227,7 @@ trait OrderEditTrait {
}
/**
- * Check a new product line. The line waits for add_queued_products(), which adds it on save.
+ * Check a new product line. The line waits for the save of the ability.
*
* @param \WC_Order $order Order.
* @param array $line Product line input.
diff --git a/plugins/woocommerce/tests/php/src/Internal/Abilities/Domain/OrderCreateTest.php b/plugins/woocommerce/tests/php/src/Internal/Abilities/Domain/OrderCreateTest.php
index a7fe315c8f8..6d6386eea95 100644
--- a/plugins/woocommerce/tests/php/src/Internal/Abilities/Domain/OrderCreateTest.php
+++ b/plugins/woocommerce/tests/php/src/Internal/Abilities/Domain/OrderCreateTest.php
@@ -261,6 +261,81 @@ class OrderCreateTest extends WC_Unit_Test_Case {
$this->assertNull( $order->get_date_paid() );
}
+ /**
+ * @testdox Should fire woocommerce_new_order with the product lines on the order, and woocommerce_new_order_item with the order ID.
+ */
+ public function test_fires_the_new_order_hooks_with_the_lines_and_the_order_id(): void {
+ $product = WC_Helper_Product::create_simple_product();
+ $lines_on_new_order = null;
+ $line_order_ids = array();
+ add_action(
+ 'woocommerce_new_order',
+ static function ( $order_id, $order ) use ( &$lines_on_new_order ): void {
+ $lines_on_new_order = count( $order->get_items() );
+ },
+ 10,
+ 2
+ );
+ add_action(
+ 'woocommerce_new_order_item',
+ static function ( $item_id, $item, $order_id ) use ( &$line_order_ids ): void {
+ if ( $item instanceof \WC_Order_Item_Product ) {
+ $line_order_ids[] = $order_id;
+ }
+ },
+ 10,
+ 3
+ );
+
+ $result = $this->execute(
+ array( 'line_items' => array( array( 'product_id' => $product->get_id() ) ) )
+ );
+
+ $this->assertSame( 1, $lines_on_new_order, 'A listener of the new order should see its product line.' );
+ $this->assertSame( array( $result['order']['id'] ), $line_order_ids, 'A listener of the new line should get the ID of its order.' );
+ }
+
+ /**
+ * @testdox Should pass the order to woocommerce_get_product_from_item while it builds a product line.
+ */
+ public function test_passes_the_order_to_the_product_from_item_filter(): void {
+ $product = WC_Helper_Product::create_simple_product();
+ $orders = array();
+ add_filter(
+ 'woocommerce_get_product_from_item',
+ static function ( $line_product, $item, $order ) use ( &$orders ) {
+ unset( $item );
+ $orders[] = $order;
+ return $line_product;
+ },
+ 10,
+ 3
+ );
+
+ $this->execute(
+ array( 'line_items' => array( array( 'product_id' => $product->get_id() ) ) )
+ );
+
+ $this->assertNotEmpty( $orders );
+ $this->assertContainsOnlyInstancesOf( \WC_Order::class, $orders, 'A listener of the filter should get the order, as checkout and the admin give it.' );
+ }
+
+ /**
+ * @testdox Should create the order with the default tax class on a line whose product reports a tax class that does not exist.
+ */
+ public function test_creates_the_order_when_a_product_reports_an_unknown_tax_class(): void {
+ $product = WC_Helper_Product::create_simple_product();
+ add_filter( 'woocommerce_product_get_tax_class', static fn() => 'no-such-class' );
+
+ $result = $this->execute(
+ array( 'line_items' => array( array( 'product_id' => $product->get_id() ) ) )
+ );
+
+ $line = array_values( wc_get_order( $result['order']['id'] )->get_items() )[0];
+ $this->assertSame( '', $line->get_tax_class(), 'The line should keep the default tax class, as WC_Order::add_product() does.' );
+ $this->assertSame( $product->get_name(), $line->get_name() );
+ }
+
/**
* @testdox Should roll back the saved order when its status cannot be set with the feature off.
*/