Use WC_Shortcode_Products inside top_rated_products shortcode

This commit is contained in:
Claudio Sanches 2017-08-25 15:55:34 -03:00
parent c0da59d75a
commit 72df581c6f
3 changed files with 79 additions and 38 deletions

View File

@ -454,35 +454,18 @@ class WC_Shortcodes {
* @return string
*/
public static function top_rated_products( $atts ) {
$atts = shortcode_atts( array(
$atts = array_merge( array(
'per_page' => '12',
'columns' => '4',
'orderby' => 'title',
'order' => 'asc',
'category' => '', // Slugs
'operator' => 'IN', // Possible values are 'IN', 'NOT IN', 'AND'.
), $atts, 'top_rated_products' );
'order' => 'ASC',
'category' => '',
'operator' => 'IN',
), (array) $atts );
$query_args = array(
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'orderby' => $atts['orderby'],
'order' => $atts['order'],
'posts_per_page' => $atts['per_page'],
'meta_query' => WC()->query->get_meta_query(),
'tax_query' => WC()->query->get_tax_query(),
);
$shortcode = new WC_Shortcode_Products( $atts, 'top_rated_products' );
$query_args = self::_maybe_add_category_args( $query_args, $atts['category'], $atts['operator'] );
add_filter( 'posts_clauses', array( __CLASS__, 'order_by_rating_post_clauses' ) );
$return = self::product_loop( $query_args, $atts, 'top_rated_products' );
remove_filter( 'posts_clauses', array( __CLASS__, 'order_by_rating_post_clauses' ) );
return $return;
return $shortcode->get_content();
}
/**
@ -641,20 +624,14 @@ class WC_Shortcodes {
}
/**
* woocommerce_order_by_rating_post_clauses function.
* Order by rating.
*
* @param array $args
* @return array
* @deprecated 3.2.0 Use WC_Shortcode_Products::order_by_rating_post_clauses().
* @param array $args Query args.
* @return array
*/
public static function order_by_rating_post_clauses( $args ) {
global $wpdb;
$args['where'] .= " AND $wpdb->commentmeta.meta_key = 'rating' ";
$args['join'] .= "LEFT JOIN $wpdb->comments ON($wpdb->posts.ID = $wpdb->comments.comment_post_ID) LEFT JOIN $wpdb->commentmeta ON($wpdb->comments.comment_ID = $wpdb->commentmeta.comment_id)";
$args['orderby'] = "$wpdb->commentmeta.meta_value DESC";
$args['groupby'] = "$wpdb->posts.ID";
return $args;
return WC_Shortcode_Products::order_by_rating_post_clauses( $args );
}
/**

View File

@ -206,7 +206,14 @@ class WC_Shortcode_Products {
$products = get_transient( $transient_name );
if ( false === $products || ! is_a( $products, 'WP_Query' ) ) {
$products = new WP_Query( $this->query_args );
if ( 'top_rated_products' === $this->type ) {
add_filter( 'posts_clauses', array( __CLASS__, 'order_by_rating_post_clauses' ) );
$products = new WP_Query( $this->query_args );
remove_filter( 'posts_clauses', array( __CLASS__, 'order_by_rating_post_clauses' ) );
} else {
$products = new WP_Query( $this->query_args );
}
set_transient( $transient_name, $products, DAY_IN_SECONDS * 30 );
}
@ -248,4 +255,22 @@ class WC_Shortcode_Products {
return '<div class="' . esc_attr( implode( ' ', $classes ) ) . '">' . ob_get_clean() . '</div>';
}
/**
* Order by rating.
*
* @since 3.2.0
* @param array $args Query args.
* @return array
*/
public static function order_by_rating_post_clauses( $args ) {
global $wpdb;
$args['where'] .= " AND $wpdb->commentmeta.meta_key = 'rating' ";
$args['join'] .= "LEFT JOIN $wpdb->comments ON($wpdb->posts.ID = $wpdb->comments.comment_post_ID) LEFT JOIN $wpdb->commentmeta ON($wpdb->comments.comment_ID = $wpdb->commentmeta.comment_id)";
$args['orderby'] = "$wpdb->commentmeta.meta_value DESC";
$args['groupby'] = "$wpdb->posts.ID";
return $args;
}
}

View File

@ -224,6 +224,29 @@ class WC_Test_Shortcode_Products extends WC_Unit_Test_Case {
);
$this->assertEquals( $expected7, $shortcode7->get_query_args() );
// top_rated_products shortcode.
$shortcode8 = new WC_Shortcode_Products( array(
'per_page' => '12',
'columns' => '4',
'orderby' => 'title',
'order' => 'ASC',
'category' => '',
'operator' => 'IN',
), 'top_rated_products' );
$expected8 = array(
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => true,
'no_found_rows' => true,
'orderby' => 'title',
'order' => 'ASC',
'posts_per_page' => 12,
'meta_query' => $meta_query,
'tax_query' => $tax_query,
);
$this->assertEquals( $expected8, $shortcode8->get_query_args() );
}
/**
@ -240,8 +263,24 @@ class WC_Test_Shortcode_Products extends WC_Unit_Test_Case {
*/
public function test_get_content() {
$shortcode = new WC_Shortcode_Products();
$results = $shortcode->get_content();
$result = $shortcode->get_content();
$this->assertTrue( ! empty( $results ) );
$this->assertTrue( ! empty( $result ) );
}
/**
* Test: WC_Shortcode_Products::order_by_rating_post_clauses.
*/
public function test_order_by_rating_post_clauses() {
global $wpdb;
$expected = array(
'where' => " AND $wpdb->commentmeta.meta_key = 'rating' ",
'join' => "LEFT JOIN $wpdb->comments ON($wpdb->posts.ID = $wpdb->comments.comment_post_ID) LEFT JOIN $wpdb->commentmeta ON($wpdb->comments.comment_ID = $wpdb->commentmeta.comment_id)",
'orderby' => "$wpdb->commentmeta.meta_value DESC",
'groupby' => "$wpdb->posts.ID",
);
$this->assertEquals( $expected, WC_Shortcode_Products::order_by_rating_post_clauses( array( 'where' => '', 'join' => '' ) ) );
}
}