rebase shortcakes category

This commit is contained in:
Nicola Mustone 2015-09-16 09:26:30 +02:00
parent 2a31d1fe98
commit ffc941fcb1
1 changed files with 74 additions and 38 deletions

View File

@ -178,17 +178,11 @@ class WC_Shortcodes {
'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']
)
)
'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'];
}
@ -294,7 +288,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 );
$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' );
}
@ -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,10 @@ 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' ) );
$return = self::product_loop( $query_args, $atts, 'top_rated_products' );
@ -595,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();
@ -614,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' );
}
@ -790,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;
}
}