From 1fc762c0b68b2afefeb4a568fd77f2deff6044a7 Mon Sep 17 00:00:00 2001 From: Nicola Mustone Date: Thu, 13 Aug 2015 14:37:21 +0200 Subject: [PATCH] added args category to some shortcodes --- includes/class-wc-shortcodes.php | 110 ++++++++++++++++++++----------- 1 file changed, 72 insertions(+), 38 deletions(-) diff --git a/includes/class-wc-shortcodes.php b/includes/class-wc-shortcodes.php index 896affbfa04..2c527722b3e 100644 --- a/includes/class-wc-shortcodes.php +++ b/includes/class-wc-shortcodes.php @@ -2,11 +2,11 @@ /** * WC_Shortcodes class. * - * @class WC_Shortcodes - * @version 2.1.0 - * @package WooCommerce/Classes - * @category Class - * @author WooThemes + * @class WC_Shortcodes + * @version 2.1.0 + * @package WooCommerce/Classes + * @category Class + * @author WooThemes */ class WC_Shortcodes { @@ -172,23 +172,17 @@ class WC_Shortcodes { $ordering_args = WC()->query->get_catalog_ordering_args( $atts['orderby'], $atts['order'] ); $meta_query = WC()->query->get_meta_query(); $query_args = array( - 'post_type' => 'product', - 'post_status' => 'publish', - 'ignore_sticky_posts' => 1, - 'orderby' => $ordering_args['orderby'], - 'order' => $ordering_args['order'], - 'posts_per_page' => $atts['per_page'], - 'meta_query' => $meta_query, - 'tax_query' => array( - array( - 'taxonomy' => 'product_cat', - 'terms' => array_map( 'sanitize_title', explode( ',', $atts['category'] ) ), - 'field' => 'slug', - 'operator' => $atts['operator'] - ) - ) + 'post_type' => 'product', + 'post_status' => 'publish', + 'ignore_sticky_posts' => 1, + 'orderby' => $ordering_args['orderby'], + 'order' => $ordering_args['order'], + 'posts_per_page' => $atts['per_page'], + 'meta_query' => $meta_query ); + $query_args = self::_maybe_add_category_args( $query_args, $atts['category'], $atts['operator'] ); + if ( isset( $ordering_args['meta_key'] ) ) { $query_args['meta_key'] = $ordering_args['meta_key']; } @@ -291,10 +285,12 @@ class WC_Shortcodes { */ public static function recent_products( $atts ) { $atts = shortcode_atts( array( - 'per_page' => '12', - 'columns' => '4', - 'orderby' => 'date', - 'order' => 'desc' + 'per_page' => '12', + 'columns' => '4', + 'orderby' => 'date', + 'order' => 'desc', + 'category' => '', // Slugs + 'operator' => 'IN' // Possible values are 'IN', 'NOT IN', 'AND'. ), $atts ); $query_args = array( @@ -307,6 +303,8 @@ class WC_Shortcodes { 'meta_query' => WC()->query->get_meta_query() ); + $query_args = self::_maybe_add_category_args( $query_args, $atts['category'], $atts['operator'] ); + return self::product_loop( $query_args, $atts, 'recent_products' ); } @@ -373,9 +371,9 @@ class WC_Shortcodes { if ( isset( $atts['sku'] ) ) { $args['meta_query'][] = array( - 'key' => '_sku', - 'value' => $atts['sku'], - 'compare' => '=' + 'key' => '_sku', + 'value' => $atts['sku'], + 'compare' => '=' ); } @@ -513,14 +511,14 @@ class WC_Shortcodes { ), $atts ); $query_args = array( - 'posts_per_page' => $atts['per_page'], - 'orderby' => $atts['orderby'], - 'order' => $atts['order'], - 'no_found_rows' => 1, - 'post_status' => 'publish', - 'post_type' => 'product', - 'meta_query' => WC()->query->get_meta_query(), - 'post__in' => array_merge( array( 0 ), wc_get_product_ids_on_sale() ) + 'posts_per_page' => $atts['per_page'], + 'orderby' => $atts['orderby'], + 'order' => $atts['order'], + 'no_found_rows' => 1, + 'post_status' => 'publish', + 'post_type' => 'product', + 'meta_query' => WC()->query->get_meta_query(), + 'post__in' => array_merge( array( 0 ), wc_get_product_ids_on_sale() ) ); return self::product_loop( $query_args, $atts, 'sale_products' ); @@ -535,7 +533,9 @@ class WC_Shortcodes { public static function best_selling_products( $atts ) { $atts = shortcode_atts( array( 'per_page' => '12', - 'columns' => '4' + 'columns' => '4', + 'category' => '', // Slugs + 'operator' => 'IN' // Possible values are 'IN', 'NOT IN', 'AND'. ), $atts ); $query_args = array( @@ -548,6 +548,8 @@ class WC_Shortcodes { 'meta_query' => WC()->query->get_meta_query() ); + $query_args = self::_maybe_add_category_args( $query_args, $atts['category'], $atts['operator'] ); + return self::product_loop( $query_args, $atts, 'best_selling_products' ); } @@ -562,7 +564,9 @@ class WC_Shortcodes { 'per_page' => '12', 'columns' => '4', 'orderby' => 'title', - 'order' => 'asc' + 'order' => 'asc', + 'category' => '', // Slugs + 'operator' => 'IN' // Possible values are 'IN', 'NOT IN', 'AND'. ), $atts ); $query_args = array( @@ -575,6 +579,8 @@ class WC_Shortcodes { 'meta_query' => WC()->query->get_meta_query() ); + $query_args = self::_maybe_add_category_args( $query_args, $atts['category'], $atts['operator'] ); + ob_start(); add_filter( 'posts_clauses', array( __CLASS__, 'order_by_rating_post_clauses' ) ); @@ -597,7 +603,9 @@ class WC_Shortcodes { 'per_page' => '12', 'columns' => '4', 'orderby' => 'date', - 'order' => 'desc' + 'order' => 'desc', + 'category' => '', // Slugs + 'operator' => 'IN' // Possible values are 'IN', 'NOT IN', 'AND'. ), $atts ); $meta_query = WC()->query->get_meta_query(); @@ -616,6 +624,8 @@ class WC_Shortcodes { 'meta_query' => $meta_query ); + $query_args = self::_maybe_add_category_args( $query_args, $atts['category'], $atts['operator'] ); + return self::product_loop( $query_args, $atts, 'featured_products' ); } @@ -792,4 +802,28 @@ class WC_Shortcodes { return ob_get_clean(); } + + /** + * Adds a tax_query index to the query to filter by category. + * + * @param array $args + * @param string $category + * @param string $operator + * @return array; + * @access private + */ + private static function _maybe_add_category_args( $args, $category, $operator ) { + if ( ! empty( $category ) ) { + $args['tax_query'] = array( + array( + 'taxonomy' => 'product_cat', + 'terms' => array_map( 'sanitize_title', explode( ',', $category ) ), + 'field' => 'slug', + 'operator' => $operator + ) + ); + } + + return $args; + } }