Apply WC_Shortcode_Products class into sale_products shortcode

This commit is contained in:
Claudio Sanches 2017-08-25 11:00:48 -03:00
parent 0cea847c63
commit 3dabdda480
3 changed files with 38 additions and 21 deletions

View File

@ -414,30 +414,18 @@ class WC_Shortcodes {
* @return string
*/
public static function sale_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, 'sale_products' );
'order' => 'ASC',
'category' => '',
'operator' => 'IN',
), (array) $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(),
'tax_query' => WC()->query->get_tax_query(),
'post__in' => array_merge( array( 0 ), wc_get_product_ids_on_sale() ),
);
$shortcode = new WC_Shortcode_Products( $atts, 'sale_products' );
$query_args = self::_maybe_add_category_args( $query_args, $atts['category'], $atts['operator'] );
return self::product_loop( $query_args, $atts, 'sale_products' );
return $shortcode->get_content();
}
/**

View File

@ -175,6 +175,11 @@ class WC_Shortcode_Products {
}
}
// On sale.
if ( 'sale_products' === $this->type ) {
$query_args['post__in'] = array_merge( array( 0 ), wc_get_product_ids_on_sale() );
}
return apply_filters( 'woocommerce_shortcode_products_query', $query_args, $this->attributes, $this->type );
}

View File

@ -160,11 +160,11 @@ class WC_Test_Shortcode_Products extends WC_Unit_Test_Case {
$this->assertEquals( $expected4, $shortcode4->get_query_args() );
// product shortcode.
$shortcode4 = new WC_Shortcode_Products( array(
$shortcode5 = new WC_Shortcode_Products( array(
'ids' => '1',
'per_page' => '1',
), 'product' );
$expected4 = array(
$expected5 = array(
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => true,
@ -177,6 +177,30 @@ class WC_Test_Shortcode_Products extends WC_Unit_Test_Case {
'p' => '1',
);
$this->assertEquals( $expected5, $shortcode5->get_query_args() );
// sale_products shortcode.
$shortcode4 = new WC_Shortcode_Products( array(
'per_page' => '12',
'columns' => '4',
'orderby' => 'title',
'order' => 'ASC',
'category' => '',
'operator' => 'IN',
), 'sale_products' );
$expected4 = 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,
'post__in' => array_merge( array( 0 ), wc_get_product_ids_on_sale() ),
);
$this->assertEquals( $expected4, $shortcode4->get_query_args() );
}