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0-beta.1 3.2.2 6.3.0-rc.1 3.2.3 6.3.0-rc.2 3.2.4 6.3.1 3.2.5 6.3.2 3.2.6 6.4.0 3.3.0 6.4.0-beta.1 3.3.1 6.4.0-rc.1 3.3.2 6.4.1 3.3.2-rc.1 6.4.2 3.3.3 6.5.0 3.3.4 6.5.0-beta.1 3.3.5 6.5.0-rc.1 3.3.6 6.5.0-rc.2 3.4.0 6.5.1 3.4.0-beta.1 6.5.2 3.4.0-rc.2 6.6.0 3.4.1 6.6.0-beta.1 3.4.2 6.6.0-rc.1 3.4.3 6.6.0-rc.2 3.4.4 6.6.1 3.4.5 6.6.2 3.4.6 6.7.0 3.4.7 6.7.0-beta.1 3.4.8 6.7.0-beta.2 3.5.0 6.7.0-rc.1 3.5.0-beta.1 6.7.1 3.5.0-rc.1 6.8.0 3.5.0-rc.2 6.8.0-beta.1 3.5.1 6.8.0-beta.2 3.5.10 6.8.0-rc.1 3.5.2 6.8.1 3.5.3 6.8.2 3.5.4 6.8.3 3.5.5 6.9.0 3.5.6 6.9.0-beta.1 3.5.7 6.9.0-beta.2 3.5.8 6.9.0-rc.1 3.5.9 6.9.1 3.6.0 6.9.2 3.6.0-beta.1 6.9.3 3.6.0-rc.1 6.9.4 3.6.0-rc.2 6.9.5 3.6.0-rc.3 7.0.0 3.6.1 7.0.0-beta.1 3.6.2 7.0.0-beta.2 3.6.3 7.0.0-beta.3 3.6.4 7.0.0-rc.1 3.6.5 7.0.0-rc.2 3.6.6 7.0.1 3.6.7 7.0.2 3.7.0 7.1.0 3.7.0-beta.1 7.1.0-beta.1 3.7.0-rc.1 7.1.0-beta.2 3.7.0-rc.2 7.1.0-rc.1 3.7.1 7.1.0-rc.2 3.7.2 7.1.1
woocommerce / includes / shortcodes / class-wc-shortcode-products.php
woocommerce / includes / shortcodes Last commit date
class-wc-shortcode-cart.php 5 years ago class-wc-shortcode-checkout.php 3 years ago class-wc-shortcode-my-account.php 5 years ago class-wc-shortcode-order-tracking.php 3 years ago class-wc-shortcode-products.php 5 years ago
class-wc-shortcode-products.php
704 lines
1 <?php
2 /**
3 * Products shortcode
4 *
5 * @package WooCommerce\Shortcodes
6 * @version 3.2.4
7 */
8
9 if ( ! defined( 'ABSPATH' ) ) {
10 exit;
11 }
12
13 /**
14 * Products shortcode class.
15 */
16 class WC_Shortcode_Products {
17
18 /**
19 * Shortcode type.
20 *
21 * @since 3.2.0
22 * @var string
23 */
24 protected $type = 'products';
25
26 /**
27 * Attributes.
28 *
29 * @since 3.2.0
30 * @var array
31 */
32 protected $attributes = array();
33
34 /**
35 * Query args.
36 *
37 * @since 3.2.0
38 * @var array
39 */
40 protected $query_args = array();
41
42 /**
43 * Set custom visibility.
44 *
45 * @since 3.2.0
46 * @var bool
47 */
48 protected $custom_visibility = false;
49
50 /**
51 * Initialize shortcode.
52 *
53 * @since 3.2.0
54 * @param array $attributes Shortcode attributes.
55 * @param string $type Shortcode type.
56 */
57 public function __construct( $attributes = array(), $type = 'products' ) {
58 $this->type = $type;
59 $this->attributes = $this->parse_attributes( $attributes );
60 $this->query_args = $this->parse_query_args();
61 }
62
63 /**
64 * Get shortcode attributes.
65 *
66 * @since 3.2.0
67 * @return array
68 */
69 public function get_attributes() {
70 return $this->attributes;
71 }
72
73 /**
74 * Get query args.
75 *
76 * @since 3.2.0
77 * @return array
78 */
79 public function get_query_args() {
80 return $this->query_args;
81 }
82
83 /**
84 * Get shortcode type.
85 *
86 * @since 3.2.0
87 * @return string
88 */
89 public function get_type() {
90 return $this->type;
91 }
92
93 /**
94 * Get shortcode content.
95 *
96 * @since 3.2.0
97 * @return string
98 */
99 public function get_content() {
100 return $this->product_loop();
101 }
102
103 /**
104 * Parse attributes.
105 *
106 * @since 3.2.0
107 * @param array $attributes Shortcode attributes.
108 * @return array
109 */
110 protected function parse_attributes( $attributes ) {
111 $attributes = $this->parse_legacy_attributes( $attributes );
112
113 $attributes = shortcode_atts(
114 array(
115 'limit' => '-1', // Results limit.
116 'columns' => '', // Number of columns.
117 'rows' => '', // Number of rows. If defined, limit will be ignored.
118 'orderby' => '', // menu_order, title, date, rand, price, popularity, rating, or id.
119 'order' => '', // ASC or DESC.
120 'ids' => '', // Comma separated IDs.
121 'skus' => '', // Comma separated SKUs.
122 'category' => '', // Comma separated category slugs or ids.
123 'cat_operator' => 'IN', // Operator to compare categories. Possible values are 'IN', 'NOT IN', 'AND'.
124 'attribute' => '', // Single attribute slug.
125 'terms' => '', // Comma separated term slugs or ids.
126 'terms_operator' => 'IN', // Operator to compare terms. Possible values are 'IN', 'NOT IN', 'AND'.
127 'tag' => '', // Comma separated tag slugs.
128 'tag_operator' => 'IN', // Operator to compare tags. Possible values are 'IN', 'NOT IN', 'AND'.
129 'visibility' => 'visible', // Product visibility setting. Possible values are 'visible', 'catalog', 'search', 'hidden'.
130 'class' => '', // HTML class.
131 'page' => 1, // Page for pagination.
132 'paginate' => false, // Should results be paginated.
133 'cache' => true, // Should shortcode output be cached.
134 ),
135 $attributes,
136 $this->type
137 );
138
139 if ( ! absint( $attributes['columns'] ) ) {
140 $attributes['columns'] = wc_get_default_products_per_row();
141 }
142
143 return $attributes;
144 }
145
146 /**
147 * Parse legacy attributes.
148 *
149 * @since 3.2.0
150 * @param array $attributes Attributes.
151 * @return array
152 */
153 protected function parse_legacy_attributes( $attributes ) {
154 $mapping = array(
155 'per_page' => 'limit',
156 'operator' => 'cat_operator',
157 'filter' => 'terms',
158 );
159
160 foreach ( $mapping as $old => $new ) {
161 if ( isset( $attributes[ $old ] ) ) {
162 $attributes[ $new ] = $attributes[ $old ];
163 unset( $attributes[ $old ] );
164 }
165 }
166
167 return $attributes;
168 }
169
170 /**
171 * Parse query args.
172 *
173 * @since 3.2.0
174 * @return array
175 */
176 protected function parse_query_args() {
177 $query_args = array(
178 'post_type' => 'product',
179 'post_status' => 'publish',
180 'ignore_sticky_posts' => true,
181 'no_found_rows' => false === wc_string_to_bool( $this->attributes['paginate'] ),
182 'orderby' => empty( $_GET['orderby'] ) ? $this->attributes['orderby'] : wc_clean( wp_unslash( $_GET['orderby'] ) ), // phpcs:ignore WordPress.Security.NonceVerification.Recommended
183 );
184
185 $orderby_value = explode( '-', $query_args['orderby'] );
186 $orderby = esc_attr( $orderby_value[0] );
187 $order = ! empty( $orderby_value[1] ) ? $orderby_value[1] : strtoupper( $this->attributes['order'] );
188 $query_args['orderby'] = $orderby;
189 $query_args['order'] = $order;
190
191 if ( wc_string_to_bool( $this->attributes['paginate'] ) ) {
192 $this->attributes['page'] = absint( empty( $_GET['product-page'] ) ? 1 : $_GET['product-page'] ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
193 }
194
195 if ( ! empty( $this->attributes['rows'] ) ) {
196 $this->attributes['limit'] = $this->attributes['columns'] * $this->attributes['rows'];
197 }
198
199 $ordering_args = WC()->query->get_catalog_ordering_args( $query_args['orderby'], $query_args['order'] );
200 $query_args['orderby'] = $ordering_args['orderby'];
201 $query_args['order'] = $ordering_args['order'];
202 if ( $ordering_args['meta_key'] ) {
203 $query_args['meta_key'] = $ordering_args['meta_key']; // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key
204 }
205 $query_args['posts_per_page'] = intval( $this->attributes['limit'] );
206 if ( 1 < $this->attributes['page'] ) {
207 $query_args['paged'] = absint( $this->attributes['page'] );
208 }
209 $query_args['meta_query'] = WC()->query->get_meta_query(); // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query
210 $query_args['tax_query'] = array(); // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_tax_query
211
212 // Visibility.
213 $this->set_visibility_query_args( $query_args );
214
215 // SKUs.
216 $this->set_skus_query_args( $query_args );
217
218 // IDs.
219 $this->set_ids_query_args( $query_args );
220
221 // Set specific types query args.
222 if ( method_exists( $this, "set_{$this->type}_query_args" ) ) {
223 $this->{"set_{$this->type}_query_args"}( $query_args );
224 }
225
226 // Attributes.
227 $this->set_attributes_query_args( $query_args );
228
229 // Categories.
230 $this->set_categories_query_args( $query_args );
231
232 // Tags.
233 $this->set_tags_query_args( $query_args );
234
235 $query_args = apply_filters( 'woocommerce_shortcode_products_query', $query_args, $this->attributes, $this->type );
236
237 // Always query only IDs.
238 $query_args['fields'] = 'ids';
239
240 return $query_args;
241 }
242
243 /**
244 * Set skus query args.
245 *
246 * @since 3.2.0
247 * @param array $query_args Query args.
248 */
249 protected function set_skus_query_args( &$query_args ) {
250 if ( ! empty( $this->attributes['skus'] ) ) {
251 $skus = array_map( 'trim', explode( ',', $this->attributes['skus'] ) );
252 $query_args['meta_query'][] = array(
253 'key' => '_sku',
254 'value' => 1 === count( $skus ) ? $skus[0] : $skus,
255 'compare' => 1 === count( $skus ) ? '=' : 'IN',
256 );
257 }
258 }
259
260 /**
261 * Set ids query args.
262 *
263 * @since 3.2.0
264 * @param array $query_args Query args.
265 */
266 protected function set_ids_query_args( &$query_args ) {
267 if ( ! empty( $this->attributes['ids'] ) ) {
268 $ids = array_map( 'trim', explode( ',', $this->attributes['ids'] ) );
269
270 if ( 1 === count( $ids ) ) {
271 $query_args['p'] = $ids[0];
272 } else {
273 $query_args['post__in'] = $ids;
274 }
275 }
276 }
277
278 /**
279 * Set attributes query args.
280 *
281 * @since 3.2.0
282 * @param array $query_args Query args.
283 */
284 protected function set_attributes_query_args( &$query_args ) {
285 if ( ! empty( $this->attributes['attribute'] ) || ! empty( $this->attributes['terms'] ) ) {
286 $taxonomy = strstr( $this->attributes['attribute'], 'pa_' ) ? sanitize_title( $this->attributes['attribute'] ) : 'pa_' . sanitize_title( $this->attributes['attribute'] );
287 $terms = $this->attributes['terms'] ? array_map( 'sanitize_title', explode( ',', $this->attributes['terms'] ) ) : array();
288 $field = 'slug';
289
290 if ( $terms && is_numeric( $terms[0] ) ) {
291 $field = 'term_id';
292 $terms = array_map( 'absint', $terms );
293 // Check numeric slugs.
294 foreach ( $terms as $term ) {
295 $the_term = get_term_by( 'slug', $term, $taxonomy );
296 if ( false !== $the_term ) {
297 $terms[] = $the_term->term_id;
298 }
299 }
300 }
301
302 // If no terms were specified get all products that are in the attribute taxonomy.
303 if ( ! $terms ) {
304 $terms = get_terms(
305 array(
306 'taxonomy' => $taxonomy,
307 'fields' => 'ids',
308 )
309 );
310 $field = 'term_id';
311 }
312
313 // We always need to search based on the slug as well, this is to accommodate numeric slugs.
314 $query_args['tax_query'][] = array(
315 'taxonomy' => $taxonomy,
316 'terms' => $terms,
317 'field' => $field,
318 'operator' => $this->attributes['terms_operator'],
319 );
320 }
321 }
322
323 /**
324 * Set categories query args.
325 *
326 * @since 3.2.0
327 * @param array $query_args Query args.
328 */
329 protected function set_categories_query_args( &$query_args ) {
330 if ( ! empty( $this->attributes['category'] ) ) {
331 $categories = array_map( 'sanitize_title', explode( ',', $this->attributes['category'] ) );
332 $field = 'slug';
333
334 if ( is_numeric( $categories[0] ) ) {
335 $field = 'term_id';
336 $categories = array_map( 'absint', $categories );
337 // Check numeric slugs.
338 foreach ( $categories as $cat ) {
339 $the_cat = get_term_by( 'slug', $cat, 'product_cat' );
340 if ( false !== $the_cat ) {
341 $categories[] = $the_cat->term_id;
342 }
343 }
344 }
345
346 $query_args['tax_query'][] = array(
347 'taxonomy' => 'product_cat',
348 'terms' => $categories,
349 'field' => $field,
350 'operator' => $this->attributes['cat_operator'],
351
352 /*
353 * When cat_operator is AND, the children categories should be excluded,
354 * as only products belonging to all the children categories would be selected.
355 */
356 'include_children' => 'AND' === $this->attributes['cat_operator'] ? false : true,
357 );
358 }
359 }
360
361 /**
362 * Set tags query args.
363 *
364 * @since 3.3.0
365 * @param array $query_args Query args.
366 */
367 protected function set_tags_query_args( &$query_args ) {
368 if ( ! empty( $this->attributes['tag'] ) ) {
369 $query_args['tax_query'][] = array(
370 'taxonomy' => 'product_tag',
371 'terms' => array_map( 'sanitize_title', explode( ',', $this->attributes['tag'] ) ),
372 'field' => 'slug',
373 'operator' => $this->attributes['tag_operator'],
374 );
375 }
376 }
377
378 /**
379 * Set sale products query args.
380 *
381 * @since 3.2.0
382 * @param array $query_args Query args.
383 */
384 protected function set_sale_products_query_args( &$query_args ) {
385 $query_args['post__in'] = array_merge( array( 0 ), wc_get_product_ids_on_sale() );
386 }
387
388 /**
389 * Set best selling products query args.
390 *
391 * @since 3.2.0
392 * @param array $query_args Query args.
393 */
394 protected function set_best_selling_products_query_args( &$query_args ) {
395 $query_args['meta_key'] = 'total_sales'; // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key
396 $query_args['order'] = 'DESC';
397 $query_args['orderby'] = 'meta_value_num';
398 }
399
400 /**
401 * Set top rated products query args.
402 *
403 * @since 3.6.5
404 * @param array $query_args Query args.
405 */
406 protected function set_top_rated_products_query_args( &$query_args ) {
407 $query_args['meta_key'] = '_wc_average_rating'; // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key
408 $query_args['order'] = 'DESC';
409 $query_args['orderby'] = 'meta_value_num';
410 }
411
412 /**
413 * Set visibility as hidden.
414 *
415 * @since 3.2.0
416 * @param array $query_args Query args.
417 */
418 protected function set_visibility_hidden_query_args( &$query_args ) {
419 $this->custom_visibility = true;
420 $query_args['tax_query'][] = array(
421 'taxonomy' => 'product_visibility',
422 'terms' => array( 'exclude-from-catalog', 'exclude-from-search' ),
423 'field' => 'name',
424 'operator' => 'AND',
425 'include_children' => false,
426 );
427 }
428
429 /**
430 * Set visibility as catalog.
431 *
432 * @since 3.2.0
433 * @param array $query_args Query args.
434 */
435 protected function set_visibility_catalog_query_args( &$query_args ) {
436 $this->custom_visibility = true;
437 $query_args['tax_query'][] = array(
438 'taxonomy' => 'product_visibility',
439 'terms' => 'exclude-from-search',
440 'field' => 'name',
441 'operator' => 'IN',
442 'include_children' => false,
443 );
444 $query_args['tax_query'][] = array(
445 'taxonomy' => 'product_visibility',
446 'terms' => 'exclude-from-catalog',
447 'field' => 'name',
448 'operator' => 'NOT IN',
449 'include_children' => false,
450 );
451 }
452
453 /**
454 * Set visibility as search.
455 *
456 * @since 3.2.0
457 * @param array $query_args Query args.
458 */
459 protected function set_visibility_search_query_args( &$query_args ) {
460 $this->custom_visibility = true;
461 $query_args['tax_query'][] = array(
462 'taxonomy' => 'product_visibility',
463 'terms' => 'exclude-from-catalog',
464 'field' => 'name',
465 'operator' => 'IN',
466 'include_children' => false,
467 );
468 $query_args['tax_query'][] = array(
469 'taxonomy' => 'product_visibility',
470 'terms' => 'exclude-from-search',
471 'field' => 'name',
472 'operator' => 'NOT IN',
473 'include_children' => false,
474 );
475 }
476
477 /**
478 * Set visibility as featured.
479 *
480 * @since 3.2.0
481 * @param array $query_args Query args.
482 */
483 protected function set_visibility_featured_query_args( &$query_args ) {
484 $query_args['tax_query'] = array_merge( $query_args['tax_query'], WC()->query->get_tax_query() ); // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_tax_query
485
486 $query_args['tax_query'][] = array(
487 'taxonomy' => 'product_visibility',
488 'terms' => 'featured',
489 'field' => 'name',
490 'operator' => 'IN',
491 'include_children' => false,
492 );
493 }
494
495 /**
496 * Set visibility query args.
497 *
498 * @since 3.2.0
499 * @param array $query_args Query args.
500 */
501 protected function set_visibility_query_args( &$query_args ) {
502 if ( method_exists( $this, 'set_visibility_' . $this->attributes['visibility'] . '_query_args' ) ) {
503 $this->{'set_visibility_' . $this->attributes['visibility'] . '_query_args'}( $query_args );
504 } else {
505 $query_args['tax_query'] = array_merge( $query_args['tax_query'], WC()->query->get_tax_query() ); // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_tax_query
506 }
507 }
508
509 /**
510 * Set product as visible when querying for hidden products.
511 *
512 * @since 3.2.0
513 * @param bool $visibility Product visibility.
514 * @return bool
515 */
516 public function set_product_as_visible( $visibility ) {
517 return $this->custom_visibility ? true : $visibility;
518 }
519
520 /**
521 * Get wrapper classes.
522 *
523 * @since 3.2.0
524 * @param int $columns Number of columns.
525 * @return array
526 */
527 protected function get_wrapper_classes( $columns ) {
528 $classes = array( 'woocommerce' );
529
530 if ( 'product' !== $this->type ) {
531 $classes[] = 'columns-' . $columns;
532 }
533
534 $classes[] = $this->attributes['class'];
535
536 return $classes;
537 }
538
539 /**
540 * Generate and return the transient name for this shortcode based on the query args.
541 *
542 * @since 3.3.0
543 * @return string
544 */
545 protected function get_transient_name() {
546 $transient_name = 'wc_product_loop_' . md5( wp_json_encode( $this->query_args ) . $this->type );
547
548 if ( 'rand' === $this->query_args['orderby'] ) {
549 // When using rand, we'll cache a number of random queries and pull those to avoid querying rand on each page load.
550 $rand_index = wp_rand( 0, max( 1, absint( apply_filters( 'woocommerce_product_query_max_rand_cache_count', 5 ) ) ) );
551 $transient_name .= $rand_index;
552 }
553
554 return $transient_name;
555 }
556
557 /**
558 * Run the query and return an array of data, including queried ids and pagination information.
559 *
560 * @since 3.3.0
561 * @return object Object with the following props; ids, per_page, found_posts, max_num_pages, current_page
562 */
563 protected function get_query_results() {
564 $transient_name = $this->get_transient_name();
565 $transient_version = WC_Cache_Helper::get_transient_version( 'product_query' );
566 $cache = wc_string_to_bool( $this->attributes['cache'] ) === true;
567 $transient_value = $cache ? get_transient( $transient_name ) : false;
568
569 if ( isset( $transient_value['value'], $transient_value['version'] ) && $transient_value['version'] === $transient_version ) {
570 $results = $transient_value['value'];
571 } else {
572 $query = new WP_Query( $this->query_args );
573
574 $paginated = ! $query->get( 'no_found_rows' );
575
576 $results = (object) array(
577 'ids' => wp_parse_id_list( $query->posts ),
578 'total' => $paginated ? (int) $query->found_posts : count( $query->posts ),
579 'total_pages' => $paginated ? (int) $query->max_num_pages : 1,
580 'per_page' => (int) $query->get( 'posts_per_page' ),
581 'current_page' => $paginated ? (int) max( 1, $query->get( 'paged', 1 ) ) : 1,
582 );
583
584 if ( $cache ) {
585 $transient_value = array(
586 'version' => $transient_version,
587 'value' => $results,
588 );
589 set_transient( $transient_name, $transient_value, DAY_IN_SECONDS * 30 );
590 }
591 }
592
593 // Remove ordering query arguments which may have been added by get_catalog_ordering_args.
594 WC()->query->remove_ordering_args();
595
596 /**
597 * Filter shortcode products query results.
598 *
599 * @since 4.0.0
600 * @param stdClass $results Query results.
601 * @param WC_Shortcode_Products $this WC_Shortcode_Products instance.
602 */
603 return apply_filters( 'woocommerce_shortcode_products_query_results', $results, $this );
604 }
605
606 /**
607 * Loop over found products.
608 *
609 * @since 3.2.0
610 * @return string
611 */
612 protected function product_loop() {
613 $columns = absint( $this->attributes['columns'] );
614 $classes = $this->get_wrapper_classes( $columns );
615 $products = $this->get_query_results();
616
617 ob_start();
618
619 if ( $products && $products->ids ) {
620 // Prime caches to reduce future queries.
621 if ( is_callable( '_prime_post_caches' ) ) {
622 _prime_post_caches( $products->ids );
623 }
624
625 // Setup the loop.
626 wc_setup_loop(
627 array(
628 'columns' => $columns,
629 'name' => $this->type,
630 'is_shortcode' => true,
631 'is_search' => false,
632 'is_paginated' => wc_string_to_bool( $this->attributes['paginate'] ),
633 'total' => $products->total,
634 'total_pages' => $products->total_pages,
635 'per_page' => $products->per_page,
636 'current_page' => $products->current_page,
637 )
638 );
639
640 $original_post = $GLOBALS['post'];
641
642 do_action( "woocommerce_shortcode_before_{$this->type}_loop", $this->attributes );
643
644 // Fire standard shop loop hooks when paginating results so we can show result counts and so on.
645 if ( wc_string_to_bool( $this->attributes['paginate'] ) ) {
646 do_action( 'woocommerce_before_shop_loop' );
647 }
648
649 woocommerce_product_loop_start();
650
651 if ( wc_get_loop_prop( 'total' ) ) {
652 foreach ( $products->ids as $product_id ) {
653 $GLOBALS['post'] = get_post( $product_id ); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
654 setup_postdata( $GLOBALS['post'] );
655
656 // Set custom product visibility when quering hidden products.
657 add_action( 'woocommerce_product_is_visible', array( $this, 'set_product_as_visible' ) );
658
659 // Render product template.
660 wc_get_template_part( 'content', 'product' );
661
662 // Restore product visibility.
663 remove_action( 'woocommerce_product_is_visible', array( $this, 'set_product_as_visible' ) );
664 }
665 }
666
667 $GLOBALS['post'] = $original_post; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
668 woocommerce_product_loop_end();
669
670 // Fire standard shop loop hooks when paginating results so we can show result counts and so on.
671 if ( wc_string_to_bool( $this->attributes['paginate'] ) ) {
672 do_action( 'woocommerce_after_shop_loop' );
673 }
674
675 do_action( "woocommerce_shortcode_after_{$this->type}_loop", $this->attributes );
676
677 wp_reset_postdata();
678 wc_reset_loop();
679 } else {
680 do_action( "woocommerce_shortcode_{$this->type}_loop_no_results", $this->attributes );
681 }
682
683 return '<div class="' . esc_attr( implode( ' ', $classes ) ) . '">' . ob_get_clean() . '</div>';
684 }
685
686 /**
687 * Order by rating.
688 *
689 * @since 3.2.0
690 * @param array $args Query args.
691 * @return array
692 */
693 public static function order_by_rating_post_clauses( $args ) {
694 global $wpdb;
695
696 $args['where'] .= " AND $wpdb->commentmeta.meta_key = 'rating' ";
697 $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)";
698 $args['orderby'] = "$wpdb->commentmeta.meta_value DESC";
699 $args['groupby'] = "$wpdb->posts.ID";
700
701 return $args;
702 }
703 }
704