Skip to content

Commit 15701be

Browse files
authored
Merge pull request #231 from kidunot89/feature/tax-rate-connection-where-args
TaxRate connection improvements
2 parents e5ed555 + e2d55d4 commit 15701be

File tree

4 files changed

+152
-25
lines changed

4 files changed

+152
-25
lines changed

includes/connection/class-tax-rates.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,19 @@ public static function get_connection_config( $args = array() ) {
5656
*/
5757
public static function get_connection_args() {
5858
return array(
59-
'class' => array(
60-
'type' => 'String',
59+
'class' => array(
60+
'type' => 'TaxClassEnum',
6161
'description' => __( 'Sort by tax class', 'wp-graphql-woocommerce' ),
6262
),
63-
'orderby' => array(
63+
'postCode' => array(
64+
'type' => 'String',
65+
'description' => __( 'Filter results by a post code.', 'wp-graphql-woocommerce' ),
66+
),
67+
'postCodeIn' => array(
68+
'type' => array( 'list_of' => 'String' ),
69+
'description' => __( 'Filter results by a group of post codes', 'wp-graphql-woocommerce' ),
70+
),
71+
'orderby' => array(
6472
'type' => array( 'list_of' => 'TaxRateConnectionOrderbyInput' ),
6573
'description' => __( 'What paramater to use to order the objects by.', 'wp-graphql-woocommerce' ),
6674
),

includes/data/connection/class-tax-rate-connection-resolver.php

Lines changed: 53 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -96,29 +96,32 @@ public function get_query_args() {
9696
public function get_query() {
9797
global $wpdb;
9898

99-
if ( ! empty( $this->query_args['column'] ) ) {
100-
$results = $wpdb->get_results(
99+
if ( ! empty( $this->query_args['where'] ) ) {
100+
$sql_where = $this->query_args['where'];
101+
102+
$results = $wpdb->get_results( // @codingStandardsIgnoreStart
101103
$wpdb->prepare(
102-
"SELECT tax_rate_id
103-
FROM {$wpdb->prefix}woocommerce_tax_rates
104-
WHERE %1s = %s
104+
"SELECT rates.tax_rate_id
105+
FROM {$wpdb->prefix}woocommerce_tax_rates AS rates
106+
LEFT JOIN {$wpdb->prefix}woocommerce_tax_rate_locations AS locations
107+
ON rates.tax_rate_id = locations.tax_rate_id
108+
WHERE {$sql_where}
109+
GROUP BY rates.tax_rate_id
105110
ORDER BY %s %s",
106-
$this->query_args['column'],
107-
$this->query_args['column_value'],
108111
$this->query_args['orderby'],
109112
$this->query_args['order']
110113
)
111-
);
114+
); // @codingStandardsIgnoreEnd
112115
} else {
113-
$results = $wpdb->get_results(
116+
$results = $wpdb->get_results( // @codingStandardsIgnoreStart
114117
$wpdb->prepare(
115118
"SELECT tax_rate_id
116119
FROM {$wpdb->prefix}woocommerce_tax_rates
117120
ORDER BY %s %s",
118121
$this->query_args['orderby'],
119122
$this->query_args['order']
120123
)
121-
);
124+
); // @codingStandardsIgnoreEnd
122125
}
123126

124127
$results = array_map(
@@ -165,9 +168,46 @@ public function sanitize_input_fields( array $where_args ) {
165168
}
166169
}
167170

168-
if ( ! empty( $where_args['class'] ) ) {
169-
$args['column'] = 'tax_rate_class';
170-
$args['column_value'] = 'standard' !== $where_args['class'] ? $where_args['class'] : '';
171+
if (
172+
isset( $where_args['class'] )
173+
|| ! empty( $where_args['postCode'] )
174+
|| ! empty( $where_args['postCodeIn'] )
175+
) {
176+
$args['where'] = '';
177+
}
178+
179+
if ( isset( $where_args['class'] ) ) {
180+
if ( empty( $where_args['class'] ) ) {
181+
$args['where'] .= 'tax_rate_class = ""';
182+
} else {
183+
$rate_class = $where_args['class'];
184+
$args['where'] .= "tax_rate_class = '{$rate_class}'";
185+
}
186+
}
187+
188+
if ( ! empty( $where_args['postCode'] ) ) {
189+
if ( ! empty( $args['where'] ) ) {
190+
$args['where'] .= ' AND ';
191+
}
192+
193+
$post_code = $where_args['postCode'];
194+
$args['where'] .= "location_code = '{$post_code}'";
195+
}
196+
197+
if ( ! empty( $where_args['postCodeIn'] ) ) {
198+
if ( ! empty( $args['where'] ) ) {
199+
$args['where'] .= ' AND ';
200+
}
201+
202+
$args['where'] .= ' (';
203+
foreach ( $where_args['postCodeIn'] as $i => $post_code ) {
204+
if ( 0 === $i ) {
205+
$args['where'] .= "location_code = '{$post_code}' ";
206+
} else {
207+
$args['where'] .= "OR location_code = '{$post_code}' ";
208+
}
209+
}
210+
$args['where'] .= ') ';
171211
}
172212

173213
return $args;

tests/_support/Helper/crud-helpers/tax-rate.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public function get_rate_object( $id ) {
7373
"SELECT location_code, location_type
7474
FROM {$wpdb->prefix}woocommerce_tax_rate_locations
7575
WHERE tax_rate_id = %d",
76-
$rate->tax_rate_id
76+
$id
7777
)
7878
);
7979

tests/wpunit/TaxRateQueriesTest.php

Lines changed: 87 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,25 @@ public function testTaxesQuery() {
9393
'class' => 'reduced-rate',
9494
)
9595
),
96+
$this->helper->create(
97+
array(
98+
'country' => 'US',
99+
'state' => 'VA',
100+
'city' => 'Norfolk',
101+
'postcode' => '23451;',
102+
'rate' => '10.5',
103+
'name' => 'US VA',
104+
'priority' => '1',
105+
'compound' => '1',
106+
'shipping' => '1',
107+
'class' => 'zero-rate',
108+
)
109+
),
96110
);
97111

98112
$query = '
99-
query taxRatesQuery( $class: String ) {
100-
taxRates( where: { class: $class } ) {
113+
query ( $class: TaxClassEnum, $postCode: String, $postCodeIn: [String] ) {
114+
taxRates( where: { class: $class, postCode: $postCode, postCodeIn: $postCodeIn } ) {
101115
nodes {
102116
id
103117
}
@@ -110,15 +124,15 @@ public function testTaxesQuery() {
110124
*
111125
* tests query
112126
*/
113-
$actual = do_graphql_request( $query, 'taxRatesQuery' );
127+
$actual = graphql( array( 'query' => $query ) );
114128
$expected = array(
115129
'data' => array(
116130
'taxRates' => array(
117131
'nodes' => array_map(
118132
function( $id ) {
119133
return array( 'id' => Relay::toGlobalId( 'tax_rate', $id ) );
120134
},
121-
array_values( $rates )
135+
$rates
122136
)
123137
)
124138
)
@@ -134,16 +148,21 @@ function( $id ) {
134148
*
135149
* tests "class" where arg
136150
*/
137-
$variables = array( 'class' => 'reduced-rate' );
138-
$actual = do_graphql_request( $query, 'taxRatesQuery', $variables );
139-
$expected = array(
151+
$variables = array( 'class' => 'REDUCED_RATE' );
152+
$actual = graphql(
153+
array(
154+
'query' => $query,
155+
'variables' => $variables,
156+
)
157+
);
158+
$expected = array(
140159
'data' => array(
141160
'taxRates' => array(
142161
'nodes' => array_map(
143162
function( $id ) {
144163
return array( 'id' => Relay::toGlobalId( 'tax_rate', $id ) );
145164
},
146-
array_values(
165+
array_values(
147166
array_filter(
148167
$rates,
149168
function( $id ) {
@@ -161,5 +180,65 @@ function( $id ) {
161180
codecept_debug( $actual );
162181

163182
$this->assertEquals( $expected, $actual );
183+
184+
/**
185+
* Assertion Three
186+
*
187+
* tests "postCode" where arg
188+
*/
189+
$variables = array( 'postCode' => '23451' );
190+
$actual = graphql(
191+
array(
192+
'query' => $query,
193+
'variables' => $variables,
194+
)
195+
);
196+
$expected = array(
197+
'data' => array(
198+
'taxRates' => array(
199+
'nodes' => array_map(
200+
function( $id ) {
201+
return array( 'id' => Relay::toGlobalId( 'tax_rate', $id ) );
202+
},
203+
array( $rates[2] )
204+
)
205+
)
206+
)
207+
);
208+
209+
// use --debug flag to view.
210+
codecept_debug( $actual );
211+
212+
$this->assertEquals( $expected, $actual );
213+
214+
/**
215+
* Assertion Four
216+
*
217+
* tests "postCodeIn" where arg
218+
*/
219+
$variables = array( 'postCodeIn' => array( '123456', '23451' ) );
220+
$actual = graphql(
221+
array(
222+
'query' => $query,
223+
'variables' => $variables,
224+
)
225+
);
226+
$expected = array(
227+
'data' => array(
228+
'taxRates' => array(
229+
'nodes' => array_map(
230+
function( $id ) {
231+
return array( 'id' => Relay::toGlobalId( 'tax_rate', $id ) );
232+
},
233+
array( $rates[1],$rates[2] )
234+
)
235+
)
236+
)
237+
);
238+
239+
// use --debug flag to view.
240+
codecept_debug( $actual );
241+
242+
$this->assertEquals( $expected, $actual );
164243
}
165244
}

0 commit comments

Comments
 (0)