-
-
Notifications
You must be signed in to change notification settings - Fork 140
Automatically get and set data from the specified product ID #947
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Conversation
There was a problem hiding this 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'] ) ) ) { |
There was a problem hiding this comment.
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.
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(); |
There was a problem hiding this comment.
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.
$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.
What does this implement/fix? Explain your changes.
Minimal input to
createOrder
mutation: with onlyproductId
inlineItems
.Added only the necessary checks and explicitly supplied default values.
Does this close any currently open issues?
Yes, issue #946