Skip to content

Commit 83d648a

Browse files
committed
Formatting functionality implemented for Order type. OrderHelper::print_query updated.
1 parent 606832a commit 83d648a

File tree

3 files changed

+184
-15
lines changed

3 files changed

+184
-15
lines changed

includes/model/class-order.php

Lines changed: 58 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -99,31 +99,82 @@ protected function init() {
9999
$price = ! empty( $this->data->get_discount_total() ) ? $this->data->get_discount_total() : 0;
100100
return \wc_graphql_price( $price, array( 'currency' => $this->data->get_currency() ) );
101101
},
102+
'discountTotalRaw' => array(
103+
'callback' => function() {
104+
return ! empty( $this->data->get_discount_total() ) ? $this->data->get_discount_total() : 0;
105+
},
106+
'capability' => $this->post_type_object->cap->edit_posts,
107+
),
102108
'discountTax' => function() {
103-
return ! empty( $this->data->get_discount_tax() ) ? $this->data->get_discount_tax() : 0;
109+
$price = ! empty( $this->data->get_discount_tax() ) ? $this->data->get_discount_tax() : 0;
110+
return \wc_graphql_price( $price, array( 'currency' => $this->data->get_currency() ) );
104111
},
112+
'discountTaxRaw' => array(
113+
'callback' => function() {
114+
return ! empty( $this->data->get_discount_tax() ) ? $this->data->get_discount_tax() : 0;
115+
},
116+
'capability' => $this->post_type_object->cap->edit_posts,
117+
),
105118
'shippingTotal' => function() {
106119
$price = ! empty( $this->data->get_shipping_total() ) ? $this->data->get_shipping_total() : 0;
107120
return \wc_graphql_price( $price, array( 'currency' => $this->data->get_currency() ) );
108121
},
122+
'shippingTotalRaw' => array(
123+
'callback' => function() {
124+
return ! empty( $this->data->get_shipping_total() ) ? $this->data->get_shipping_total() : 0;
125+
},
126+
'capability' => $this->post_type_object->cap->edit_posts,
127+
),
109128
'shippingTax' => function() {
110-
return ! empty( $this->data->get_shipping_tax() ) ? $this->data->get_shipping_tax() : 0;
129+
$price = ! empty( $this->data->get_shipping_tax() ) ? $this->data->get_shipping_tax() : 0;
130+
return \wc_graphql_price( $price, array( 'currency' => $this->data->get_currency() ) );
111131
},
132+
'shippingTaxRaw' => array(
133+
'callback' => function() {
134+
return ! empty( $this->data->get_shipping_tax() ) ? $this->data->get_shipping_tax() : 0;
135+
},
136+
'capability' => $this->post_type_object->cap->edit_posts,
137+
),
112138
'cartTax' => function() {
113-
return ! empty( $this->data->get_cart_tax() ) ? $this->data->get_cart_tax() : 0;
139+
$price = ! empty( $this->data->get_cart_tax() ) ? $this->data->get_cart_tax() : 0;
140+
return \wc_graphql_price( $price, array( 'currency' => $this->data->get_currency() ) );
114141
},
142+
'cartTaxRaw' => array(
143+
'callback' => function() {
144+
return ! empty( $this->data->get_cart_tax() ) ? $this->data->get_cart_tax() : 0;
145+
},
146+
'capability' => $this->post_type_object->cap->edit_posts,
147+
),
115148
'total' => function() {
116149
$price = ! empty( $this->data->get_total() ) ? $this->data->get_total() : 0;
117150
return \wc_graphql_price( $price, array( 'currency' => $this->data->get_currency() ) );
118151
},
152+
'totalRaw' => array(
153+
'callback' => function() {
154+
return ! empty( $this->data->get_total() ) ? $this->data->get_total() : 0;
155+
},
156+
'capability' => $this->post_type_object->cap->edit_posts,
157+
),
119158
'totalTax' => function() {
120-
return ! empty( $this->data->get_total_tax() ) ? $this->data->get_total_tax() : 0;
159+
$price = ! empty( $this->data->get_total_tax() ) ? $this->data->get_total_tax() : 0;
160+
return \wc_graphql_price( $price, array( 'currency' => $this->data->get_currency() ) );
121161
},
162+
'totalTaxRaw' => array(
163+
'callback' => function() {
164+
return ! empty( $this->data->get_total_tax() ) ? $this->data->get_total_tax() : 0;
165+
},
166+
'capability' => $this->post_type_object->cap->edit_posts,
167+
),
122168
'subtotal' => function() {
123-
return ! empty( $this->data->get_subtotal() )
124-
? \wc_graphql_price( $this->data->get_subtotal(), array( 'currency' => $this->data->get_currency() ) )
125-
: null;
169+
$price = ! empty( $this->data->get_subtotal() ) ? $this->data->get_subtotal() : null;
170+
return \wc_graphql_price( $price, array( 'currency' => $this->data->get_currency() ) );
126171
},
172+
'subtotalRaw' => array(
173+
'callback' => function() {
174+
return ! empty( $this->data->get_subtotal() ) ? $this->data->get_subtotal() : 0;
175+
},
176+
'capability' => $this->post_type_object->cap->edit_posts,
177+
),
127178
'orderNumber' => function() {
128179
return ! empty( $this->data->get_order_number() ) ? $this->data->get_order_number() : null;
129180
},

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

