Skip to content

Commit

Permalink
fix: Customer order connection args priority fixed. (#698)
Browse files Browse the repository at this point in the history
  • Loading branch information
kidunot89 authored Jan 25, 2023
1 parent c083fa5 commit 30b34aa
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 14 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@
"/codeception.yml",
"/composer.json",
"/composer.lock",
"/docker-composer.yml",
"/docker-compose.yml",
"/Dockerfile",
"/netlify.toml",
"/README.md"
Expand Down
41 changes: 32 additions & 9 deletions includes/connection/class-orders.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public static function register_connections() {
'fromType' => 'Customer',
'fromFieldName' => 'orders',
'resolve' => function( $source, array $args, AppContext $context, ResolveInfo $info ) {
$resolver = new PostObjectConnectionResolver( $source, $args, $context, $info, 'shop_order' );
$resolver = new PostObjectConnectionResolver( $source, $args, $context, $info, wc_get_order_types( 'view-orders' ) );

return self::get_customer_order_connection( $resolver, $source );
},
Expand Down Expand Up @@ -113,14 +113,37 @@ private static function get_customer_order_connection( $resolver, $customer ) {
return [];
}

// If the querying user has a "billing email" set filter orders by user's billing email, otherwise filter by user's ID.
$meta_key = ! empty( $customer->get_billing_email() ) ? '_billing_email' : '_customer_user';
$meta_value = ! empty( $customer->get_billing_email() )
? $customer->get_billing_email()
: $customer->get_id();
$resolver->set_query_arg( 'meta_key', $meta_key );
$resolver->set_query_arg( 'meta_value', $meta_value );
$meta_query = false;
if ( ! empty ( $customer->get_id() ) ) {
$meta_query = [
[
'key' => '_customer_user',
'value' => $customer->get_id(),
'compare' => '=',
],
];
} else if ( ! empty( $customer->get_billing_email() ) ) {
$meta_query = [
'relation' => 'AND',
[
'key' => '_billing_email',
'value' => $customer->get_billing_email(),
'compare' => '=',
],
[
'key' => '_customer_user',
'value' => 0,
'compare' => '=',
]
];
}

// Bail if needed info not found on customer object.
if ( false === $meta_query ) {
return [];
}

$resolver->set_query_arg( 'meta_query', $meta_query );
return $resolver->get_connection();
}

Expand Down Expand Up @@ -342,7 +365,7 @@ public static function map_input_fields_to_wp_query( $query_args, $where_args, $

// Process order meta inputs.
$metas = [ 'customerId', 'customersIn' ];
$meta_query = [];
$meta_query = isset( $query_args['meta_query'] ) ? $query_args['meta_query'] : [];
foreach ( $metas as $field ) {
if ( isset( $query_args[ $field ] ) ) {
$value = $query_args[ $field ];
Expand Down
15 changes: 11 additions & 4 deletions tests/_support/Factory/OrderFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,11 @@ public function create_object( $args ) {
throw new \Exception( $order->get_error_message( $args->get_error_code() ) );
}

// Set meta data.
if ( ! empty( $args['meta_data'] ) ) {
$order->set_meta_data( $args['meta_data'] );
// Set props.
foreach ( $args as $key => $value ) {
if ( is_callable( [ $order, "set_{$key}" ] ) ) {
$order->{"set_{$key}"}( $value );
}
}

return $order->save();
Expand Down Expand Up @@ -339,8 +341,10 @@ public function add_tax( $order, $args = [], $save = true ) {

public function set_to_customer_billing_address( $order, $customer, $save = true ) {
$order = \wc_get_order( $order );
if ( ! $customer ) {
return $order;
}
$customer = new \WC_Customer( $customer );

// Set billing address
$order->set_billing_first_name( $customer->get_first_name() );
$order->set_billing_last_name( $customer->get_last_name() );
Expand All @@ -363,6 +367,9 @@ public function set_to_customer_billing_address( $order, $customer, $save = true

public function set_to_customer_shipping_address( $order, $customer, $save = true ) {
$order = \wc_get_order( $order );
if ( ! $customer ) {
return $order;
}
$customer = new \WC_Customer( $customer );

// Set shipping address
Expand Down
24 changes: 24 additions & 0 deletions tests/wpunit/CustomerQueriesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -405,13 +405,23 @@ public function testCustomersQueryAndWhereArgs() {

public function testCustomerToOrdersConnection() {
$new_customer_id = $this->factory->customer->create();

$order_1 = $this->factory->order->createNew(
[ 'customer_id' => $this->customer ]
);
$order_2 = $this->factory->order->createNew(
[ 'customer_id' => $new_customer_id ]
);

$guest_customer = new \WC_Customer();
$guest_customer->set_billing_email( 'test@test.com' );
$order_3 = $this->factory->order->createNew(
[
'customer_id' => $guest_customer->get_id(),
'billing_email' => $guest_customer->get_billing_email(),
]
);

$query = '
query {
customer {
Expand All @@ -436,5 +446,19 @@ public function testCustomerToOrdersConnection() {
];

$this->assertQuerySuccessful( $response, $expected );

/**
* Assertion Two
*
* Query for a guest's orders.
*/
$this->loginAs( 0 );
WC()->customer = $guest_customer;
$response = $this->graphql( compact( 'query' ) );
$expected = [
$this->expectedField( 'customer.orders.nodes.#.databaseId', $order_3 ),
];

$this->assertQuerySuccessful( $response, $expected );
}
}

0 comments on commit 30b34aa

Please sign in to comment.