Skip to content

Commit 82e5e72

Browse files
authored
fix: Product "price" field now supports the "taxes included" display … (#703)
* fix: Product "price" field now supports the "taxes included" display setting. * chore: WPCS compliance met.
1 parent 121918f commit 82e5e72

File tree

3 files changed

+159
-117
lines changed

3 files changed

+159
-117
lines changed

includes/model/class-product.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,11 +237,11 @@ protected function init() {
237237
$fields += [
238238
'price' => function() {
239239
return ! empty( $this->wc_data->get_price() )
240-
? \wc_graphql_price( $this->wc_data->get_price() )
240+
? \wc_graphql_price( \wc_get_price_to_display( $this->wc_data ) )
241241
: null;
242242
},
243243
'priceRaw' => function() {
244-
return ! empty( $this->wc_data->get_price() ) ? $this->wc_data->get_price() : null;
244+
return ! empty( $this->wc_data->get_price() ) ? \wc_get_price_to_display( $this->wc_data ) : null;
245245
},
246246
'regularPrice' => function() {
247247
return ! empty( $this->wc_data->get_regular_price() )

tests/_support/Factory/TaxRateFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public function update_object( $object, $fields ) {
7474

7575
public function get_object_by_id( $id ) {
7676
global $wpdb;
77-
$rate = WC_Tax::_get_tax_rate( $id, OBJECT );
77+
$rate = \WC_Tax::_get_tax_rate( $id, OBJECT );
7878
if ( \is_wp_error( $rate ) || empty( $rate ) ) {
7979
return null;
8080
}
Lines changed: 156 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,36 @@
11
<?php
22

3-
use GraphQLRelay\Relay;
3+
class TaxRateQueriesTest extends \Tests\WPGraphQL\WooCommerce\TestCase\WooGraphQLTestCase {
4+
public function expectedTaxRateData( $rate_id ) {
5+
$rate = $this->factory->tax_rate->get_object_by_id( $rate_id );
46

5-
class TaxRateQueriesTest extends \Codeception\TestCase\WPTestCase {
6-
private $shop_manager;
7-
private $customer;
8-
private $rate;
9-
private $helper;
10-
11-
public function setUp(): void {
12-
parent::setUp();
7+
$expected = [
8+
$this->expectedField( 'taxRate.id', $this->toRelayId( 'tax_rate', $rate_id ) ),
9+
$this->expectedField( 'taxRate.databaseId', absint( $rate->tax_rate_id ) ),
10+
$this->expectedField( 'taxRate.country', ! empty( $rate->tax_rate_country ) ? $rate->tax_rate_country : self::IS_NULL ),
11+
$this->expectedField( 'taxRate.state', ! empty( $rate->tax_rate_state ) ? $rate->tax_rate_state : self::IS_NULL ),
12+
$this->expectedField( 'taxRate.postcode', ! empty( $rate->tax_rate_postcode ) ? $rate->tax_rate_postcode : [ '*' ] ),
13+
$this->expectedField( 'taxRate.city', ! empty( $rate->tax_rate_city ) ? $rate->tax_rate_city : [ '*' ] ),
14+
$this->expectedField( 'taxRate.rate', ! empty( $rate->tax_rate ) ? $rate->tax_rate : self::IS_NULL ),
15+
$this->expectedField( 'taxRate.name', ! empty( $rate->tax_rate_name ) ? $rate->tax_rate_name : self::IS_NULL ),
16+
$this->expectedField( 'taxRate.priority', absint( $rate->tax_rate_priority ) ),
17+
$this->expectedField( 'taxRate.compound', (bool) $rate->tax_rate_compound ),
18+
$this->expectedField( 'taxRate.shipping', (bool) $rate->tax_rate_shipping ),
19+
$this->expectedField( 'taxRate.order', absint( $rate->tax_rate_order ) ),
20+
$this->expectedField(
21+
'taxRate.class',
22+
! empty( $rate->tax_rate_class )
23+
? WPEnumType::get_safe_name( $rate->tax_rate_class )
24+
: 'STANDARD'
25+
),
26+
];
1327

14-
$this->shop_manager = $this->factory->user->create( [ 'role' => 'shop_manager' ] );
15-
$this->customer = $this->factory->user->create( [ 'role' => 'customer' ] );
16-
$this->helper = $this->getModule( '\Helper\Wpunit' )->tax_rate();
17-
$this->rate = $this->helper->create();
28+
return $expected;
1829
}
1930

2031
// tests
2132
public function testTaxQuery() {
22-
$id = Relay::toGlobalId( 'tax_rate', $this->rate );
33+
$rate = $this->factory->tax_rate->create();
2334

2435
$query = '
2536
query taxRateQuery( $id: ID, $idType: TaxRateIdTypeEnum ) {
@@ -46,37 +57,30 @@ class
4657
*
4758
* Tests query, "id" query arg, and results
4859
*/
49-
$variables = [ 'id' => $id ];
50-
$actual = do_graphql_request( $query, 'taxRateQuery', $variables );
51-
$expected = [ 'data' => [ 'taxRate' => $this->helper->print_query( $this->rate ) ] ];
60+
$variables = [ 'id' => $this->toRelayId( 'tax_rate', $rate ) ];
61+
$response = $this->graphql( compact( 'query', 'variables' ) );
62+
$expected = $this->expectedTaxRateData( $rate );
5263

53-
// use --debug flag to view.
54-
codecept_debug( $actual );
55-
56-
$this->assertEquals( $expected, $actual );
64+
$this->assertQuerySuccessful( $response, $expected );
5765

5866
/**
5967
* Assertion Two
6068
*
6169
* Tests query, "rateId" query arg, and results
6270
*/
6371
$variables = [
64-
'id' => $this->rate,
72+
'id' => $rate,
6573
'idType' => 'DATABASE_ID',
6674
];
67-
$actual = do_graphql_request( $query, 'taxRateQuery', $variables );
68-
$expected = [ 'data' => [ 'taxRate' => $this->helper->print_query( $this->rate ) ] ];
69-
70-
// use --debug flag to view.
71-
codecept_debug( $actual );
75+
$response = $this->graphql( compact( 'query', 'variables' ) );
7276

73-
$this->assertEquals( $expected, $actual );
77+
$this->assertQuerySuccessful( $response, $expected );
7478
}
7579

7680
public function testTaxesQuery() {
7781
$rates = [
78-
$this->rate,
79-
$this->helper->create(
82+
$this->factory->tax_rate->create(),
83+
$this->factory->tax_rate->create(
8084
[
8185
'country' => 'US',
8286
'state' => 'AL',
@@ -90,7 +94,7 @@ public function testTaxesQuery() {
9094
'class' => 'reduced-rate',
9195
]
9296
),
93-
$this->helper->create(
97+
$this->factory->tax_rate->create(
9498
[
9599
'country' => 'US',
96100
'state' => 'VA',
@@ -111,6 +115,7 @@ public function testTaxesQuery() {
111115
taxRates( where: { class: $class, postCode: $postCode, postCodeIn: $postCodeIn } ) {
112116
nodes {
113117
id
118+
class
114119
}
115120
}
116121
}
@@ -121,121 +126,158 @@ public function testTaxesQuery() {
121126
*
122127
* Tests query
123128
*/
124-
$actual = graphql( [ 'query' => $query ] );
125-
$expected = [
126-
'data' => [
127-
'taxRates' => [
128-
'nodes' => array_map(
129-
function( $id ) {
130-
return [ 'id' => Relay::toGlobalId( 'tax_rate', $id ) ];
131-
},
132-
$rates
133-
),
134-
],
135-
],
136-
];
137-
138-
// use --debug flag to view.
139-
codecept_debug( $actual );
129+
$response = $this->graphql( compact( 'query' ) );
130+
$expected = array_map(
131+
function( $id ) {
132+
return $this->expectedNode(
133+
'taxRates.nodes',
134+
[
135+
$this->expectedField( 'id', $this->toRelayId( 'tax_rate', $id ) ),
136+
]
137+
);
138+
},
139+
$rates
140+
);
140141

141-
$this->assertEquals( $expected, $actual );
142+
$this->assertQuerySuccessful( $response, $expected );
142143

143144
/**
144145
* Assertion Two
145146
*
146147
* Tests "class" where arg
147148
*/
148-
$variables = [ 'class' => 'REDUCED_RATE' ];
149-
$actual = graphql(
150-
[
151-
'query' => $query,
152-
'variables' => $variables,
153-
]
149+
$reduced_tax_rates = array_values(
150+
array_filter(
151+
$rates,
152+
function( $id ) {
153+
$rate = $this->factory->tax_rate->get_object_by_id( $id );
154+
return 'reduced-rate' === $rate->tax_rate_class;
155+
}
156+
)
157+
);
158+
$variables = [ 'class' => 'REDUCED_RATE' ];
159+
$response = $this->graphql( compact( 'query', 'variables' ) );
160+
$expected = array_map(
161+
function( $id ) {
162+
return $this->expectedNode(
163+
'taxRates.nodes',
164+
[
165+
$this->expectedField( 'id', $this->toRelayId( 'tax_rate', $id ) ),
166+
]
167+
);
168+
},
169+
$reduced_tax_rates,
154170
);
155-
$expected = [
156-
'data' => [
157-
'taxRates' => [
158-
'nodes' => array_map(
159-
function( $id ) {
160-
return [ 'id' => Relay::toGlobalId( 'tax_rate', $id ) ];
161-
},
162-
array_values(
163-
array_filter(
164-
$rates,
165-
function( $id ) {
166-
$rate = $this->helper->get_rate_object( $id );
167-
return 'reduced-rate' === $rate->tax_rate_class;
168-
}
169-
)
170-
)
171-
),
172-
],
173-
],
174-
];
175171

176-
// use --debug flag to view.
177-
codecept_debug( $actual );
172+
$expected[] = $this->not()->expectedField( 'taxRates.nodes.#.class', 'STANDARD' );
178173

179-
$this->assertEquals( $expected, $actual );
174+
$this->assertQuerySuccessful( $response, $expected );
180175

181176
/**
182177
* Assertion Three
183178
*
184179
* Tests "postCode" where arg
185180
*/
186181
$variables = [ 'postCode' => '23451' ];
187-
$actual = graphql(
188-
[
189-
'query' => $query,
190-
'variables' => $variables,
191-
]
192-
);
182+
$response = $this->graphql( compact( 'query', 'variables' ) );
193183
$expected = [
194-
'data' => [
195-
'taxRates' => [
196-
'nodes' => array_map(
197-
function( $id ) {
198-
return [ 'id' => Relay::toGlobalId( 'tax_rate', $id ) ];
199-
},
200-
[ $rates[2] ]
201-
),
202-
],
203-
],
184+
$this->expectedField( 'taxRates.nodes.0.id', $this->toRelayId( 'tax_rate', $rates[2] ) ),
185+
$this->not()->expectedField( 'taxRates.nodes.#.id', $this->toRelayId( 'tax_rate', $rates[0] ) ),
186+
$this->not()->expectedField( 'taxRates.nodes.#.id', $this->toRelayId( 'tax_rate', $rates[1] ) ),
204187
];
205188

206-
// use --debug flag to view.
207-
codecept_debug( $actual );
208-
209-
$this->assertEquals( $expected, $actual );
189+
$this->assertQuerySuccessful( $response, $expected );
210190

211191
/**
212192
* Assertion Four
213193
*
214194
* Tests "postCodeIn" where arg
215195
*/
216196
$variables = [ 'postCodeIn' => [ '123456', '23451' ] ];
217-
$actual = graphql(
197+
$response = $this->graphql( compact( 'query', 'variables' ) );
198+
$expected = [
199+
$this->expectedField( 'taxRates.nodes.#.id', $this->toRelayId( 'tax_rate', $rates[1] ) ),
200+
$this->expectedField( 'taxRates.nodes.#.id', $this->toRelayId( 'tax_rate', $rates[2] ) ),
201+
$this->not()->expectedField( 'taxRates.nodes.#.id', $this->toRelayId( 'tax_rate', $rates[0] ) ),
202+
];
203+
204+
$this->assertQuerySuccessful( $response, $expected );
205+
}
206+
207+
public function testTaxIncludedOptionEffect() {
208+
// Create tax rate.
209+
$this->factory->tax_rate->create(
218210
[
219-
'query' => $query,
220-
'variables' => $variables,
211+
'country' => 'US',
212+
'state' => '',
213+
'rate' => 10,
214+
'name' => 'US',
215+
'priority' => '1',
216+
'compound' => '0',
217+
'shipping' => '1',
218+
'class' => '',
221219
]
222220
);
223-
$expected = [
224-
'data' => [
225-
'taxRates' => [
226-
'nodes' => array_map(
227-
function( $id ) {
228-
return [ 'id' => Relay::toGlobalId( 'tax_rate', $id ) ];
229-
},
230-
[ $rates[1], $rates[2] ]
231-
),
232-
],
233-
],
221+
222+
// Set customer address.
223+
\WC()->customer->set_shipping_city( 'Norfolk' );
224+
\WC()->customer->set_shipping_state( 'VA' );
225+
\WC()->customer->set_shipping_postcode( '23451' );
226+
\WC()->customer->set_shipping_country( 'US' );
227+
\WC()->customer->save();
228+
\WC()->initialize_session();
229+
230+
// Create product to query.
231+
$product_id = $this->factory->product->createSimple(
232+
[
233+
'price' => 10,
234+
'regular_price' => 10,
235+
]
236+
);
237+
$query = '
238+
query( $id: ID! ) {
239+
product( id: $id ) {
240+
... on SimpleProduct {
241+
price
242+
regularPrice
243+
}
244+
}
245+
}
246+
';
247+
$variables = [ 'id' => $this->toRelayId( 'post', $product_id ) ];
248+
249+
/**
250+
* Assertion One
251+
*
252+
* Test without taxes included.
253+
*/
254+
255+
$response = $this->graphql( compact( 'query', 'variables' ) );
256+
$expected = [
257+
$this->expectedField( 'product.price', '$10.00' ),
234258
];
235259

236-
// use --debug flag to view.
237-
codecept_debug( $actual );
260+
$this->assertQuerySuccessful( $response, $expected );
261+
262+
// Clear product cache.
263+
$this->clearLoaderCache( 'wc_post' );
264+
$this->clearLoaderCache( 'post' );
265+
266+
/**
267+
* Assertion Two
268+
*
269+
* Test with taxes included.
270+
*/
271+
update_option( 'woocommerce_calc_taxes', 'yes' );
272+
update_option( 'woocommerce_prices_include_tax', 'no' );
273+
update_option( 'woocommerce_tax_display_shop', 'incl' );
274+
275+
$this->logData( \wc_prices_include_tax() ? 'included' : 'excluded' );
276+
$response = $this->graphql( compact( 'query', 'variables' ) );
277+
$expected = [
278+
$this->expectedField( 'product.price', '$11.00' ),
279+
];
238280

239-
$this->assertEquals( $expected, $actual );
281+
$this->assertQuerySuccessful( $response, $expected );
240282
}
241283
}

0 commit comments

Comments
 (0)