Lines changed: 122 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,34 +91,152 @@ public static function register() {
9191
'discountTotal' => array(
9292
'type' => 'String',
9393
'description' => __( 'Discount total amount', 'wp-graphql-woocommerce' ),
94+
'args' => array(
95+
'format' => array(
96+
'type' => 'PricingFieldFormatEnum',
97+
'description' => __( 'Format of the price', 'wp-graphql-woocommerce' ),
98+
),
99+
),
100+
'resolve' => function( $source, $args ) {
101+
if ( isset( $args['format'] ) && 'raw' === $args['format'] ) {
102+
// @codingStandardsIgnoreLine.
103+
return $source->discountTotalRaw;
104+
} else {
105+
// @codingStandardsIgnoreLine.
106+
return $source->discountTotal;
107+
}
108+
},
94109
),
95110
'discountTax' => array(
96-
'type' => 'Float',
111+
'type' => 'String',
97112
'description' => __( 'Discount tax amount', 'wp-graphql-woocommerce' ),
113+
'args' => array(
114+
'format' => array(
115+
'type' => 'PricingFieldFormatEnum',
116+
'description' => __( 'Format of the price', 'wp-graphql-woocommerce' ),
117+
),
118+
),
119+
'resolve' => function( $source, $args ) {
120+
if ( isset( $args['format'] ) && 'raw' === $args['format'] ) {
121+
// @codingStandardsIgnoreLine.
122+
return $source->discountTaxRaw;
123+
} else {
124+
// @codingStandardsIgnoreLine.
125+
return $source->discountTax;
126+
}
127+
},
98128
),
99129
'shippingTotal' => array(
100130
'type' => 'String',
101131
'description' => __( 'Shipping total amount', 'wp-graphql-woocommerce' ),
132+
'args' => array(
133+
'format' => array(
134+
'type' => 'PricingFieldFormatEnum',
135+
'description' => __( 'Format of the price', 'wp-graphql-woocommerce' ),
136+
),
137+
),
138+
'resolve' => function( $source, $args ) {
139+
if ( isset( $args['format'] ) && 'raw' === $args['format'] ) {
140+
// @codingStandardsIgnoreLine.
141+
return $source->shippingTotalRaw;
142+
} else {
143+
// @codingStandardsIgnoreLine.
144+
return $source->shippingTotal;
145+
}
146+
},
102147
),
103148
'shippingTax' => array(
104-
'type' => 'Float',
149+
'type' => 'String',
105150
'description' => __( 'Shipping tax amount', 'wp-graphql-woocommerce' ),
151+
'args' => array(
152+
'format' => array(
153+
'type' => 'PricingFieldFormatEnum',
154+
'description' => __( 'Format of the price', 'wp-graphql-woocommerce' ),
155+
),
156+
),
157+
'resolve' => function( $source, $args ) {
158+
if ( isset( $args['format'] ) && 'raw' === $args['format'] ) {
159+
// @codingStandardsIgnoreLine.
160+
return $source->shippingTaxRaw;
161+
} else {
162+
// @codingStandardsIgnoreLine.
163+
return $source->shippingTax;
164+
}
165+
},
106166
),
107167
'cartTax' => array(
108-
'type' => 'Float',
168+
'type' => 'String',
109169
'description' => __( 'Cart tax amount', 'wp-graphql-woocommerce' ),
170+
'args' => array(
171+
'format' => array(
172+
'type' => 'PricingFieldFormatEnum',
173+
'description' => __( 'Format of the price', 'wp-graphql-woocommerce' ),
174+
),
175+
),
176+
'resolve' => function( $source, $args ) {
177+
if ( isset( $args['format'] ) && 'raw' === $args['format'] ) {
178+
// @codingStandardsIgnoreLine.
179+
return $source->cartTaxRaw;
180+
} else {
181+
// @codingStandardsIgnoreLine.
182+
return $source->cartTax;
183+
}
184+
},
110185
),
111186
'total' => array(
112187
'type' => 'String',
113188
'description' => __( 'Order grand total', 'wp-graphql-woocommerce' ),
189+
'args' => array(
190+
'format' => array(
191+
'type' => 'PricingFieldFormatEnum',
192+
'description' => __( 'Format of the price', 'wp-graphql-woocommerce' ),
193+
),
194+
),
195+
'resolve' => function( $source, $args ) {
196+
if ( isset( $args['format'] ) && 'raw' === $args['format'] ) {
197+
// @codingStandardsIgnoreLine.
198+
return $source->totalRaw;
199+
} else {
200+
return $source->total;
201+
}
202+
},
114203
),
115204
'totalTax' => array(
116-
'type' => 'Float',
205+
'type' => 'String',
117206
'description' => __( 'Order taxes', 'wp-graphql-woocommerce' ),
207+
'args' => array(
208+
'format' => array(
209+
'type' => 'PricingFieldFormatEnum',
210+
'description' => __( 'Format of the price', 'wp-graphql-woocommerce' ),
211+
),
212+
),
213+
'resolve' => function( $source, $args ) {
214+
if ( isset( $args['format'] ) && 'raw' === $args['format'] ) {
215+
// @codingStandardsIgnoreLine.
216+
return $source->totalTaxRaw;
217+
} else {
218+
// @codingStandardsIgnoreLine.
219+
return $source->totalTax;
220+
}
221+
},
118222
),
119223
'subtotal' => array(
120224
'type' => 'String',
121225
'description' => __( 'Order subtotal', 'wp-graphql-woocommerce' ),
226+
'args' => array(
227+
'format' => array(
228+
'type' => 'PricingFieldFormatEnum',
229+
'description' => __( 'Format of the price', 'wp-graphql-woocommerce' ),
230+
),
231+
),
232+
'resolve' => function( $source, $args ) {
233+
if ( isset( $args['format'] ) && 'raw' === $args['format'] ) {
234+
// @codingStandardsIgnoreLine.
235+
return $source->subtotalRaw;
236+
} else {
237+
return $source->subtotal;
238+
}
239+
},
122240
),
123241
'orderNumber' => array(
124242
'type' => 'String',

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -176,12 +176,12 @@ public function print_query( $id ) {
176176
'modified' => $data->get_date_modified()->__toString(),
177177
'status' => strtoupper( $data->get_status() ),
178178
'discountTotal' => \wc_graphql_price( $data->get_discount_total(), array( 'currency' => $data->get_currency() ) ),
179-
'discountTax' => floatval( $data->get_discount_tax() ),
179+
'discountTax' => \wc_graphql_price( $data->get_discount_tax(), array( 'currency' => $data->get_currency() ) ),
180180
'shippingTotal' => \wc_graphql_price( $data->get_shipping_total(), array( 'currency' => $data->get_currency() ) ),
181-
'shippingTax' => floatval( $data->get_shipping_tax() ),
182-
'cartTax' => floatval( $data->get_cart_tax() ),
181+
'shippingTax' => \wc_graphql_price( $data->get_shipping_tax(), array( 'currency' => $data->get_currency() ) ),
182+
'cartTax' => \wc_graphql_price( $data->get_cart_tax(), array( 'currency' => $data->get_currency() ) ),
183183
'total' => \wc_graphql_price( $data->get_total(), array( 'currency' => $data->get_currency() ) ),
184-
'totalTax' => floatval( $data->get_total_tax() ),
184+
'totalTax' => \wc_graphql_price( $data->get_total_tax(), array( 'currency' => $data->get_currency() ) ),
185185
'subtotal' => \wc_graphql_price( $data->get_subtotal(), array( 'currency' => $data->get_currency() ) ),
186186
'orderNumber' => $data->get_order_number(),
187187
'orderKey' => $data->get_order_key(),

0 commit comments

Comments
 (0)