From 1bceaf2c1261051c501b43fb8eee18f832f7d5d3 Mon Sep 17 00:00:00 2001 From: kermage Date: Fri, 1 Aug 2025 22:07:53 +0800 Subject: [PATCH 1/3] always calculate; default quantity of 1 --- includes/data/mutation/class-order-mutation.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/includes/data/mutation/class-order-mutation.php b/includes/data/mutation/class-order-mutation.php index 53b89ceb..1eddf0c5 100644 --- a/includes/data/mutation/class-order-mutation.php +++ b/includes/data/mutation/class-order-mutation.php @@ -334,7 +334,7 @@ protected static function map_input_to_item( &$item, $input, $type ) { } // Calculate to subtotal/total for line items. - if ( isset( $args['quantity'] ) ) { + if ( empty( $args['subtotal'] ) || empty( $args['total'] ) ) { $product = ( ! empty( $item['product_id'] ) ) ? wc_get_product( $item['product_id'] ) : wc_get_product( self::get_product_id( $args ) ); @@ -342,7 +342,7 @@ protected static function map_input_to_item( &$item, $input, $type ) { throw new \Exception( __( 'Failed to retrieve product connected to order item.', 'wp-graphql-woocommerce' ) ); } - $total = wc_get_price_excluding_tax( $product, [ 'qty' => $args['quantity'] ] ); + $total = wc_get_price_excluding_tax( $product, [ 'qty' => $args['quantity'] ?? 1 ] ); $args['subtotal'] = ! empty( $args['subtotal'] ) ? $args['subtotal'] : $total; $args['total'] = ! empty( $args['total'] ) ? $args['total'] : $total; } From eaee8f1b5659cbca7221208d05eadced4d24d27a Mon Sep 17 00:00:00 2001 From: kermage Date: Fri, 1 Aug 2025 22:19:02 +0800 Subject: [PATCH 2/3] fill the name; complete order item data --- includes/data/mutation/class-order-mutation.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/includes/data/mutation/class-order-mutation.php b/includes/data/mutation/class-order-mutation.php index 1eddf0c5..f58ae1b7 100644 --- a/includes/data/mutation/class-order-mutation.php +++ b/includes/data/mutation/class-order-mutation.php @@ -333,8 +333,9 @@ protected static function map_input_to_item( &$item, $input, $type ) { } } - // Calculate to subtotal/total for line items. - if ( empty( $args['subtotal'] ) || empty( $args['total'] ) ) { + // If subtotal or total is not provided, calculate it based on the product price and quantity. + // If name is not provided, use the actual product name. + if ( empty( $args['subtotal'] ) || empty( $args['total'] ) || empty( $args['name'] ) ) { $product = ( ! empty( $item['product_id'] ) ) ? wc_get_product( $item['product_id'] ) : wc_get_product( self::get_product_id( $args ) ); @@ -345,6 +346,7 @@ protected static function map_input_to_item( &$item, $input, $type ) { $total = wc_get_price_excluding_tax( $product, [ 'qty' => $args['quantity'] ?? 1 ] ); $args['subtotal'] = ! empty( $args['subtotal'] ) ? $args['subtotal'] : $total; $args['total'] = ! empty( $args['total'] ) ? $args['total'] : $total; + $args['name'] = ! empty( $args['name'] ) ? $args['name'] : $product->get_name(); } // Set item props. From 130eea6084b031af52b65bce7e606f102d669cef Mon Sep 17 00:00:00 2001 From: kermage Date: Fri, 1 Aug 2025 22:58:14 +0800 Subject: [PATCH 3/3] dont introduce new requirement; leave as is --- includes/data/mutation/class-order-mutation.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/includes/data/mutation/class-order-mutation.php b/includes/data/mutation/class-order-mutation.php index f58ae1b7..68c8b903 100644 --- a/includes/data/mutation/class-order-mutation.php +++ b/includes/data/mutation/class-order-mutation.php @@ -335,7 +335,8 @@ protected static function map_input_to_item( &$item, $input, $type ) { // If subtotal or total is not provided, calculate it based on the product price and quantity. // If name is not provided, use the actual product name. - if ( empty( $args['subtotal'] ) || empty( $args['total'] ) || empty( $args['name'] ) ) { + // ONLY IF $args is not empty. + if ( ! empty( $args ) && ( empty( $args['subtotal'] ) || empty( $args['total'] ) || empty( $args['name'] ) ) ) { $product = ( ! empty( $item['product_id'] ) ) ? wc_get_product( $item['product_id'] ) : wc_get_product( self::get_product_id( $args ) );