Skip to content

Commit 8025de7

Browse files
committed
"MetaData" type and queries implemented and tested.
1 parent fa73610 commit 8025de7

File tree

17 files changed

+255
-6133
lines changed

17 files changed

+255
-6133
lines changed

includes/type/object/class-meta-data-type.php

Lines changed: 64 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,25 @@ public static function register() {
2626
array(
2727
'description' => __( 'Extra data defined on the WC object', 'wp-graphql-woocommerce' ),
2828
'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+
),
2936
'key' => array(
3037
'type' => array( 'non_null' => 'String' ),
3138
'description' => __( 'Meta key.', 'wp-graphql-woocommerce' ),
3239
'resolve' => function ( $source ) {
33-
return ! empty( $source['key'] ) ? $source['key'] : null;
40+
return ! empty( $source->key ) ? $source->key : null;
3441
},
3542
),
3643
'value' => array(
3744
'type' => array( 'non_null' => 'String' ),
3845
'description' => __( 'Meta value.', 'wp-graphql-woocommerce' ),
3946
'resolve' => function ( $source ) {
40-
return ! empty( $source['value'] ) ? $source['value'] : null;
47+
return ! empty( $source->value ) ? $source->value : null;
4148
},
4249
),
4350
),
@@ -62,16 +69,17 @@ public static function register() {
6269
),
6370
),
6471
'resolve' => function( $source, array $args ) {
72+
// Check if "key" argument set and assigns to target "keys" array.
6573
if ( ! empty( $args['key'] ) && ! empty( $source[ $args['key'] ] ) ) {
6674
$keys = array( $args['key'] );
67-
} elseif ( ! empty( $args['keysIn'] ) ) {
75+
} elseif ( ! empty( $args['keysIn'] ) ) { // Check if "keysIn" argument set and assigns to target "keys" array.
6876
$keys = array();
6977
foreach ( $args['keysIn'] as $key ) {
7078
if ( ! empty( $source[ $key ] ) ) {
7179
$keys[] = $key;
7280
}
7381
}
74-
} else {
82+
} else { // If no arguments set, all extra data keys are assigns to target "keys" array.
7583
$keys = array_diff(
7684
array_keys( $source ),
7785
array(
@@ -90,10 +98,14 @@ public static function register() {
9098
)
9199
);
92100
}
101+
// Create meta ID prefix.
102+
$id_prefix = apply_filters( 'cart_meta_id_prefix', 'cart_' );
93103

104+
// Format meta data for resolution.
94105
$data = array();
95106
foreach ( $keys as $key ) {
96-
$data[] = array(
107+
$data[] = (object) array(
108+
'id' => "{$id_prefix}_{$key}",
97109
'key' => $key,
98110
'value' => $source[ $key ],
99111
);
@@ -104,6 +116,7 @@ public static function register() {
104116
)
105117
);
106118

119+
// Register 'metaData' field on WC CRUD types.
107120
$types = array(
108121
'Coupon',
109122
'Customer',
@@ -130,7 +143,7 @@ public static function register() {
130143
'type' => 'String',
131144
'description' => __( 'Retrieve meta by key', 'wp-graphql-woocommerce' ),
132145
),
133-
'keysIn' => array(
146+
'keysIn' => array(
134147
'type' => array( 'list_of' => 'String' ),
135148
'description' => __( 'Retrieve multiple metas by key', 'wp-graphql-woocommerce' ),
136149
),
@@ -140,21 +153,56 @@ public static function register() {
140153
),
141154
),
142155
'resolve' => function( $source, array $args ) {
156+
// Set unique flag.
143157
$single = ! empty( $args['multiple'] ) ? ! $args['multiple'] : true;
144-
$data = array();
145158

146-
if ( ! empty( $args['key'] ) ) {
147-
$data[ $args['key'] ] = $source->get_meta( $args['key'], $single );
148-
} elseif ( ! empty( $args['keysIn'] ) ) {
149-
$data = array();
150-
foreach ( $args['keysIn'] as $key ) {
151-
$data[ $key ] = $source->get_meta( $key, $single );
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+
);
152169
}
153-
} else {
154-
$data = $source->get_meta_data();
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+
);
155204
}
156205

157-
\codecept_debug( $source->get_meta_data() );
158206
return ! empty( $data ) ? $data : null;
159207
},
160208
)

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,6 @@ public function create( $args = array(), $save = true ) {
1717
// Create new coupon crud object instance.
1818
$coupon = new WC_Coupon();
1919

20-
// Set meta data.
21-
if ( ! empty( $args['meta_data'] ) ) {
22-
$coupon->set_meta_data( $args['meta_data'] );
23-
}
24-
2520
// Set props.
2621
$amount = $this->dummy->number( 0, 75 );
2722
$coupon->set_props(
@@ -37,6 +32,11 @@ public function create( $args = array(), $save = true ) {
3732
)
3833
);
3934

35+
// Set meta data.
36+
if ( ! empty( $args['meta_data'] ) ) {
37+
$coupon->set_meta_data( $args['meta_data'] );
38+
}
39+
4040
// Return instance in not saving.
4141
if( ! $save ) {
4242
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(

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,10 @@ public function create_variable( $args = array() ) {
145145
)
146146
);
147147

148+
if ( ! empty( $args['meta_data'] ) ) {
149+
$product->set_meta_data( $args['meta_data'] );
150+
}
151+
148152
// Create and add size attribute.
149153
$attribute_data = $this->create_attribute( 'size', array( 'small', 'medium', 'large' ) ); // Create all attribute related things.
150154
$attribute_1 = new WC_Product_Attribute();

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

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

35+
if ( ! empty( $refund ) && ! empty( $args['meta_data'] ) ) {
36+
$refund->set_meta_data( $args['meta_data'] );
37+
$refund->save_meta_data();
38+
}
39+
3540
return ! empty( $refund ) ? $refund->get_id() : 0;
3641
}
3742

0 commit comments

Comments
 (0)