2017-08-24 18:29:30 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Products shortcode
|
|
|
|
*
|
|
|
|
* @author Automattic
|
|
|
|
* @category Shortcodes
|
|
|
|
* @package WooCommerce/Shortcodes
|
|
|
|
* @version 3.2.0
|
|
|
|
*/
|
|
|
|
|
|
|
|
if ( ! defined( 'ABSPATH' ) ) {
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Products shortcode class.
|
|
|
|
*/
|
|
|
|
class WC_Shortcode_Products {
|
|
|
|
|
|
|
|
/**
|
2017-08-25 01:22:42 +00:00
|
|
|
* Shortcode type.
|
2017-08-24 18:29:30 +00:00
|
|
|
*
|
|
|
|
* @since 3.2.0
|
|
|
|
* @var string
|
|
|
|
*/
|
2017-08-25 01:22:42 +00:00
|
|
|
protected $type = 'products';
|
2017-08-24 18:29:30 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Attributes.
|
|
|
|
*
|
|
|
|
* @since 3.2.0
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $attributes = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Query args.
|
|
|
|
*
|
|
|
|
* @since 3.2.0
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $query_args = array();
|
|
|
|
|
2017-08-29 21:29:29 +00:00
|
|
|
/**
|
|
|
|
* Set custom visibility.
|
|
|
|
*
|
|
|
|
* @since 3.2.0
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
protected $custom_visibility = false;
|
|
|
|
|
2017-08-24 18:29:30 +00:00
|
|
|
/**
|
|
|
|
* Initialize shortcode.
|
|
|
|
*
|
|
|
|
* @since 3.2.0
|
|
|
|
* @param array $attributes Shortcode attributes.
|
2017-08-25 01:22:42 +00:00
|
|
|
* @param array $type Shortcode type.
|
2017-08-24 18:29:30 +00:00
|
|
|
*/
|
2017-08-25 01:22:42 +00:00
|
|
|
public function __construct( $attributes = array(), $type = 'products' ) {
|
|
|
|
$this->type = $type;
|
2017-08-25 00:24:05 +00:00
|
|
|
$this->attributes = $this->parse_attributes( $attributes );
|
|
|
|
$this->query_args = $this->parse_query_args();
|
2017-08-24 18:29:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-08-24 23:11:18 +00:00
|
|
|
* Get shortcode attributes.
|
2017-08-24 18:29:30 +00:00
|
|
|
*
|
|
|
|
* @since 3.2.0
|
|
|
|
* @return array
|
|
|
|
*/
|
2017-08-24 23:11:18 +00:00
|
|
|
public function get_attributes() {
|
|
|
|
return $this->attributes;
|
2017-08-24 18:29:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-08-24 23:11:18 +00:00
|
|
|
* Get query args.
|
2017-08-24 18:29:30 +00:00
|
|
|
*
|
|
|
|
* @since 3.2.0
|
|
|
|
* @return array
|
|
|
|
*/
|
2017-08-24 23:11:18 +00:00
|
|
|
public function get_query_args() {
|
|
|
|
return $this->query_args;
|
2017-08-24 18:29:30 +00:00
|
|
|
}
|
|
|
|
|
2017-08-24 23:20:14 +00:00
|
|
|
/**
|
2017-08-25 01:22:42 +00:00
|
|
|
* Get shortcode type.
|
2017-08-24 23:20:14 +00:00
|
|
|
*
|
|
|
|
* @since 3.2.0
|
|
|
|
* @return array
|
|
|
|
*/
|
2017-08-25 01:22:42 +00:00
|
|
|
public function get_type() {
|
|
|
|
return $this->type;
|
2017-08-24 23:20:14 +00:00
|
|
|
}
|
|
|
|
|
2017-08-24 18:29:30 +00:00
|
|
|
/**
|
2017-08-24 22:33:14 +00:00
|
|
|
* Get shortcode content.
|
2017-08-24 18:29:30 +00:00
|
|
|
*
|
|
|
|
* @since 3.2.0
|
|
|
|
* @return string
|
|
|
|
*/
|
2017-08-24 23:11:18 +00:00
|
|
|
public function get_content() {
|
2017-08-24 18:30:44 +00:00
|
|
|
return $this->product_loop();
|
2017-08-24 18:29:30 +00:00
|
|
|
}
|
|
|
|
|
2017-08-25 00:24:05 +00:00
|
|
|
/**
|
|
|
|
* Parse attributes.
|
|
|
|
*
|
2017-08-28 20:45:56 +00:00
|
|
|
* @since 3.2.0
|
2017-08-25 00:24:05 +00:00
|
|
|
* @param array $attributes Shortcode attributes.
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function parse_attributes( $attributes ) {
|
2017-08-28 20:45:56 +00:00
|
|
|
$attributes = $this->parse_legacy_attributes( $attributes );
|
|
|
|
|
2017-08-25 00:24:05 +00:00
|
|
|
return shortcode_atts( array(
|
2017-08-29 21:29:29 +00:00
|
|
|
'limit' => '-1', // Results limit.
|
|
|
|
'columns' => '4', // Number of columns.
|
|
|
|
'orderby' => 'title', // menu_order, title, date, rand, price, popularity, rating, or id.
|
|
|
|
'order' => 'ASC', // ASC or DESC.
|
|
|
|
'ids' => '', // Comma separated IDs.
|
|
|
|
'skus' => '', // Comma separated SKUs.
|
|
|
|
'category' => '', // Comma separated category slugs.
|
|
|
|
'cat_operator' => 'IN', // Operator to compare categories. Possible values are 'IN', 'NOT IN', 'AND'.
|
|
|
|
'attribute' => '', // Single attribute slug.
|
|
|
|
'terms' => '', // Comma separated term slugs.
|
|
|
|
'terms_operator' => 'IN', // Operator to compare terms. Possible values are 'IN', 'NOT IN', 'AND'.
|
|
|
|
'visibility' => 'visible', // Possible values are 'visible', 'catalog', 'search', 'hidden'.
|
|
|
|
'class' => '', // HTML class.
|
2017-08-25 01:22:42 +00:00
|
|
|
), $attributes, $this->type );
|
2017-08-25 00:24:05 +00:00
|
|
|
}
|
|
|
|
|
2017-08-28 20:45:56 +00:00
|
|
|
/**
|
|
|
|
* Parse legacy attributes.
|
|
|
|
*
|
|
|
|
* @since 3.2.0
|
|
|
|
* @param array $attributes Attributes.
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function parse_legacy_attributes( $attributes ) {
|
|
|
|
$mapping = array(
|
|
|
|
'per_page' => 'limit',
|
|
|
|
'operator' => 'cat_operator',
|
|
|
|
'filter' => 'terms',
|
|
|
|
);
|
|
|
|
|
|
|
|
foreach ( $mapping as $old => $new ) {
|
|
|
|
if ( isset( $attributes[ $old ] ) ) {
|
|
|
|
$attributes[ $new ] = $attributes[ $old ];
|
|
|
|
unset( $attributes[ $old ] );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $attributes;
|
|
|
|
}
|
|
|
|
|
2017-08-24 18:29:30 +00:00
|
|
|
/**
|
|
|
|
* Parse query args.
|
|
|
|
*
|
|
|
|
* @since 3.2.0
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function parse_query_args() {
|
2017-08-25 00:24:51 +00:00
|
|
|
$query_args = array(
|
2017-08-24 18:29:30 +00:00
|
|
|
'post_type' => 'product',
|
|
|
|
'post_status' => 'publish',
|
2017-08-25 01:16:42 +00:00
|
|
|
'ignore_sticky_posts' => true,
|
|
|
|
'no_found_rows' => true,
|
2017-08-24 18:29:30 +00:00
|
|
|
'orderby' => $this->attributes['orderby'],
|
2017-08-25 00:24:05 +00:00
|
|
|
'order' => strtoupper( $this->attributes['order'] ),
|
2017-08-24 18:29:30 +00:00
|
|
|
);
|
|
|
|
|
2017-08-24 22:17:18 +00:00
|
|
|
// @codingStandardsIgnoreStart
|
2017-08-28 20:27:35 +00:00
|
|
|
$query_args['posts_per_page'] = (int) $this->attributes['limit'];
|
2017-08-25 00:24:51 +00:00
|
|
|
$query_args['meta_query'] = WC()->query->get_meta_query();
|
2017-08-29 21:29:29 +00:00
|
|
|
$query_args['tax_query'] = array();
|
2017-08-24 22:17:18 +00:00
|
|
|
// @codingStandardsIgnoreEnd
|
|
|
|
|
2017-08-29 21:29:29 +00:00
|
|
|
// Visibility.
|
|
|
|
$this->set_visibility_query_args( $query_args );
|
|
|
|
|
2017-08-25 00:24:05 +00:00
|
|
|
// SKUs.
|
2017-08-25 22:39:43 +00:00
|
|
|
$this->set_skus_query_args( $query_args );
|
|
|
|
|
|
|
|
// IDs.
|
|
|
|
$this->set_ids_query_args( $query_args );
|
|
|
|
|
|
|
|
// Set specific types query args.
|
|
|
|
if ( method_exists( $this, "set_{$this->type}_query_args" ) ) {
|
|
|
|
$this->{"set_{$this->type}_query_args"}( $query_args );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Attributes.
|
|
|
|
$this->set_attributes_query_args( $query_args );
|
|
|
|
|
|
|
|
// Categories.
|
|
|
|
$this->set_categories_query_args( $query_args );
|
|
|
|
|
|
|
|
return apply_filters( 'woocommerce_shortcode_products_query', $query_args, $this->attributes, $this->type );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set skus query args.
|
|
|
|
*
|
|
|
|
* @since 3.2.0
|
|
|
|
* @param array $query_args Query args.
|
|
|
|
*/
|
|
|
|
protected function set_skus_query_args( &$query_args ) {
|
2017-08-24 18:29:30 +00:00
|
|
|
if ( ! empty( $this->attributes['skus'] ) ) {
|
2017-08-25 01:16:42 +00:00
|
|
|
$skus = array_map( 'trim', explode( ',', $this->attributes['skus'] ) );
|
2017-08-25 00:24:51 +00:00
|
|
|
$query_args['meta_query'][] = array(
|
2017-08-24 18:29:30 +00:00
|
|
|
'key' => '_sku',
|
2017-08-25 01:16:42 +00:00
|
|
|
'value' => 1 === count( $skus ) ? $skus[0] : $skus,
|
|
|
|
'compare' => 1 === count( $skus ) ? '=' : 'IN',
|
2017-08-24 18:29:30 +00:00
|
|
|
);
|
|
|
|
}
|
2017-08-25 22:39:43 +00:00
|
|
|
}
|
2017-08-24 18:29:30 +00:00
|
|
|
|
2017-08-25 22:39:43 +00:00
|
|
|
/**
|
|
|
|
* Set ids query args.
|
|
|
|
*
|
|
|
|
* @since 3.2.0
|
|
|
|
* @param array $query_args Query args.
|
|
|
|
*/
|
|
|
|
protected function set_ids_query_args( &$query_args ) {
|
2017-08-24 18:29:30 +00:00
|
|
|
if ( ! empty( $this->attributes['ids'] ) ) {
|
2017-08-25 01:16:42 +00:00
|
|
|
$ids = array_map( 'trim', explode( ',', $this->attributes['ids'] ) );
|
|
|
|
|
|
|
|
if ( 1 === count( $ids ) ) {
|
|
|
|
$query_args['p'] = $ids[0];
|
|
|
|
} else {
|
|
|
|
$query_args['post__in'] = $ids;
|
|
|
|
}
|
2017-08-24 18:29:30 +00:00
|
|
|
}
|
2017-08-25 22:39:43 +00:00
|
|
|
}
|
2017-08-24 18:29:30 +00:00
|
|
|
|
2017-08-25 22:39:43 +00:00
|
|
|
/**
|
|
|
|
* Set attributes query args.
|
|
|
|
*
|
|
|
|
* @since 3.2.0
|
|
|
|
* @param array $query_args Query args.
|
|
|
|
*/
|
|
|
|
protected function set_attributes_query_args( &$query_args ) {
|
2017-08-29 21:29:29 +00:00
|
|
|
if ( ! empty( $this->attributes['attribute'] ) || ! empty( $this->attributes['terms'] ) ) {
|
2017-08-25 22:07:10 +00:00
|
|
|
$query_args['tax_query'][] = array(
|
|
|
|
'taxonomy' => strstr( $this->attributes['attribute'], 'pa_' ) ? sanitize_title( $this->attributes['attribute'] ) : 'pa_' . sanitize_title( $this->attributes['attribute'] ),
|
2017-08-28 20:27:35 +00:00
|
|
|
'terms' => array_map( 'sanitize_title', explode( ',', $this->attributes['terms'] ) ),
|
2017-08-25 22:07:10 +00:00
|
|
|
'field' => 'slug',
|
2017-08-28 20:27:35 +00:00
|
|
|
'operator' => $this->attributes['terms_operator'],
|
2017-08-25 22:07:10 +00:00
|
|
|
);
|
|
|
|
}
|
2017-08-25 22:39:43 +00:00
|
|
|
}
|
2017-08-25 22:07:10 +00:00
|
|
|
|
2017-08-25 22:39:43 +00:00
|
|
|
/**
|
|
|
|
* Set categories query args.
|
|
|
|
*
|
|
|
|
* @since 3.2.0
|
|
|
|
* @param array $query_args Query args.
|
|
|
|
*/
|
|
|
|
protected function set_categories_query_args( &$query_args ) {
|
2017-08-25 14:46:47 +00:00
|
|
|
if ( ! empty( $this->attributes['category'] ) ) {
|
|
|
|
$ordering_args = WC()->query->get_catalog_ordering_args( $query_args['orderby'], $query_args['order'] );
|
|
|
|
$query_args['orderby'] = $ordering_args['orderby'];
|
|
|
|
$query_args['order'] = $ordering_args['order'];
|
|
|
|
|
|
|
|
if ( isset( $ordering_args['meta_key'] ) ) {
|
2017-08-25 22:25:19 +00:00
|
|
|
// @codingStandardsIgnoreStart
|
2017-08-25 14:46:47 +00:00
|
|
|
$query_args['meta_key'] = $ordering_args['meta_key'];
|
2017-08-25 22:25:19 +00:00
|
|
|
// @codingStandardsIgnoreEnd
|
2017-08-25 14:46:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$query_args['tax_query'][] = array(
|
|
|
|
'taxonomy' => 'product_cat',
|
|
|
|
'terms' => array_map( 'sanitize_title', explode( ',', $this->attributes['category'] ) ),
|
|
|
|
'field' => 'slug',
|
2017-08-28 20:27:35 +00:00
|
|
|
'operator' => $this->attributes['cat_operator'],
|
2017-08-25 14:46:47 +00:00
|
|
|
);
|
|
|
|
}
|
2017-08-25 22:39:43 +00:00
|
|
|
}
|
2017-08-25 14:46:47 +00:00
|
|
|
|
2017-08-25 22:39:43 +00:00
|
|
|
/**
|
|
|
|
* Set sale products query args.
|
|
|
|
*
|
|
|
|
* @since 3.2.0
|
|
|
|
* @param array $query_args Query args.
|
|
|
|
*/
|
|
|
|
protected function set_sale_products_query_args( &$query_args ) {
|
|
|
|
$query_args['post__in'] = array_merge( array( 0 ), wc_get_product_ids_on_sale() );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set best selling products query args.
|
|
|
|
*
|
|
|
|
* @since 3.2.0
|
|
|
|
* @param array $query_args Query args.
|
|
|
|
*/
|
|
|
|
protected function set_best_selling_products_query_args( &$query_args ) {
|
|
|
|
// @codingStandardsIgnoreStart
|
|
|
|
$query_args['meta_key'] = 'total_sales';
|
|
|
|
// @codingStandardsIgnoreEnd
|
|
|
|
$query_args['order'] = 'DESC';
|
|
|
|
$query_args['orderby'] = 'meta_value_num';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-08-29 21:29:29 +00:00
|
|
|
* Set visibility query args.
|
2017-08-25 22:39:43 +00:00
|
|
|
*
|
|
|
|
* @since 3.2.0
|
|
|
|
* @param array $query_args Query args.
|
|
|
|
*/
|
2017-08-29 21:29:29 +00:00
|
|
|
protected function set_visibility_query_args( &$query_args ) {
|
|
|
|
switch ( $this->attributes['visibility'] ) {
|
|
|
|
case 'hidden' :
|
|
|
|
$this->custom_visibility = true;
|
|
|
|
$query_args['tax_query'][] = array(
|
|
|
|
'taxonomy' => 'product_visibility',
|
|
|
|
'terms' => array( 'exclude-from-catalog', 'exclude-from-search' ),
|
|
|
|
'field' => 'name',
|
|
|
|
'operator' => 'AND',
|
|
|
|
'include_children' => false,
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
case 'catalog' :
|
|
|
|
$this->custom_visibility = true;
|
|
|
|
$query_args['tax_query'][] = array(
|
|
|
|
'taxonomy' => 'product_visibility',
|
|
|
|
'terms' => 'exclude-from-search',
|
|
|
|
'field' => 'name',
|
|
|
|
'operator' => 'IN',
|
|
|
|
'include_children' => false,
|
|
|
|
);
|
|
|
|
$query_args['tax_query'][] = array(
|
|
|
|
'taxonomy' => 'product_visibility',
|
|
|
|
'terms' => 'exclude-from-catalog',
|
|
|
|
'field' => 'name',
|
|
|
|
'operator' => 'NOT IN',
|
|
|
|
'include_children' => false,
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
case 'search' :
|
|
|
|
$this->custom_visibility = true;
|
|
|
|
$query_args['tax_query'][] = array(
|
|
|
|
'taxonomy' => 'product_visibility',
|
|
|
|
'terms' => 'exclude-from-catalog',
|
|
|
|
'field' => 'name',
|
|
|
|
'operator' => 'IN',
|
|
|
|
'include_children' => false,
|
|
|
|
);
|
|
|
|
$query_args['tax_query'][] = array(
|
|
|
|
'taxonomy' => 'product_visibility',
|
|
|
|
'terms' => 'exclude-from-search',
|
|
|
|
'field' => 'name',
|
|
|
|
'operator' => 'NOT IN',
|
|
|
|
'include_children' => false,
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
case 'featured' :
|
|
|
|
$query_args['tax_query'] = array_merge( $query_args['tax_query'], WC()->query->get_tax_query() );
|
|
|
|
$query_args['tax_query'][] = array(
|
|
|
|
'taxonomy' => 'product_visibility',
|
|
|
|
'terms' => 'featured',
|
|
|
|
'field' => 'name',
|
|
|
|
'operator' => 'IN',
|
|
|
|
'include_children' => false,
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default :
|
|
|
|
$query_args['tax_query'] = array_merge( $query_args['tax_query'], WC()->query->get_tax_query() );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set product as visible when quering for hidden products.
|
|
|
|
*
|
|
|
|
* @since 3.2.0
|
|
|
|
* @param bool $visibility Product visibility.
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function set_product_as_visible( $visibility ) {
|
|
|
|
if ( $this->custom_visibility ) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $visibility;
|
2017-08-25 22:39:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get wrapper classes.
|
|
|
|
*
|
|
|
|
* @since 3.2.0
|
|
|
|
* @param array $columns Number of columns.
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function get_wrapper_classes( $columns ) {
|
|
|
|
$classes = array( 'woocommerce' );
|
|
|
|
|
|
|
|
if ( 'product' !== $this->type ) {
|
|
|
|
$classes[] = 'columns-' . $columns;
|
|
|
|
}
|
|
|
|
|
|
|
|
$classes[] = $this->attributes['class'];
|
|
|
|
|
|
|
|
return $classes;
|
2017-08-24 18:29:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-08-29 21:29:29 +00:00
|
|
|
* Get products.
|
2017-08-24 18:29:30 +00:00
|
|
|
*
|
|
|
|
* @since 3.2.0
|
2017-08-29 21:29:29 +00:00
|
|
|
* @return array
|
2017-08-24 18:29:30 +00:00
|
|
|
*/
|
2017-08-29 21:29:29 +00:00
|
|
|
protected function get_products() {
|
|
|
|
$transient_name = 'wc_loop' . substr( md5( wp_json_encode( $this->query_args ) . $this->type ), 28 ) . WC_Cache_Helper::get_transient_version( 'product_query' );
|
|
|
|
$products = get_transient( $transient_name );
|
2017-08-24 18:29:30 +00:00
|
|
|
|
|
|
|
if ( false === $products || ! is_a( $products, 'WP_Query' ) ) {
|
2017-08-25 18:55:34 +00:00
|
|
|
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 );
|
|
|
|
}
|
|
|
|
|
2017-08-29 21:29:29 +00:00
|
|
|
// Remove ordering query arguments.
|
|
|
|
if ( ! empty( $this->attributes['category'] ) ) {
|
|
|
|
WC()->query->remove_ordering_args();
|
|
|
|
}
|
|
|
|
|
2017-08-24 18:29:30 +00:00
|
|
|
set_transient( $transient_name, $products, DAY_IN_SECONDS * 30 );
|
|
|
|
}
|
|
|
|
|
2017-08-29 21:29:29 +00:00
|
|
|
return $products;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Loop over found products.
|
|
|
|
*
|
|
|
|
* @since 3.2.0
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function product_loop() {
|
|
|
|
global $woocommerce_loop;
|
|
|
|
|
|
|
|
$columns = absint( $this->attributes['columns'] );
|
|
|
|
$classes = $this->get_wrapper_classes( $columns );
|
|
|
|
$woocommerce_loop['columns'] = $columns;
|
|
|
|
$woocommerce_loop['name'] = $this->type;
|
|
|
|
$products = $this->get_products();
|
2017-08-25 00:24:05 +00:00
|
|
|
|
2017-08-24 18:29:30 +00:00
|
|
|
ob_start();
|
|
|
|
|
|
|
|
if ( $products->have_posts() ) {
|
|
|
|
// Prime caches before grabbing objects.
|
|
|
|
update_post_caches( $products->posts, array( 'product', 'product_variation' ) );
|
|
|
|
|
2017-08-25 01:22:42 +00:00
|
|
|
do_action( "woocommerce_shortcode_before_{$this->type}_loop", $this->attributes );
|
2017-08-24 18:29:30 +00:00
|
|
|
|
|
|
|
woocommerce_product_loop_start();
|
|
|
|
|
|
|
|
while ( $products->have_posts() ) {
|
|
|
|
$products->the_post();
|
2017-08-29 21:29:29 +00:00
|
|
|
|
|
|
|
// Set custom product visibility when quering hidden products.
|
|
|
|
add_action( 'woocommerce_product_is_visible', array( $this, 'set_product_as_visible' ) );
|
|
|
|
|
|
|
|
// Render product template.
|
2017-08-24 18:29:30 +00:00
|
|
|
wc_get_template_part( 'content', 'product' );
|
2017-08-29 21:29:29 +00:00
|
|
|
|
|
|
|
// Restore product visibility.
|
|
|
|
remove_action( 'woocommerce_product_is_visible', array( $this, 'set_product_as_visible' ) );
|
2017-08-24 18:29:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
woocommerce_product_loop_end();
|
|
|
|
|
2017-08-25 01:22:42 +00:00
|
|
|
do_action( "woocommerce_shortcode_after_{$this->type}_loop", $this->attributes );
|
2017-08-24 18:29:30 +00:00
|
|
|
} else {
|
2017-08-25 01:22:42 +00:00
|
|
|
do_action( "woocommerce_shortcode_{$this->type}_loop_no_results", $this->attributes );
|
2017-08-24 18:29:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
woocommerce_reset_loop();
|
|
|
|
wp_reset_postdata();
|
|
|
|
|
2017-08-25 01:16:42 +00:00
|
|
|
return '<div class="' . esc_attr( implode( ' ', $classes ) ) . '">' . ob_get_clean() . '</div>';
|
2017-08-24 18:29:30 +00:00
|
|
|
}
|
2017-08-25 18:55:34 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
2017-08-24 18:29:30 +00:00
|
|
|
}
|