added args category to some shortcodes

This commit is contained in:
Nicola Mustone 2015-08-13 14:37:21 +02:00
parent fb64e6e66e
commit 1fc762c0b6
1 changed files with 72 additions and 38 deletions

View File

@ -178,17 +178,11 @@ class WC_Shortcodes {
'orderby' => $ordering_args['orderby'], 'orderby' => $ordering_args['orderby'],
'order' => $ordering_args['order'], 'order' => $ordering_args['order'],
'posts_per_page' => $atts['per_page'], 'posts_per_page' => $atts['per_page'],
'meta_query' => $meta_query, 'meta_query' => $meta_query
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'terms' => array_map( 'sanitize_title', explode( ',', $atts['category'] ) ),
'field' => 'slug',
'operator' => $atts['operator']
)
)
); );
$query_args = self::_maybe_add_category_args( $query_args, $atts['category'], $atts['operator'] );
if ( isset( $ordering_args['meta_key'] ) ) { if ( isset( $ordering_args['meta_key'] ) ) {
$query_args['meta_key'] = $ordering_args['meta_key']; $query_args['meta_key'] = $ordering_args['meta_key'];
} }
@ -294,7 +288,9 @@ class WC_Shortcodes {
'per_page' => '12', 'per_page' => '12',
'columns' => '4', 'columns' => '4',
'orderby' => 'date', 'orderby' => 'date',
'order' => 'desc' 'order' => 'desc',
'category' => '', // Slugs
'operator' => 'IN' // Possible values are 'IN', 'NOT IN', 'AND'.
), $atts ); ), $atts );
$query_args = array( $query_args = array(
@ -307,6 +303,8 @@ class WC_Shortcodes {
'meta_query' => WC()->query->get_meta_query() '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' ); return self::product_loop( $query_args, $atts, 'recent_products' );
} }
@ -535,7 +533,9 @@ class WC_Shortcodes {
public static function best_selling_products( $atts ) { public static function best_selling_products( $atts ) {
$atts = shortcode_atts( array( $atts = shortcode_atts( array(
'per_page' => '12', 'per_page' => '12',
'columns' => '4' 'columns' => '4',
'category' => '', // Slugs
'operator' => 'IN' // Possible values are 'IN', 'NOT IN', 'AND'.
), $atts ); ), $atts );
$query_args = array( $query_args = array(
@ -548,6 +548,8 @@ class WC_Shortcodes {
'meta_query' => WC()->query->get_meta_query() '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' ); return self::product_loop( $query_args, $atts, 'best_selling_products' );
} }
@ -562,7 +564,9 @@ class WC_Shortcodes {
'per_page' => '12', 'per_page' => '12',
'columns' => '4', 'columns' => '4',
'orderby' => 'title', 'orderby' => 'title',
'order' => 'asc' 'order' => 'asc',
'category' => '', // Slugs
'operator' => 'IN' // Possible values are 'IN', 'NOT IN', 'AND'.
), $atts ); ), $atts );
$query_args = array( $query_args = array(
@ -575,6 +579,8 @@ class WC_Shortcodes {
'meta_query' => WC()->query->get_meta_query() 'meta_query' => WC()->query->get_meta_query()
); );
$query_args = self::_maybe_add_category_args( $query_args, $atts['category'], $atts['operator'] );
ob_start(); ob_start();
add_filter( 'posts_clauses', array( __CLASS__, 'order_by_rating_post_clauses' ) ); add_filter( 'posts_clauses', array( __CLASS__, 'order_by_rating_post_clauses' ) );
@ -597,7 +603,9 @@ class WC_Shortcodes {
'per_page' => '12', 'per_page' => '12',
'columns' => '4', 'columns' => '4',
'orderby' => 'date', 'orderby' => 'date',
'order' => 'desc' 'order' => 'desc',
'category' => '', // Slugs
'operator' => 'IN' // Possible values are 'IN', 'NOT IN', 'AND'.
), $atts ); ), $atts );
$meta_query = WC()->query->get_meta_query(); $meta_query = WC()->query->get_meta_query();
@ -616,6 +624,8 @@ class WC_Shortcodes {
'meta_query' => $meta_query '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' ); return self::product_loop( $query_args, $atts, 'featured_products' );
} }
@ -792,4 +802,28 @@ class WC_Shortcodes {
return ob_get_clean(); 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;
}
} }