Skip to content

Commit 16834b5

Browse files
authored
Merge pull request #123 from kidunot89/new-type/metadata
MetaData type and queries
2 parents 142c482 + 8025de7 commit 16834b5

File tree

16 files changed

+965
-13
lines changed

16 files changed

+965
-13
lines changed

includes/class-type-registry.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ public static function graphql_register_types() {
6868
\WPGraphQL\Extensions\WooCommerce\Type\WPObject\Cart_Type::register();
6969
\WPGraphQL\Extensions\WooCommerce\Type\WPObject\Variation_Attribute_Type::register();
7070
\WPGraphQL\Extensions\WooCommerce\Type\WPObject\Payment_Gateway_Type::register();
71+
\WPGraphQL\Extensions\WooCommerce\Type\WPObject\Meta_Data_Type::register();
7172

7273
// Object fields.
7374
\WPGraphQL\Extensions\WooCommerce\Type\WPObject\Product_Category_Type::register_fields();

includes/model/class-customer.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,18 @@ public function __construct( $id ) {
4141
parent::__construct( $restricted_cap, $allowed_restricted_fields, $id );
4242
}
4343

44+
/**
45+
* Forwards function calls to WC_Data sub-class instance.
46+
*
47+
* @param string $method - function name.
48+
* @param array $args - function call arguments.
49+
*
50+
* @return mixed
51+
*/
52+
public function __call( $method, $args ) {
53+
return $this->data->$method( ...$args );
54+
}
55+
4456
/**
4557
* Initializes the Customer field resolvers
4658
*

includes/model/class-order-item.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,18 @@ public function __construct( $item ) {
6060
parent::__construct( $restricted_cap, $allowed_restricted_fields, $author_id );
6161
}
6262

63+
/**
64+
* Forwards function calls to WC_Data sub-class instance.
65+
*
66+
* @param string $method - function name.
67+
* @param array $args - function call arguments.
68+
*
69+
* @return mixed
70+
*/
71+
public function __call( $method, $args ) {
72+
return $this->data->$method( ...$args );
73+
}
74+
6375
/**
6476
* Initializes the Order field resolvers
6577
*
Lines changed: 212 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
1+
<?php
2+
/**
3+
* WPObject Type - Order_Item_Type
4+
*
5+
* Registers MetaData type and queries
6+
*
7+
* @package \WPGraphQL\Extensions\WooCommerce\Type\WPObject
8+
* @since 0.0.2
9+
*/
10+
11+
namespace WPGraphQL\Extensions\WooCommerce\Type\WPObject;
12+
13+
use WPGraphQL\AppContext;
14+
use WPGraphQL\Extensions\WooCommerce\Data\Factory;
15+
16+
/**
17+
* Class Meta_Data_Type
18+
*/
19+
class Meta_Data_Type {
20+
/**
21+
* Register Order type and queries to the WPGraphQL schema
22+
*/
23+
public static function register() {
24+
register_graphql_object_type(
25+
'MetaData',
26+
array(
27+
'description' => __( 'Extra data defined on the WC object', 'wp-graphql-woocommerce' ),
28+
'fields' => array(
29+
'id' => array(
30+
'type' => array( 'non_null' => 'String' ),
31+
'description' => __( 'Meta ID.', 'wp-graphql-woocommerce' ),
32+
'resolve' => function ( $source ) {
33+
return ! empty( $source->id ) ? $source->id : null;
34+
},
35+
),
36+
'key' => array(
37+
'type' => array( 'non_null' => 'String' ),
38+
'description' => __( 'Meta key.', 'wp-graphql-woocommerce' ),
39+
'resolve' => function ( $source ) {
40+
return ! empty( $source->key ) ? $source->key : null;
41+
},
42+
),
43+
'value' => array(
44+
'type' => array( 'non_null' => 'String' ),
45+
'description' => __( 'Meta value.', 'wp-graphql-woocommerce' ),
46+
'resolve' => function ( $source ) {
47+
return ! empty( $source->value ) ? $source->value : null;
48+
},
49+
),
50+
),
51+
)
52+
);
53+
54+
// Register 'extraData' field on CartItem.
55+
register_graphql_field(
56+
'CartItem',
57+
'extraData',
58+
array(
59+
'type' => array( 'list_of' => 'MetaData' ),
60+
'description' => __( 'Object meta data', 'wp-graphql-woocommerce' ),
61+
'args' => array(
62+
'key' => array(
63+
'type' => 'String',
64+
'description' => __( 'Retrieve meta by key', 'wp-graphql-woocommerce' ),
65+
),
66+
'keysIn' => array(
67+
'type' => array( 'list_of' => 'String' ),
68+
'description' => __( 'Retrieve multiple metas by key', 'wp-graphql-woocommerce' ),
69+
),
70+
),
71+
'resolve' => function( $source, array $args ) {
72+
// Check if "key" argument set and assigns to target "keys" array.
73+
if ( ! empty( $args['key'] ) && ! empty( $source[ $args['key'] ] ) ) {
74+
$keys = array( $args['key'] );
75+
} elseif ( ! empty( $args['keysIn'] ) ) { // Check if "keysIn" argument set and assigns to target "keys" array.
76+
$keys = array();
77+
foreach ( $args['keysIn'] as $key ) {
78+
if ( ! empty( $source[ $key ] ) ) {
79+
$keys[] = $key;
80+
}
81+
}
82+
} else { // If no arguments set, all extra data keys are assigns to target "keys" array.
83+
$keys = array_diff(
84+
array_keys( $source ),
85+
array(
86+
'key',
87+
'product_id',
88+
'variation_id',
89+
'variation',
90+
'quantity',
91+
'data',
92+
'data_hash',
93+
'line_tax_data',
94+
'line_subtotal',
95+
'line_subtotal_tax',
96+
'line_total',
97+
'line_tax',
98+
)
99+
);
100+
}
101+
// Create meta ID prefix.
102+
$id_prefix = apply_filters( 'cart_meta_id_prefix', 'cart_' );
103+
104+
// Format meta data for resolution.
105+
$data = array();
106+
foreach ( $keys as $key ) {
107+
$data[] = (object) array(
108+
'id' => "{$id_prefix}_{$key}",
109+
'key' => $key,
110+
'value' => $source[ $key ],
111+
);
112+
}
113+
114+
return $data;
115+
},
116+
)
117+
);
118+
119+
// Register 'metaData' field on WC CRUD types.
120+
$types = array(
121+
'Coupon',
122+
'Customer',
123+
'CouponLine',
124+
'LineItem',
125+
'FeeLine',
126+
'Order',
127+
'Product',
128+
'ProductVariation',
129+
'Refund',
130+
'ShippingLine',
131+
'TaxLine',
132+
);
133+
134+
foreach ( $types as $type ) {
135+
register_graphql_field(
136+
$type,
137+
'metaData',
138+
array(
139+
'type' => array( 'list_of' => 'MetaData' ),
140+
'description' => __( 'Object meta data', 'wp-graphql-woocommerce' ),
141+
'args' => array(
142+
'key' => array(
143+
'type' => 'String',
144+
'description' => __( 'Retrieve meta by key', 'wp-graphql-woocommerce' ),
145+
),
146+
'keysIn' => array(
147+
'type' => array( 'list_of' => 'String' ),
148+
'description' => __( 'Retrieve multiple metas by key', 'wp-graphql-woocommerce' ),
149+
),
150+
'multiple' => array(
151+
'type' => 'Boolean',
152+
'description' => __( 'Retrieve meta with matching keys', 'wp-graphql-woocommerce' ),
153+
),
154+
),
155+
'resolve' => function( $source, array $args ) {
156+
// Set unique flag.
157+
$single = ! empty( $args['multiple'] ) ? ! $args['multiple'] : true;
158+
159+
// Check "key" argument and format meta_data objects.
160+
if ( ! empty( $args['key'] ) && $source->meta_exists( $args['key'] ) ) {
161+
$data = $source->get_meta( $args['key'], $single );
162+
if ( ! is_array( $data ) ) {
163+
$data = array_filter(
164+
$source->get_meta_data(),
165+
function( $meta ) use ( $data ) {
166+
return $meta->value === $data;
167+
}
168+
);
169+
}
170+
} elseif ( ! empty( $args['keysIn'] ) ) { // Check "keysIn" argument and format meta_data objects.
171+
$keys = $args['keysIn'];
172+
173+
$found = array();
174+
$data = array_filter(
175+
$source->get_meta_data(),
176+
function( $meta ) use ( $keys, $single, &$found ) {
177+
if ( in_array( $meta->key, $keys, true ) ) {
178+
if ( $single ) {
179+
if ( ! in_array( $meta->key, $found, true ) ) {
180+
$found[] = $meta->key;
181+
return true;
182+
}
183+
return false;
184+
}
185+
return true;
186+
}
187+
}
188+
);
189+
} else { // If no arguments set return all meta (in accordance with unique flag).
190+
$found = array();
191+
$data = array_filter(
192+
$source->get_meta_data(),
193+
function( $meta ) use ( $single, &$found ) {
194+
if ( $single ) {
195+
if ( ! in_array( $meta->key, $found, true ) ) {
196+
$found[] = $meta->key;
197+
return true;
198+
}
199+
return false;
200+
}
201+
return true;
202+
}
203+
);
204+
}
205+
206+
return ! empty( $data ) ? $data : null;
207+
},
208+
)
209+
);
210+
}
211+
}
212+
}

tests/_support/Helper/crud-helpers/coupon.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ public function create( $args = array(), $save = true ) {
3232
)
3333
);
3434

