Tag support in shortcode

This commit is contained in:
claudiulodro 2017-11-30 13:43:32 -08:00
parent 8d6d4c4fd9
commit 22031c5b56
2 changed files with 28 additions and 9 deletions

View File

@ -237,19 +237,17 @@ class WC_Template_Loader {
global $wp_query, $post;
$queried_object = get_queried_object();
$shortcode_args = array(
);
$shortcode_args = array();
if ( is_product_category() ) {
$shortcode_args['category'] = $queried_object->slug;
$shortcode_args['category'] = sanitize_title( $queried_object->slug );
} elseif ( taxonomy_is_product_attribute( $queried_object->taxonomy ) ) {
$shortcode_args['attribute'] = $queried_object->taxonomy;
$shortcode_args['terms'] = $queried_object->slug;
$shortcode_args['attribute'] = sanitize_title( $queried_object->taxonomy );
$shortcode_args['terms'] = sanitize_title( $queried_object->slug );
} elseif ( is_product_tag() ) {
// todo
die( "NOT IMPLEMENTED" );
$shortcode_args['tag'] = sanitize_title( $queried_object->slug );
} else {
// Default theme archive for all other taxonomies.
return;
}
@ -267,7 +265,7 @@ class WC_Template_Loader {
'post_modified' => $shop_page->post_modified,
'post_modified_gmt' => $shop_page->post_modified_gmt,
'post_content' => $shortcode->get_content(),
'post_title' => $queried_object->name,
'post_title' => wc_clean( $queried_object->name ),
'post_excerpt' => '',
'post_content_filtered' => '',
'post_mime_type' => '',

View File

@ -125,6 +125,7 @@ class WC_Shortcode_Products {
'attribute' => '', // Single attribute slug.
'terms' => '', // Comma separated term slugs.
'terms_operator' => 'IN', // Operator to compare terms. Possible values are 'IN', 'NOT IN', 'AND'.
'tag' => '', // Comma separated tag slugs.
'visibility' => 'visible', // Possible values are 'visible', 'catalog', 'search', 'hidden'.
'class' => '', // HTML class.
'page' => 1, // Page for pagination.
@ -216,6 +217,9 @@ class WC_Shortcode_Products {
// Categories.
$this->set_categories_query_args( $query_args );
// Tags.
$this->set_tags_query_args( $query_args );
return apply_filters( 'woocommerce_shortcode_products_query', $query_args, $this->attributes, $this->type );
}
@ -288,6 +292,23 @@ class WC_Shortcode_Products {
}
}
/**
* Set tags query args.
*
* @since 3.3.0
* @param array $query_args Query args.
*/
protected function set_tags_query_args( &$query_args ) {
if ( ! empty( $this->attributes['tag'] ) ) {
$query_args['tax_query'][] = array(
'taxonomy' => 'product_tag',
'terms' => array_map( 'sanitize_title', explode( ',', $this->attributes['tag'] ) ),
'field' => 'slug',
'operator' => 'IN',
);
}
}
/**
* Set sale products query args.
*