From 48de1f6b97b887a1636da2c28a0e01dc1504b38f Mon Sep 17 00:00:00 2001 From: Gerhard Date: Mon, 15 Oct 2018 12:49:56 +0200 Subject: [PATCH 1/2] Accommodate numeric slugs in the product shortcode attribute and category fields. --- .../class-wc-shortcode-products.php | 31 +++++++++++++++++-- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/includes/shortcodes/class-wc-shortcode-products.php b/includes/shortcodes/class-wc-shortcode-products.php index 72de10afaaa..ac75ff82a7b 100644 --- a/includes/shortcodes/class-wc-shortcode-products.php +++ b/includes/shortcodes/class-wc-shortcode-products.php @@ -288,7 +288,13 @@ class WC_Shortcode_Products { if ( $terms && is_numeric( $terms[0] ) ) { $terms = array_map( 'absint', $terms ); - $field = 'term_id'; + $query_args['tax_query']['relation'] = 'OR'; + $query_args['tax_query'][] = array( + 'taxonomy' => $taxonomy, + 'terms' => $terms, + 'field' => 'term_id', + 'operator' => $this->attributes['terms_operator'], + ); } // If no terms were specified get all products that are in the attribute taxonomy. @@ -299,9 +305,15 @@ class WC_Shortcode_Products { 'fields' => 'ids', ) ); - $field = 'term_id'; + $query_args['tax_query'][] = array( + 'taxonomy' => $taxonomy, + 'terms' => $terms, + 'field' => 'term_id', + 'operator' => $this->attributes['terms_operator'], + ); } + // We always need to search based on the slug as well, this is to accommodate numeric slugs. $query_args['tax_query'][] = array( 'taxonomy' => $taxonomy, 'terms' => $terms, @@ -324,9 +336,22 @@ class WC_Shortcode_Products { if ( is_numeric( $categories[0] ) ) { $categories = array_map( 'absint', $categories ); - $field = 'term_id'; + $query_args['tax_query']['relation'] = 'OR'; + $query_args['tax_query'][] = array( + 'taxonomy' => 'product_cat', + 'terms' => $categories, + 'field' => 'term_id', + 'operator' => $this->attributes['cat_operator'], + + /* + * When cat_operator is AND, the children categories should be excluded, + * as only products belonging to all the children categories would be selected. + */ + 'include_children' => 'AND' === $this->attributes['cat_operator'] ? false : true, + ); } + // We always need to search based on the slug as well, this is to accommodate numeric slugs. $query_args['tax_query'][] = array( 'taxonomy' => 'product_cat', 'terms' => $categories, From aa6a6f2b105203ea07158004c067cf3607903b26 Mon Sep 17 00:00:00 2001 From: Gerhard Date: Mon, 15 Oct 2018 13:22:12 +0200 Subject: [PATCH 2/2] Refine numeric slug checking when using the products shortcode for attributes and categories. --- .../class-wc-shortcode-products.php | 44 +++++++------------ 1 file changed, 17 insertions(+), 27 deletions(-) diff --git a/includes/shortcodes/class-wc-shortcode-products.php b/includes/shortcodes/class-wc-shortcode-products.php index ac75ff82a7b..b4545504a45 100644 --- a/includes/shortcodes/class-wc-shortcode-products.php +++ b/includes/shortcodes/class-wc-shortcode-products.php @@ -287,14 +287,15 @@ class WC_Shortcode_Products { $field = 'slug'; if ( $terms && is_numeric( $terms[0] ) ) { + $field = 'term_id'; $terms = array_map( 'absint', $terms ); - $query_args['tax_query']['relation'] = 'OR'; - $query_args['tax_query'][] = array( - 'taxonomy' => $taxonomy, - 'terms' => $terms, - 'field' => 'term_id', - 'operator' => $this->attributes['terms_operator'], - ); + // Check numeric slugs. + foreach ( $terms as $term ) { + $the_term = get_term_by( 'slug', $term, $taxonomy ); + if ( false !== $the_term ) { + $terms[] = $the_term->term_id; + } + } } // If no terms were specified get all products that are in the attribute taxonomy. @@ -305,12 +306,7 @@ class WC_Shortcode_Products { 'fields' => 'ids', ) ); - $query_args['tax_query'][] = array( - 'taxonomy' => $taxonomy, - 'terms' => $terms, - 'field' => 'term_id', - 'operator' => $this->attributes['terms_operator'], - ); + $field = 'term_id'; } // We always need to search based on the slug as well, this is to accommodate numeric slugs. @@ -335,23 +331,17 @@ class WC_Shortcode_Products { $field = 'slug'; if ( is_numeric( $categories[0] ) ) { + $field = 'term_id'; $categories = array_map( 'absint', $categories ); - $query_args['tax_query']['relation'] = 'OR'; - $query_args['tax_query'][] = array( - 'taxonomy' => 'product_cat', - 'terms' => $categories, - 'field' => 'term_id', - 'operator' => $this->attributes['cat_operator'], - - /* - * When cat_operator is AND, the children categories should be excluded, - * as only products belonging to all the children categories would be selected. - */ - 'include_children' => 'AND' === $this->attributes['cat_operator'] ? false : true, - ); + // Check numeric slugs. + foreach ( $categories as $cat ) { + $the_cat = get_term_by( 'slug', $cat, 'product_cat' ); + if ( false !== $the_cat ) { + $categories[] = $the_cat->term_id; + } + } } - // We always need to search based on the slug as well, this is to accommodate numeric slugs. $query_args['tax_query'][] = array( 'taxonomy' => 'product_cat', 'terms' => $categories,