Skip to content

Commit fa73610

Browse files
committed
CartItem field "extraData" implemented and tested.
1 parent f93b590 commit fa73610

File tree

11 files changed

+651
-142
lines changed

11 files changed

+651
-142
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: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
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+
'key' => array(
30+
'type' => array( 'non_null' => 'String' ),
31+
'description' => __( 'Meta key.', 'wp-graphql-woocommerce' ),
32+
'resolve' => function ( $source ) {
33+
return ! empty( $source['key'] ) ? $source['key'] : null;
34+
},
35+
),
36+
'value' => array(
37+
'type' => array( 'non_null' => 'String' ),
38+
'description' => __( 'Meta value.', 'wp-graphql-woocommerce' ),
39+
'resolve' => function ( $source ) {
40+
return ! empty( $source['value'] ) ? $source['value'] : null;
41+
},
42+
),
43+
),
44+
)
45+
);
46+
47+
// Register 'extraData' field on CartItem.
48+
register_graphql_field(
49+
'CartItem',
50+
'extraData',
51+
array(
52+
'type' => array( 'list_of' => 'MetaData' ),
53+
'description' => __( 'Object meta data', 'wp-graphql-woocommerce' ),
54+
'args' => array(
55+
'key' => array(
56+
'type' => 'String',
57+
'description' => __( 'Retrieve meta by key', 'wp-graphql-woocommerce' ),
58+
),
59+
'keysIn' => array(
60+
'type' => array( 'list_of' => 'String' ),
61+
'description' => __( 'Retrieve multiple metas by key', 'wp-graphql-woocommerce' ),
62+
),
63+
),
64+
'resolve' => function( $source, array $args ) {
65+
if ( ! empty( $args['key'] ) && ! empty( $source[ $args['key'] ] ) ) {
66+
$keys = array( $args['key'] );
67+
} elseif ( ! empty( $args['keysIn'] ) ) {
68+
$keys = array();
69+
foreach ( $args['keysIn'] as $key ) {
70+
if ( ! empty( $source[ $key ] ) ) {
71+
$keys[] = $key;
72+
}
73+
}
74+
} else {
75+
$keys = array_diff(
76+
array_keys( $source ),
77+
array(
78+
'key',
79+
'product_id',
80+
'variation_id',
81+
'variation',
82+
'quantity',
83+
'data',
84+
'data_hash',
85+
'line_tax_data',
86+
'line_subtotal',
87+
'line_subtotal_tax',
88+
'line_total',
89+
'line_tax',
90+
)
91+
);
92+
}
93+
94+
$data = array();
95+
foreach ( $keys as $key ) {
96+
$data[] = array(
97+
'key' => $key,
98+
'value' => $source[ $key ],
99+
);
100+
}
101+
102+
return $data;
103+
},
104+
)
105+
);
106+
107+
$types = array(
108+
'Coupon',
109+
'Customer',
110+
'CouponLine',
111+
'LineItem',
112+
'FeeLine',
113+
'Order',
114+
'Product',
115+
'ProductVariation',
116+
'Refund',
117+
'ShippingLine',
118+
'TaxLine',
119+
);
120+
121+
foreach ( $types as $type ) {
122+
register_graphql_field(
123+
$type,
124+
'metaData',
125+
array(
126+
'type' => array( 'list_of' => 'MetaData' ),
127+
'description' => __( 'Object meta data', 'wp-graphql-woocommerce' ),
128+
'args' => array(
129+
'key' => array(
130+
'type' => 'String',
131+
'description' => __( 'Retrieve meta by key', 'wp-graphql-woocommerce' ),
132+
),
133+
'keysIn' => array(
134+
'type' => array( 'list_of' => 'String' ),
135+
'description' => __( 'Retrieve multiple metas by key', 'wp-graphql-woocommerce' ),
136+
),
137+
'multiple' => array(
138+
'type' => 'Boolean',
139+
'description' => __( 'Retrieve meta with matching keys', 'wp-graphql-woocommerce' ),
140+
),
141+
),
142+
'resolve' => function( $source, array $args ) {
143+
$single = ! empty( $args['multiple'] ) ? ! $args['multiple'] : true;
144+
$data = array();
145+
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 );
152+
}
153+
} else {
154+
$data = $source->get_meta_data();
155+
}
156+
157+
\codecept_debug( $source->get_meta_data() );
158+
return ! empty( $data ) ? $data : null;
159+
},
160+
)
161+
);
162+
}
163+
}
164+
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ 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+
2025
// Set props.
2126
$amount = $this->dummy->number( 0, 75 );
2227
$coupon->set_props(

0 commit comments

Comments
 (0)