Merge pull request #23194 from woocommerce/fix/23171

[3.6] Fix results from wp_term_counts when sorting by menu_order
This commit is contained in:
Mike Jolley 2019-04-03 15:46:01 +01:00 committed by GitHub
commit 274a471167
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 11 deletions

View File

@ -34,8 +34,6 @@ function wc_change_get_terms_defaults( $defaults, $taxonomies ) {
switch ( $orderby ) { switch ( $orderby ) {
case 'menu_order': case 'menu_order':
$defaults['orderby'] = 'meta_value_num';
$defaults['meta_key'] = 'order'; // phpcs:ignore
$defaults['force_menu_order_sort'] = true; $defaults['force_menu_order_sort'] = true;
break; break;
case 'name_num': case 'name_num':
@ -60,14 +58,21 @@ add_filter( 'get_terms_defaults', 'wc_change_get_terms_defaults', 10, 2 );
function wc_change_pre_get_terms( $terms_query ) { function wc_change_pre_get_terms( $terms_query ) {
$args = &$terms_query->query_vars; $args = &$terms_query->query_vars;
if ( ! empty( $args['menu_order'] ) ) { // When COUNTING, disable custom sorting.
$args['order'] = 'DESC' === strtoupper( $args['menu_order'] ) ? 'DESC' : 'ASC'; if ( 'count' === $args['fields'] ) {
$args['orderby'] = 'meta_value_num'; return;
$args['meta_key'] = 'order'; // phpcs:ignore
$args['force_menu_order_sort'] = true;
$terms_query->meta_query->parse_query_vars( $args );
} }
if ( ! empty( $args['menu_order'] ) ) {
$args['order'] = 'DESC' === strtoupper( $args['menu_order'] ) ? 'DESC' : 'ASC';
$args['force_menu_order_sort'] = true;
}
if ( ! empty( $args['force_menu_order_sort'] ) ) {
$args['orderby'] = 'meta_value_num';
$args['meta_key'] = 'order'; // phpcs:ignore
$terms_query->meta_query->parse_query_vars( $args );
}
} }
add_action( 'pre_get_terms', 'wc_change_pre_get_terms', 10, 1 ); add_action( 'pre_get_terms', 'wc_change_pre_get_terms', 10, 1 );
@ -83,7 +88,7 @@ function wc_terms_clauses( $clauses, $taxonomies, $args ) {
global $wpdb; global $wpdb;
// No need to filter when counting. // No need to filter when counting.
if ( strpos( 'COUNT(*)', $clauses['fields'] ) !== false ) { if ( strpos( $clauses['fields'], 'COUNT(*)' ) !== false ) {
return $clauses; return $clauses;
} }

View File

@ -1,10 +1,14 @@
<?php <?php
/** /**
* Class Functions. * Attribute function tests.
*
* @package WooCommerce\Tests\Attributes * @package WooCommerce\Tests\Attributes
* @since 3.2.0 * @since 3.2.0
*/ */
/**
* WC_Tests_Attributes_Functions class.
*/
class WC_Tests_Attributes_Functions extends WC_Unit_Test_Case { class WC_Tests_Attributes_Functions extends WC_Unit_Test_Case {
/** /**
@ -202,4 +206,19 @@ class WC_Tests_Attributes_Functions extends WC_Unit_Test_Case {
$result = wc_delete_attribute( 9999999 ); $result = wc_delete_attribute( 9999999 );
$this->assertFalse( $result ); $this->assertFalse( $result );
} }
/**
* Test counts of attributes.
*/
public function test_count_attribute_terms() {
$global_attribute_data = WC_Helper_Product::create_attribute( 'test', array( 'Chicken', 'Nuggets' ) );
$count = wp_count_terms(
$global_attribute_data['attribute_taxonomy'],
array(
'hide_empty' => false,
)
);
$this->assertEquals( 2, $count );
}
} }