Use WC_Shortcode_Products to render product shortcode results

This commit is contained in:
Claudio Sanches 2017-08-24 22:16:42 -03:00
parent 013da90f51
commit d7bea55aae
3 changed files with 59 additions and 61 deletions

View File

@ -311,56 +311,12 @@ class WC_Shortcodes {
return '';
}
$meta_query = WC()->query->get_meta_query();
$atts['skus'] = isset( $atts['sku'] ) ? $atts['sku'] : '';
$atts['ids'] = isset( $atts['id'] ) ? $atts['id'] : '';
$atts['per_page'] = '1';
$shortcode = new WC_Shortcode_Products( (array) $atts, 'product' );
$args = array(
'post_type' => 'product',
'posts_per_page' => 1,
'no_found_rows' => 1,
'post_status' => 'publish',
'meta_query' => $meta_query,
'tax_query' => WC()->query->get_tax_query(),
);
if ( isset( $atts['sku'] ) ) {
$args['meta_query'][] = array(
'key' => '_sku',
'value' => $atts['sku'],
'compare' => '=',
);
}
if ( isset( $atts['id'] ) ) {
$args['p'] = $atts['id'];
}
ob_start();
$products = new WP_Query( apply_filters( 'woocommerce_shortcode_products_query', $args, $atts, null ) );
if ( $products->have_posts() ) : ?>
<?php woocommerce_product_loop_start(); ?>
<?php while ( $products->have_posts() ) : $products->the_post(); ?>
<?php wc_get_template_part( 'content', 'product' ); ?>
<?php endwhile; // end of the loop. ?>
<?php woocommerce_product_loop_end(); ?>
<?php endif;
wp_reset_postdata();
$css_class = 'woocommerce';
if ( isset( $atts['class'] ) ) {
$css_class .= ' ' . $atts['class'];
}
return '<div class="' . esc_attr( $css_class ) . '">' . ob_get_clean() . '</div>';
return $shortcode->get_content();
}
/**

View File

@ -110,6 +110,7 @@ class WC_Shortcode_Products {
'skus' => '',
'category' => '', // Slugs.
'operator' => 'IN', // Category operator. Possible values are 'IN', 'NOT IN', 'AND'.
'class' => '',
), $attributes, $this->loop_name );
}
@ -123,7 +124,8 @@ class WC_Shortcode_Products {
$query_args = array(
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'ignore_sticky_posts' => true,
'no_found_rows' => true,
'orderby' => $this->attributes['orderby'],
'order' => strtoupper( $this->attributes['order'] ),
);
@ -154,16 +156,23 @@ class WC_Shortcode_Products {
// SKUs.
if ( ! empty( $this->attributes['skus'] ) ) {
$skus = array_map( 'trim', explode( ',', $this->attributes['skus'] ) );
$query_args['meta_query'][] = array(
'key' => '_sku',
'value' => array_map( 'trim', explode( ',', $this->attributes['skus'] ) ),
'compare' => 'IN',
'value' => 1 === count( $skus ) ? $skus[0] : $skus,
'compare' => 1 === count( $skus ) ? '=' : 'IN',
);
}
// IDs.
if ( ! empty( $this->attributes['ids'] ) ) {
$query_args['post__in'] = array_map( 'trim', explode( ',', $this->attributes['ids'] ) );
$ids = array_map( 'trim', explode( ',', $this->attributes['ids'] ) );
if ( 1 === count( $ids ) ) {
$query_args['p'] = $ids[0];
} else {
$query_args['post__in'] = $ids;
}
}
return apply_filters( 'woocommerce_shortcode_products_query', $query_args, $this->attributes, $this->loop_name );
@ -219,6 +228,12 @@ class WC_Shortcode_Products {
woocommerce_reset_loop();
wp_reset_postdata();
return '<div class="woocommerce columns-' . $columns . '">' . ob_get_clean() . '</div>';
$classes = array( 'woocommerce' );
if ( 'product' !== $this->loop_name ) {
$classes[] = 'columns-' . $columns;
}
$classes[] = $this->attributes['class'];
return '<div class="' . esc_attr( implode( ' ', $classes ) ) . '">' . ob_get_clean() . '</div>';
}
}

View File

@ -21,6 +21,7 @@ class WC_Test_Shortcode_Products extends WC_Unit_Test_Case {
'skus' => '',
'category' => '',
'operator' => 'IN',
'class' => '',
);
$this->assertEquals( $expected, $shortcode->get_attributes() );
@ -37,6 +38,7 @@ class WC_Test_Shortcode_Products extends WC_Unit_Test_Case {
'skus' => '',
'category' => '',
'operator' => 'IN',
'class' => '',
);
$this->assertEquals( $expected2, $shortcode2->get_attributes() );
}
@ -53,7 +55,8 @@ class WC_Test_Shortcode_Products extends WC_Unit_Test_Case {
$expected = array(
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'ignore_sticky_posts' => true,
'no_found_rows' => true,
'orderby' => 'title',
'order' => 'ASC',
'posts_per_page' => -1,
@ -70,7 +73,8 @@ class WC_Test_Shortcode_Products extends WC_Unit_Test_Case {
$expected2 = array(
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'ignore_sticky_posts' => true,
'no_found_rows' => true,
'orderby' => 'id',
'order' => 'DESC',
'posts_per_page' => -1,
@ -86,7 +90,8 @@ class WC_Test_Shortcode_Products extends WC_Unit_Test_Case {
$expected2 = array(
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'ignore_sticky_posts' => true,
'no_found_rows' => true,
'orderby' => 'title',
'order' => 'ASC',
'posts_per_page' => -1,
@ -110,11 +115,12 @@ class WC_Test_Shortcode_Products extends WC_Unit_Test_Case {
'order' => 'ASC',
'category' => 'clothing',
'operator' => 'IN',
) );
), 'product_category' );
$expected3 = array(
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'ignore_sticky_posts' => true,
'no_found_rows' => true,
'orderby' => 'menu_order title',
'order' => 'ASC',
'posts_per_page' => 12,
@ -138,11 +144,12 @@ class WC_Test_Shortcode_Products extends WC_Unit_Test_Case {
'order' => 'DESC',
'category' => '',
'operator' => 'IN',
) );
), 'recent_products' );
$expected4 = array(
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'ignore_sticky_posts' => true,
'no_found_rows' => true,
'orderby' => 'date',
'order' => 'DESC',
'posts_per_page' => 12,
@ -151,6 +158,26 @@ 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(
'ids' => '1',
'per_page' => '1',
), 'product' );
$expected4 = array(
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => true,
'no_found_rows' => true,
'orderby' => 'title',
'order' => 'ASC',
'posts_per_page' => 1,
'meta_query' => $meta_query,
'tax_query' => $tax_query,
'p' => '1',
);
$this->assertEquals( $expected4, $shortcode4->get_query_args() );
}
/**