Skip to content

Conversation

kermage
Copy link

@kermage kermage commented Aug 1, 2025

What does this implement/fix? Explain your changes.

Minimal input to createOrder mutation: with only productId in lineItems.
Added only the necessary checks and explicitly supplied default values.

Does this close any currently open issues?

Yes, issue #946

@kidunot89 kidunot89 requested review from kidunot89 and Copilot August 19, 2025 21:09
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR implements automatic data population for order line items when only a product ID is provided to the createOrder mutation. It automatically retrieves and sets the product name, subtotal, and total values based on the product's data when these fields are not explicitly provided.

Key changes:

  • Enhanced input validation to check for missing subtotal, total, or name fields
  • Added automatic product data retrieval when required fields are empty
  • Set default quantity to 1 when not provided

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

// 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
Preview

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.

$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
Preview

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant