Skip to content

Commit b366aee

Browse files
committed
Refund connection resolver fixed
1 parent 8485fff commit b366aee

File tree

7 files changed

+181
-126
lines changed

7 files changed

+181
-126
lines changed

includes/data/connection/class-cart-item-connection-resolver.php

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,14 @@ public function should_execute() {
3535
* @return array
3636
*/
3737
public function get_query_args() {
38-
$query_args = array();
38+
$query_args = array( 'filters' => array() );
3939
if ( ! empty( $this->args['where'] ) ) {
4040
$where_args = $this->args['where'];
4141
if ( isset( $where_args['needsShipping'] ) ) {
4242
$needs_shipping = $where_args['needsShipping'];
43-
$query_args['filters'] = array();
4443
$query_args['filters'][] = function( $cart_item ) use ( $needs_shipping ) {
4544
$product = \WC()->product_factory->get_product( $cart_item['product_id'] );
46-
if ( $product ) {
47-
return $needs_shipping === (bool) $product->needs_shipping();
48-
}
45+
return $needs_shipping === (bool) $product->needs_shipping();
4946
};
5047
}
5148
}
@@ -72,13 +69,9 @@ public function get_query_args() {
7269
public function get_query() {
7370
$cart_items = array_values( $this->source->get_cart() );
7471

75-
if ( ! empty( $this->query_args['filters'] ) ) {
76-
if ( is_array( $this->query_args['filters'] ) ) {
77-
foreach ( $this->query_args['filters'] as $filter ) {
78-
$cart_items = array_filter( $cart_items, $filter );
79-
}
80-
} else {
81-
$cart_items = array_filter( $cart_items, $this->query_args['filters'] );
72+
if ( ! empty( $this->query_args['filters'] ) && is_array( $this->query_args['filters'] ) ) {
73+
foreach ( $this->query_args['filters'] as $filter ) {
74+
$cart_items = array_filter( $cart_items, $filter );
8275
}
8376
}
8477

includes/data/connection/class-product-connection-resolver.php

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -57,48 +57,12 @@ public function __construct( $source, $args, $context, $info ) {
5757
? 'product_variation'
5858
: 'product';
5959

60-
add_filter(
61-
'woocommerce_product_data_store_cpt_get_products_query',
62-
array( &$this, 'product_query_filter' ),
63-
10,
64-
2
65-
);
6660
/**
6761
* Call the parent construct to setup class data
6862
*/
6963
parent::__construct( $source, $args, $context, $info );
7064
}
7165

72-
/**
73-
* Applies price meta_query args to product query args
74-
*
75-
* @param array $wp_query_args - Formatted query args.
76-
* @param array $query_vars - Raw query args.
77-
*
78-
* @return array
79-
*/
80-
public function product_query_filter( $wp_query_args, $query_vars ) {
81-
if ( empty( $query_vars['meta_query'] ) ) {
82-
return $wp_query_args;
83-
}
84-
85-
$price_meta_query = array_filter(
86-
$query_vars['meta_query'],
87-
function ( $query ) {
88-
return ! empty( $query['key'] ) ? '_price' === $query['key'] : false;
89-
}
90-
);
91-
92-
if ( ! empty( $price_meta_query ) ) {
93-
$wp_query_args['meta_query'] = array_merge( // WPCS: slow query ok.
94-
$wp_query_args['meta_query'],
95-
$price_meta_query
96-
);
97-
}
98-
99-
return $wp_query_args;
100-
}
101-
10266
/**
10367
* Confirms the user has the privileges to query the products
10468
*

includes/data/connection/class-refund-connection-resolver.php

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@
1010

1111
namespace WPGraphQL\WooCommerce\Data\Connection;
1212

13-
use WPGraphQL\Data\Connection\AbstractConnectionResolver;
1413
use GraphQL\Type\Definition\ResolveInfo;
1514
use WPGraphQL\AppContext;
16-
use WPGraphQL\Extension\WooCommerce\Model\Customer;
17-
use WPGraphQL\Extension\WooCommerce\Model\Order;
15+
use WPGraphQL\Data\Connection\AbstractConnectionResolver;
16+
use WPGraphQL\WooCommerce\Model\Customer;
17+
use WPGraphQL\WooCommerce\Model\Order;
1818

1919
/**
2020
* Class Refund_Connection_Resolver
@@ -84,7 +84,7 @@ public function get_query_args() {
8484
/**
8585
* Collect the input_fields and sanitize them to prepare them for sending to the WP_Query
8686
*/
87-
$input_fields = [];
87+
$input_fields = array();
8888
if ( ! empty( $this->args['where'] ) ) {
8989
$input_fields = $this->sanitize_input_fields( $this->args['where'] );
9090
}
@@ -116,20 +116,19 @@ public function get_query_args() {
116116
switch ( true ) {
117117
case is_a( $this->source, Order::class ):
118118
if ( 'refunds' === $this->info->fieldName ) {
119-
unset( $query_args['post_parent__in'] );
120-
$query_args['post_parent'] = $this->source->ID;
119+
$query_args['parent'] = $this->source->ID;
121120
}
122121
break;
123122
case is_a( $this->source, Customer::class ):
124123
if ( 'refunds' === $this->info->fieldName ) {
125-
if ( ! empty( $args['meta_query'] ) ) {
126-
$args['meta_query'] = array(); // WPCS: slow query ok.
127-
}
128-
$args['meta_query'][] = array(
129-
'key' => '_customer_user',
130-
'value' => $this->source->ID,
131-
'type' => 'NUMERIC',
124+
$customer_orders = \wc_get_orders(
125+
array(
126+
'customer_id' => $this->source->ID,
127+
'no_rows_found' => true,
128+
'return' => 'ids',
129+
)
132130
);
131+
$query_args['post_parent__in'] = array_map( 'absint', $customer_orders );
133132
}
134133
break;
135134
default:

includes/data/loader/class-wc-post-crud-loader.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -142,12 +142,14 @@ function() use ( $post_type, $key ) {
142142
// Resolve post author for future capability checks.
143143
if ( 'shop_order' === $post_type ) {
144144
$customer_id = get_post_meta( $key, '_customer_user', true );
145-
$customer = Factory::resolve_customer( $customer_id, $this->context );
146-
return $customer->then(
147-
function () use ( $post_type, $key ) {
148-
return $this->resolve_model( $post_type, $key );
149-
}
150-
);
145+
if ( ! empty( $customer_id ) ) {
146+
$customer = Factory::resolve_customer( $customer_id, $this->context );
147+
return $customer->then(
148+
function () use ( $post_type, $key ) {
149+
return $this->resolve_model( $post_type, $key );
150+
}
151+
);
152+
}
151153
} elseif ( 'product_variation' === $post_type || 'shop_refund' === $post_type ) {
152154
$parent_id = get_post_field( 'post_parent', $key );
153155
$parent = Factory::resolve_crud_object( $parent_id, $this->context );

includes/type/object/class-order-type.php

Lines changed: 19 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -331,45 +331,19 @@ public static function register() {
331331
'type' => 'Order',
332332
'description' => __( 'A order object', 'wp-graphql-woocommerce' ),
333333
'args' => array(
334-
'id' => array(
335-
'type' => array(
336-
'non_null' => 'ID',
337-
),
334+
'id' => array(
335+
'type' => 'ID',
336+
'description' => __( 'Get the order by its global ID', 'wp-graphql-woocommerce' ),
337+
),
338+
'orderId' => array(
339+
'type' => 'Int',
340+
'description' => __( 'Get the order by its database ID', 'wp-graphql-woocommerce' ),
341+
),
342+
'orderKey' => array(
343+
'type' => 'String',
344+
'description' => __( 'Get the order by its order number', 'wp-graphql-woocommerce' ),
338345
),
339346
),
340-
'resolve' => function ( $source, array $args, AppContext $context, ResolveInfo $info ) {
341-
$id_components = Relay::fromGlobalId( $args['id'] );
342-
if ( ! isset( $id_components['id'] ) || ! absint( $id_components['id'] ) ) {
343-
throw new UserError( __( 'The ID input is invalid', 'wp-graphql-woocommerce' ) );
344-
}
345-
$order_id = absint( $id_components['id'] );
346-
return Factory::resolve_crud_object( $order_id, $context );
347-
},
348-
)
349-
);
350-
351-
$post_by_args = array(
352-
'id' => array(
353-
'type' => 'ID',
354-
'description' => __( 'Get the order by its global ID', 'wp-graphql-woocommerce' ),
355-
),
356-
'orderId' => array(
357-
'type' => 'Int',
358-
'description' => __( 'Get the order by its database ID', 'wp-graphql-woocommerce' ),
359-
),
360-
'orderKey' => array(
361-
'type' => 'String',
362-
'description' => __( 'Get the order by its order number', 'wp-graphql-woocommerce' ),
363-
),
364-
);
365-
366-
register_graphql_field(
367-
'RootQuery',
368-
'orderBy',
369-
array(
370-
'type' => 'Order',
371-
'description' => __( 'A order object', 'wp-graphql-woocommerce' ),
372-
'args' => $post_by_args,
373347
'resolve' => function ( $source, array $args, AppContext $context, ResolveInfo $info ) {
374348
$order_id = 0;
375349
if ( ! empty( $args['id'] ) ) {
@@ -384,12 +358,16 @@ public static function register() {
384358
$order_id = \wc_get_order_id_by_order_key( $args['orderKey'] );
385359
}
386360

387-
$order = Factory::resolve_crud_object( $order_id, $context );
388-
if ( get_post( $order_id )->post_type !== 'shop_order' ) {
389-
/* translators: not order found error message */
390-
throw new UserError( sprintf( __( 'No order exists with this id: %1$s' ), $order_id ) );
361+
if ( empty( $order_id ) ) {
362+
/* translators: %1$s: ID type, %2$s: ID value */
363+
throw new UserError( sprintf( __( 'No order ID was found corresponding to the %1$s: %2$s' ), $id_type, $product_id ) );
364+
} elseif ( get_post( $order_id )->post_type !== 'shop_order' ) {
365+
/* translators: %1$s: ID type, %2$s: ID value */
366+
throw new UserError( sprintf( __( 'No order exists with the %1$s: %2$s' ), $id_type, $product_id ) );
391367
}
392368

369+
$order = Factory::resolve_crud_object( $order_id, $context );
370+
393371
return $order;
394372
},
395373
)

tests/wpunit/OrderQueriesTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -141,12 +141,12 @@ public function testOrderQuery() {
141141
$this->assertEquals( $expected, $actual );
142142
}
143143

144-
public function testOrderByQueryAndArgs() {
144+
public function testOrderQueryAndArgs() {
145145
$id = Relay::toGlobalId( 'shop_order', $this->order );
146146

147147
$query = '
148148
query ($id: ID, $orderId: Int, $orderKey: String) {
149-
orderBy(id: $id, orderId: $orderId, orderKey: $orderKey) {
149+
order(id: $id, orderId: $orderId, orderKey: $orderKey) {
150150
id
151151
}
152152
}
@@ -159,7 +159,7 @@ public function testOrderByQueryAndArgs() {
159159
*/
160160
$variables = array( 'id' => $id );
161161
$actual = graphql( array( 'query' => $query, 'variables' => $variables ) );
162-
$expected = array( 'data' => array( 'orderBy' => array( 'id' => $id ) ) );
162+
$expected = array( 'data' => array( 'order' => array( 'id' => $id ) ) );
163163

164164
// use --debug flag to view.
165165
codecept_debug( $actual );
@@ -173,7 +173,7 @@ public function testOrderByQueryAndArgs() {
173173
*/
174174
$variables = array( 'orderId' => $this->order );
175175
$actual = graphql( array( 'query' => $query, 'variables' => $variables ) );
176-
$expected = array( 'data' => array( 'orderBy' => array( 'id' => $id ) ) );
176+
$expected = array( 'data' => array( 'order' => array( 'id' => $id ) ) );
177177

178178
// use --debug flag to view.
179179
codecept_debug( $actual );
@@ -187,7 +187,7 @@ public function testOrderByQueryAndArgs() {
187187
*/
188188
$variables = array( 'orderKey' => $this->order_helper->get_order_key( $this->order ) );
189189
$actual = graphql( array( 'query' => $query, 'variables' => $variables ) );
190-
$expected = array( 'data' => array( 'orderBy' => array( 'id' => $id ) ) );
190+
$expected = array( 'data' => array( 'order' => array( 'id' => $id ) ) );
191191

192192
// use --debug flag to view.
193193
codecept_debug( $actual );

0 commit comments

Comments
 (0)