Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions includes/data/mutation/class-order-mutation.php
Original file line number Diff line number Diff line change
Expand Up @@ -333,18 +333,21 @@ protected static function map_input_to_item( &$item, $input, $type ) {
}
}

// Calculate to subtotal/total for line items.
if ( isset( $args['quantity'] ) ) {
// 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.
// ONLY IF $args is not empty.
if ( ! empty( $args ) && ( empty( $args['subtotal'] ) || empty( $args['total'] ) || empty( $args['name'] ) ) ) {
Copy link

Copilot AI Aug 19, 2025

Choose a reason for hiding this comment

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

The condition uses empty() which will treat '0' values as empty. For subtotal and total, '0' is a valid value that should not trigger automatic calculation. Use !isset() or explicit null checks instead.

Suggested change
if ( ! empty( $args ) && ( empty( $args['subtotal'] ) || empty( $args['total'] ) || empty( $args['name'] ) ) ) {
if (
! empty( $args ) &&
( ! isset( $args['subtotal'] ) || ! isset( $args['total'] ) || empty( $args['name'] ) )
) {

Copilot uses AI. Check for mistakes.

$product = ( ! empty( $item['product_id'] ) )
? wc_get_product( $item['product_id'] )
: wc_get_product( self::get_product_id( $args ) );
if ( ! is_object( $product ) ) {
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;
$args['name'] = ! empty( $args['name'] ) ? $args['name'] : $product->get_name();
Copy link

Copilot AI Aug 19, 2025

Choose a reason for hiding this comment

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

Inconsistent use of empty() for checking existing values. Line 347 uses null coalescing operator for quantity (correct), but lines 348-349 use empty() which will overwrite legitimate '0' values. Use isset() or null coalescing operator for consistency.

Suggested change
$args['name'] = ! empty( $args['name'] ) ? $args['name'] : $product->get_name();
$args['subtotal'] = $args['subtotal'] ?? $total;
$args['total'] = $args['total'] ?? $total;
$args['name'] = $args['name'] ?? $product->get_name();

Copilot uses AI. Check for mistakes.

}

// Set item props.
Expand Down
Loading