35+
// Set meta data.
36+
if ( ! empty( $args['meta_data'] ) ) {
37+
$coupon->set_meta_data( $args['meta_data'] );
38+
}
39+
3540
// Return instance in not saving.
3641
if( ! $save ) {
3742
return $coupon;

tests/_support/Helper/crud-helpers/customer.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public function create( $args = array() ) {
2828
$email = $this->dummy->email();
2929
$phone = $this->dummy->telephone();
3030

31+
// Set data.
3132
$customer->set_props(
3233
array_merge(
3334
array(
@@ -67,6 +68,11 @@ public function create( $args = array() ) {
6768
)
6869
);
6970

71+
// Set meta data.
72+
if ( ! empty( $args['meta_data'] ) ) {
73+
$customer->set_meta_data( $args['meta_data'] );
74+
}
75+
7076
return absint( $customer->save() );
7177
}
7278

tests/_support/Helper/crud-helpers/order-item.php

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,7 @@ public function add_coupon( $order, $coupon_id = 0, $save = true ) {
4848

4949
public function add_fee( $order, $args = array(), $save = true ) {
5050
// Retrieve order.
51-
if ( ! is_a( $order, WC_Order::class ) ) {
52-
$order = new WC_Order( $order );
53-
}
51+
$order = new WC_Order( $order );
5452

5553
// Get thre customer country code.
5654
$country_code = $order->get_shipping_country();
@@ -76,6 +74,12 @@ public function add_fee( $order, $args = array(), $save = true ) {
7674
if ( ! empty( $args ) ) {
7775
$item->set_props( $args );
7876
}
77+
78+
// Set meta data.
79+
if ( ! empty( $args['meta_data'] ) ) {
80+
$item->set_meta_data( $args['meta_data'] );
81+
}
82+
7983
// Calculating Fee taxes
8084
$item->calculate_taxes( $calculate_tax_for );
8185

@@ -113,6 +117,11 @@ public function add_shipping( $order, $args = array(), $save = true ) {
113117
)
114118
);
115119

120+
// Set meta data.
121+
if ( ! empty( $args['meta_data'] ) ) {
122+
$item->set_meta_data( $args['meta_data'] );
123+
}
124+
116125
$item_id = $item->save();
117126

118127
$order->add_item( $item );
@@ -151,6 +160,12 @@ public function add_tax( $order, $args = array(), $save = true ) {
151160
$args
152161
)
153162
);
163+
164+
// Set meta data.
165+
if ( ! empty( $args['meta_data'] ) ) {
166+
$item->set_meta_data( $args['meta_data'] );
167+
}
168+
154169
$item->save();
155170

156171
$order->add_item( $item );
@@ -189,6 +204,11 @@ public function add_line_item( $order, $args = array(), $save = true ) {
189204
)
190205
);
191206

207+
// Set meta data.
208+
if ( ! empty( $args['meta_data'] ) ) {
209+
$item->set_meta_data( $args['meta_data'] );
210+
}
211+
192212
$order->add_item( $item );
193213

194214
if ( ! $save ) {

tests/_support/Helper/crud-helpers/order.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,12 @@ public function create( $args = array(), $items = array() ) {
141141
$order->set_cart_tax( 0 );
142142
$order->set_shipping_tax( 0 );
143143
$order->set_total( 50 ); // 4 x $10 simple helper product
144-
144+
145+
// Set meta data.
146+
if ( ! empty( $args['meta_data'] ) ) {
147+
$order->set_meta_data( $args['meta_data'] );
148+
}
149+
145150
// Save and return ID.
146151
return $order->save();
147152
}

tests/_support/Helper/crud-helpers/product-variation.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ public function create( $product_id, $args = array() ) {
4040
)
4141
);
4242
$variation_1->set_attributes( array( 'pa_size' => 'small' ) );
43+
if ( ! empty( $args['meta_data'] ) ) {
44+
$variation_1->set_meta_data( $args['meta_data'] );
45+
}
4346

4447
// Create medium size variation with image
4548
$image_id = \wp_insert_post(
@@ -64,6 +67,9 @@ public function create( $product_id, $args = array() ) {
6467
)
6568
);
6669
$variation_2->set_attributes( array( 'pa_size' => 'medium' ) );
70+
if ( ! empty( $args['meta_data'] ) ) {
71+
$variation_2->set_meta_data( $args['meta_data'] );
72+
}
6773

6874
// Create large size variation
6975
$variation_3 = new WC_Product_Variation();
@@ -76,6 +82,9 @@ public function create( $product_id, $args = array() ) {
7682
)
7783
);
7884
$variation_3->set_attributes( array( 'pa_size' => 'large' ) );
85+
if ( ! empty( $args['meta_data'] ) ) {
86+
$variation_3->set_meta_data( $args['meta_data'] );
87+
}
7988

8089
return array(
8190
'variations' => array(

0 commit comments

Comments
 (0)