Render product_attribute shortcode results with WC_Shortcode_Products
This commit is contained in:
parent
ddeea547b1
commit
32ea76f09f
|
@ -622,33 +622,22 @@ class WC_Shortcodes {
|
|||
* @return string
|
||||
*/
|
||||
public static function product_attribute( $atts ) {
|
||||
$atts = shortcode_atts( array(
|
||||
$atts = array_merge( array(
|
||||
'per_page' => '12',
|
||||
'columns' => '4',
|
||||
'orderby' => 'title',
|
||||
'order' => 'asc',
|
||||
'attribute' => '',
|
||||
'filter' => '',
|
||||
), $atts, 'product_attribute' );
|
||||
), (array) $atts );
|
||||
|
||||
$query_args = array(
|
||||
'post_type' => 'product',
|
||||
'post_status' => 'publish',
|
||||
'ignore_sticky_posts' => 1,
|
||||
'posts_per_page' => $atts['per_page'],
|
||||
'orderby' => $atts['orderby'],
|
||||
'order' => $atts['order'],
|
||||
'meta_query' => WC()->query->get_meta_query(),
|
||||
'tax_query' => WC()->query->get_tax_query(),
|
||||
);
|
||||
if ( empty( $atts['attribute'] ) || empty( $atts['filter'] ) ) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$query_args['tax_query'][] = array(
|
||||
'taxonomy' => strstr( $atts['attribute'], 'pa_' ) ? sanitize_title( $atts['attribute'] ) : 'pa_' . sanitize_title( $atts['attribute'] ),
|
||||
'terms' => array_map( 'sanitize_title', explode( ',', $atts['filter'] ) ),
|
||||
'field' => 'slug',
|
||||
);
|
||||
$shortcode = new WC_Shortcode_Products( $atts, 'product_attribute' );
|
||||
|
||||
return self::product_loop( $query_args, $atts, 'product_attribute' );
|
||||
return $shortcode->get_content();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -102,15 +102,17 @@ class WC_Shortcode_Products {
|
|||
*/
|
||||
protected function parse_attributes( $attributes ) {
|
||||
return shortcode_atts( array(
|
||||
'per_page' => '-1',
|
||||
'columns' => '4',
|
||||
'orderby' => 'title',
|
||||
'order' => 'ASC',
|
||||
'ids' => '',
|
||||
'skus' => '',
|
||||
'category' => '', // Slugs.
|
||||
'operator' => 'IN', // Category operator. Possible values are 'IN', 'NOT IN', 'AND'.
|
||||
'class' => '',
|
||||
'per_page' => '-1',
|
||||
'columns' => '4',
|
||||
'orderby' => 'title',
|
||||
'order' => 'ASC',
|
||||
'ids' => '',
|
||||
'skus' => '',
|
||||
'category' => '', // Slugs.
|
||||
'operator' => 'IN', // Category operator. Possible values are 'IN', 'NOT IN', 'AND'.
|
||||
'class' => '',
|
||||
'attribute' => '',
|
||||
'filter' => '',
|
||||
), $attributes, $this->type );
|
||||
}
|
||||
|
||||
|
@ -173,12 +175,21 @@ class WC_Shortcode_Products {
|
|||
if ( 'featured_products' === $this->type ) {
|
||||
$query_args['tax_query'][] = array(
|
||||
'taxonomy' => 'product_visibility',
|
||||
'field' => 'name',
|
||||
'terms' => 'featured',
|
||||
'field' => 'name',
|
||||
'operator' => 'IN',
|
||||
);
|
||||
}
|
||||
|
||||
// Attributes.
|
||||
if ( ! empty( $this->attributes['attribute'] ) || ! empty( $this->attributes['filter'] ) ) {
|
||||
$query_args['tax_query'][] = array(
|
||||
'taxonomy' => strstr( $this->attributes['attribute'], 'pa_' ) ? sanitize_title( $this->attributes['attribute'] ) : 'pa_' . sanitize_title( $this->attributes['attribute'] ),
|
||||
'terms' => array_map( 'sanitize_title', explode( ',', $this->attributes['filter'] ) ),
|
||||
'field' => 'slug',
|
||||
);
|
||||
}
|
||||
|
||||
// Categories.
|
||||
if ( ! empty( $this->attributes['category'] ) ) {
|
||||
$ordering_args = WC()->query->get_catalog_ordering_args( $query_args['orderby'], $query_args['order'] );
|
||||
|
|
|
@ -13,15 +13,17 @@ class WC_Test_Shortcode_Products extends WC_Unit_Test_Case {
|
|||
public function test_get_attributes() {
|
||||
$shortcode = new WC_Shortcode_Products();
|
||||
$expected = array(
|
||||
'per_page' => '-1',
|
||||
'columns' => '4',
|
||||
'orderby' => 'title',
|
||||
'order' => 'ASC',
|
||||
'ids' => '',
|
||||
'skus' => '',
|
||||
'category' => '',
|
||||
'operator' => 'IN',
|
||||
'class' => '',
|
||||
'per_page' => '-1',
|
||||
'columns' => '4',
|
||||
'orderby' => 'title',
|
||||
'order' => 'ASC',
|
||||
'ids' => '',
|
||||
'skus' => '',
|
||||
'category' => '',
|
||||
'operator' => 'IN',
|
||||
'class' => '',
|
||||
'attribute' => '',
|
||||
'filter' => '',
|
||||
);
|
||||
$this->assertEquals( $expected, $shortcode->get_attributes() );
|
||||
|
||||
|
@ -30,15 +32,17 @@ class WC_Test_Shortcode_Products extends WC_Unit_Test_Case {
|
|||
'order' => 'DESC',
|
||||
) );
|
||||
$expected2 = array(
|
||||
'per_page' => '-1',
|
||||
'columns' => '4',
|
||||
'orderby' => 'id',
|
||||
'order' => 'DESC',
|
||||
'ids' => '',
|
||||
'skus' => '',
|
||||
'category' => '',
|
||||
'operator' => 'IN',
|
||||
'class' => '',
|
||||
'per_page' => '-1',
|
||||
'columns' => '4',
|
||||
'orderby' => 'id',
|
||||
'order' => 'DESC',
|
||||
'ids' => '',
|
||||
'skus' => '',
|
||||
'category' => '',
|
||||
'operator' => 'IN',
|
||||
'class' => '',
|
||||
'attribute' => '',
|
||||
'filter' => '',
|
||||
);
|
||||
$this->assertEquals( $expected2, $shortcode2->get_attributes() );
|
||||
}
|
||||
|
@ -274,7 +278,34 @@ class WC_Test_Shortcode_Products extends WC_Unit_Test_Case {
|
|||
) ),
|
||||
);
|
||||
|
||||
$this->assertEquals( $expected9, $shortcode9->get_query_args() );
|
||||
// product_attribute shortcode.
|
||||
$shortcode10 = new WC_Shortcode_Products( array(
|
||||
'per_page' => '12',
|
||||
'columns' => '4',
|
||||
'orderby' => 'title',
|
||||
'order' => 'asc',
|
||||
'attribute' => 'color',
|
||||
'filter' => 'black',
|
||||
), 'product_attribute' );
|
||||
$expected10 = 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' => array_merge( $tax_query, array(
|
||||
array(
|
||||
'taxonomy' => 'pa_color',
|
||||
'terms' => array( 'black' ),
|
||||
'field' => 'slug',
|
||||
),
|
||||
) ),
|
||||
);
|
||||
|
||||
$this->assertEquals( $expected10, $shortcode10->get_query_args() );
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in New Issue