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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 / class-wc-shortcodes.php
woocommerce / includes Last commit date
abstracts 8 years ago admin 8 years ago api 8 years ago cli 8 years ago customizer 8 years ago data-stores 8 years ago emails 8 years ago export 8 years ago gateways 8 years ago import 8 years ago interfaces 8 years ago legacy 8 years ago libraries 8 years ago log-handlers 8 years ago payment-tokens 8 years ago shipping 8 years ago shortcodes 8 years ago theme-support 8 years ago walkers 8 years ago widgets 8 years ago class-wc-ajax.php 8 years ago class-wc-api.php 8 years ago class-wc-auth.php 8 years ago class-wc-autoloader.php 8 years ago class-wc-background-emailer.php 8 years ago class-wc-background-updater.php 8 years ago class-wc-breadcrumb.php 8 years ago class-wc-cache-helper.php 8 years ago class-wc-cart-fees.php 8 years ago class-wc-cart-session.php 8 years ago class-wc-cart-totals.php 8 years ago class-wc-cart.php 8 years ago class-wc-checkout.php 8 years ago class-wc-cli.php 8 years ago class-wc-comments.php 8 years ago class-wc-countries.php 8 years ago class-wc-coupon.php 8 years ago class-wc-customer-download-log.php 8 years ago class-wc-customer-download.php 8 years ago class-wc-customer.php 8 years ago class-wc-data-exception.php 8 years ago class-wc-data-store.php 8 years ago class-wc-datetime.php 8 years ago class-wc-deprecated-action-hooks.php 8 years ago class-wc-deprecated-filter-hooks.php 8 years ago class-wc-discounts.php 8 years ago class-wc-download-handler.php 8 years ago class-wc-emails.php 8 years ago class-wc-embed.php 8 years ago class-wc-form-handler.php 8 years ago class-wc-frontend-scripts.php 8 years ago class-wc-geo-ip.php 8 years ago class-wc-geolite-integration.php 8 years ago class-wc-geolocation.php 8 years ago class-wc-https.php 8 years ago class-wc-install.php 8 years ago class-wc-integrations.php 8 years ago class-wc-log-levels.php 8 years ago class-wc-logger.php 8 years ago class-wc-meta-data.php 8 years ago class-wc-order-factory.php 8 years ago class-wc-order-item-coupon.php 8 years ago class-wc-order-item-fee.php 8 years ago class-wc-order-item-meta.php 8 years ago class-wc-order-item-product.php 8 years ago class-wc-order-item-shipping.php 8 years ago class-wc-order-item-tax.php 8 years ago class-wc-order-item.php 8 years ago class-wc-order-query.php 8 years ago class-wc-order-refund.php 8 years ago class-wc-order.php 8 years ago class-wc-payment-gateways.php 8 years ago class-wc-payment-tokens.php 8 years ago class-wc-post-data.php 8 years ago class-wc-post-types.php 8 years ago class-wc-privacy-background-process.php 8 years ago class-wc-privacy-erasers.php 8 years ago class-wc-privacy-exporters.php 8 years ago class-wc-privacy.php 8 years ago class-wc-product-attribute.php 8 years ago class-wc-product-download.php 8 years ago class-wc-product-external.php 8 years ago class-wc-product-factory.php 8 years ago class-wc-product-grouped.php 8 years ago class-wc-product-query.php 8 years ago class-wc-product-simple.php 8 years ago class-wc-product-variable.php 8 years ago class-wc-product-variation.php 8 years ago class-wc-query.php 8 years ago class-wc-regenerate-images-request.php 8 years ago class-wc-regenerate-images.php 8 years ago class-wc-register-wp-admin-settings.php 8 years ago class-wc-session-handler.php 8 years ago class-wc-shipping-rate.php 8 years ago class-wc-shipping-zone.php 8 years ago class-wc-shipping-zones.php 8 years ago class-wc-shipping.php 8 years ago class-wc-shortcodes.php 8 years ago class-wc-structured-data.php 8 years ago class-wc-tax.php 8 years ago class-wc-template-loader.php 8 years ago class-wc-tracker.php 8 years ago class-wc-validation.php 8 years ago class-wc-webhook.php 8 years ago class-woocommerce.php 8 years ago wc-account-functions.php 8 years ago wc-attribute-functions.php 8 years ago wc-cart-functions.php 8 years ago wc-conditional-functions.php 8 years ago wc-core-functions.php 8 years ago wc-coupon-functions.php 8 years ago wc-deprecated-functions.php 8 years ago wc-formatting-functions.php 8 years ago wc-notice-functions.php 8 years ago wc-order-functions.php 8 years ago wc-order-item-functions.php 8 years ago wc-page-functions.php 8 years ago wc-product-functions.php 8 years ago wc-rest-functions.php 8 years ago wc-stock-functions.php 8 years ago wc-template-functions.php 8 years ago wc-template-hooks.php 8 years ago wc-term-functions.php 8 years ago wc-update-functions.php 8 years ago wc-user-functions.php 8 years ago wc-webhook-functions.php 8 years ago wc-widget-functions.php 8 years ago
class-wc-shortcodes.php
659 lines
1 <?php
2 /**
3 * Shortcodes
4 *
5 * @package WooCommerce/Classes
6 * @version 3.2.0
7 */
8
9 defined( 'ABSPATH' ) || exit;
10
11 /**
12 * WooCommerce Shortcodes class.
13 */
14 class WC_Shortcodes {
15
16 /**
17 * Init shortcodes.
18 */
19 public static function init() {
20 $shortcodes = array(
21 'product' => __CLASS__ . '::product',
22 'product_page' => __CLASS__ . '::product_page',
23 'product_category' => __CLASS__ . '::product_category',
24 'product_categories' => __CLASS__ . '::product_categories',
25 'add_to_cart' => __CLASS__ . '::product_add_to_cart',
26 'add_to_cart_url' => __CLASS__ . '::product_add_to_cart_url',
27 'products' => __CLASS__ . '::products',
28 'recent_products' => __CLASS__ . '::recent_products',
29 'sale_products' => __CLASS__ . '::sale_products',
30 'best_selling_products' => __CLASS__ . '::best_selling_products',
31 'top_rated_products' => __CLASS__ . '::top_rated_products',
32 'featured_products' => __CLASS__ . '::featured_products',
33 'product_attribute' => __CLASS__ . '::product_attribute',
34 'related_products' => __CLASS__ . '::related_products',
35 'shop_messages' => __CLASS__ . '::shop_messages',
36 'woocommerce_order_tracking' => __CLASS__ . '::order_tracking',
37 'woocommerce_cart' => __CLASS__ . '::cart',
38 'woocommerce_checkout' => __CLASS__ . '::checkout',
39 'woocommerce_my_account' => __CLASS__ . '::my_account',
40 );
41
42 foreach ( $shortcodes as $shortcode => $function ) {
43 add_shortcode( apply_filters( "{$shortcode}_shortcode_tag", $shortcode ), $function );
44 }
45
46 // Alias for pre 2.1 compatibility.
47 add_shortcode( 'woocommerce_messages', __CLASS__ . '::shop_messages' );
48 }
49
50 /**
51 * Shortcode Wrapper.
52 *
53 * @param string[] $function Callback function.
54 * @param array $atts Attributes. Default to empty array.
55 * @param array $wrapper Customer wrapper data.
56 *
57 * @return string
58 */
59 public static function shortcode_wrapper(
60 $function,
61 $atts = array(),
62 $wrapper = array(
63 'class' => 'woocommerce',
64 'before' => null,
65 'after' => null,
66 )
67 ) {
68 ob_start();
69
70 // @codingStandardsIgnoreStart
71 echo empty( $wrapper['before'] ) ? '<div class="' . esc_attr( $wrapper['class'] ) . '">' : $wrapper['before'];
72 call_user_func( $function, $atts );
73 echo empty( $wrapper['after'] ) ? '</div>' : $wrapper['after'];
74 // @codingStandardsIgnoreEnd
75
76 return ob_get_clean();
77 }
78
79 /**
80 * Cart page shortcode.
81 *
82 * @return string
83 */
84 public static function cart() {
85 return is_null( WC()->cart ) ? '' : self::shortcode_wrapper( array( 'WC_Shortcode_Cart', 'output' ) );
86 }
87
88 /**
89 * Checkout page shortcode.
90 *
91 * @param array $atts Attributes.
92 * @return string
93 */
94 public static function checkout( $atts ) {
95 return self::shortcode_wrapper( array( 'WC_Shortcode_Checkout', 'output' ), $atts );
96 }
97
98 /**
99 * Order tracking page shortcode.
100 *
101 * @param array $atts Attributes.
102 * @return string
103 */
104 public static function order_tracking( $atts ) {
105 return self::shortcode_wrapper( array( 'WC_Shortcode_Order_Tracking', 'output' ), $atts );
106 }
107
108 /**
109 * My account page shortcode.
110 *
111 * @param array $atts Attributes.
112 * @return string
113 */
114 public static function my_account( $atts ) {
115 return self::shortcode_wrapper( array( 'WC_Shortcode_My_Account', 'output' ), $atts );
116 }
117
118 /**
119 * List products in a category shortcode.
120 *
121 * @param array $atts Attributes.
122 * @return string
123 */
124 public static function product_category( $atts ) {
125 if ( empty( $atts['category'] ) ) {
126 return '';
127 }
128
129 $atts = array_merge( array(
130 'limit' => '12',
131 'columns' => '4',
132 'orderby' => 'menu_order title',
133 'order' => 'ASC',
134 'category' => '',
135 'cat_operator' => 'IN',
136 ), (array) $atts );
137
138 $shortcode = new WC_Shortcode_Products( $atts, 'product_category' );
139
140 return $shortcode->get_content();
141 }
142
143 /**
144 * List all (or limited) product categories.
145 *
146 * @param array $atts Attributes.
147 * @return string
148 */
149 public static function product_categories( $atts ) {
150 if ( isset( $atts['number'] ) ) {
151 $atts['limit'] = $atts['number'];
152 }
153
154 $atts = shortcode_atts( array(
155 'limit' => '-1',
156 'orderby' => 'name',
157 'order' => 'ASC',
158 'columns' => '4',
159 'hide_empty' => 1,
160 'parent' => '',
161 'ids' => '',
162 ), $atts, 'product_categories' );
163
164 $ids = array_filter( array_map( 'trim', explode( ',', $atts['ids'] ) ) );
165 $hide_empty = ( true === $atts['hide_empty'] || 'true' === $atts['hide_empty'] || 1 === $atts['hide_empty'] || '1' === $atts['hide_empty'] ) ? 1 : 0;
166
167 // Get terms and workaround WP bug with parents/pad counts.
168 $args = array(
169 'orderby' => $atts['orderby'],
170 'order' => $atts['order'],
171 'hide_empty' => $hide_empty,
172 'include' => $ids,
173 'pad_counts' => true,
174 'child_of' => $atts['parent'],
175 );
176
177 $product_categories = get_terms( 'product_cat', $args );
178
179 if ( '' !== $atts['parent'] ) {
180 $product_categories = wp_list_filter( $product_categories, array(
181 'parent' => $atts['parent'],
182 ) );
183 }
184
185 if ( $hide_empty ) {
186 foreach ( $product_categories as $key => $category ) {
187 if ( 0 === $category->count ) {
188 unset( $product_categories[ $key ] );
189 }
190 }
191 }
192
193 $atts['limit'] = '-1' === $atts['limit'] ? null : intval( $atts['limit'] );
194 if ( $atts['limit'] ) {
195 $product_categories = array_slice( $product_categories, 0, $atts['limit'] );
196 }
197
198 $columns = absint( $atts['columns'] );
199
200 wc_set_loop_prop( 'columns', $columns );
201 wc_set_loop_prop( 'is_shortcode', true );
202
203 ob_start();
204
205 if ( $product_categories ) {
206 woocommerce_product_loop_start();
207
208 foreach ( $product_categories as $category ) {
209 wc_get_template( 'content-product_cat.php', array(
210 'category' => $category,
211 ) );
212 }
213
214 woocommerce_product_loop_end();
215 }
216
217 woocommerce_reset_loop();
218
219 return '<div class="woocommerce columns-' . $columns . '">' . ob_get_clean() . '</div>';
220 }
221
222 /**
223 * Recent Products shortcode.
224 *
225 * @param array $atts Attributes.
226 * @return string
227 */
228 public static function recent_products( $atts ) {
229 $atts = array_merge( array(
230 'limit' => '12',
231 'columns' => '4',
232 'orderby' => 'date',
233 'order' => 'DESC',
234 'category' => '',
235 'cat_operator' => 'IN',
236 ), (array) $atts );
237
238 $shortcode = new WC_Shortcode_Products( $atts, 'recent_products' );
239
240 return $shortcode->get_content();
241 }
242
243 /**
244 * List multiple products shortcode.
245 *
246 * @param array $atts Attributes.
247 * @return string
248 */
249 public static function products( $atts ) {
250 $atts = (array) $atts;
251 $type = 'products';
252
253 // Allow list product based on specific cases.
254 if ( isset( $atts['on_sale'] ) && wc_string_to_bool( $atts['on_sale'] ) ) {
255 $type = 'sale_products';
256 } elseif ( isset( $atts['best_selling'] ) && wc_string_to_bool( $atts['best_selling'] ) ) {
257 $type = 'best_selling_products';
258 } elseif ( isset( $atts['top_rated'] ) && wc_string_to_bool( $atts['top_rated'] ) ) {
259 $type = 'top_rated_products';
260 }
261
262 $shortcode = new WC_Shortcode_Products( $atts, $type );
263
264 return $shortcode->get_content();
265 }
266
267 /**
268 * Display a single product.
269 *
270 * @param array $atts Attributes.
271 * @return string
272 */
273 public static function product( $atts ) {
274 if ( empty( $atts ) ) {
275 return '';
276 }
277
278 $atts['skus'] = isset( $atts['sku'] ) ? $atts['sku'] : '';
279 $atts['ids'] = isset( $atts['id'] ) ? $atts['id'] : '';
280 $atts['limit'] = '1';
281 $shortcode = new WC_Shortcode_Products( (array) $atts, 'product' );
282
283 return $shortcode->get_content();
284 }
285
286 /**
287 * Display a single product price + cart button.
288 *
289 * @param array $atts Attributes.
290 * @return string
291 */
292 public static function product_add_to_cart( $atts ) {
293 global $post;
294
295 if ( empty( $atts ) ) {
296 return '';
297 }
298
299 $atts = shortcode_atts( array(
300 'id' => '',
301 'class' => '',
302 'quantity' => '1',
303 'sku' => '',
304 'style' => 'border:4px solid #ccc; padding: 12px;',
305 'show_price' => 'true',
306 ), $atts, 'product_add_to_cart' );
307
308 if ( ! empty( $atts['id'] ) ) {
309 $product_data = get_post( $atts['id'] );
310 } elseif ( ! empty( $atts['sku'] ) ) {
311 $product_id = wc_get_product_id_by_sku( $atts['sku'] );
312 $product_data = get_post( $product_id );
313 } else {
314 return '';
315 }
316
317 $product = is_object( $product_data ) && in_array( $product_data->post_type, array( 'product', 'product_variation' ), true ) ? wc_setup_product_data( $product_data ) : false;
318
319 if ( ! $product ) {
320 return '';
321 }
322
323 ob_start();
324
325 echo '<p class="product woocommerce add_to_cart_inline ' . esc_attr( $atts['class'] ) . '" style="' . ( empty( $atts['style'] ) ? '' : esc_attr( $atts['style'] ) ) . '">';
326
327 if ( wc_string_to_bool( $atts['show_price'] ) ) {
328 // @codingStandardsIgnoreStart
329 echo $product->get_price_html();
330 // @codingStandardsIgnoreEnd
331 }
332
333 woocommerce_template_loop_add_to_cart( array(
334 'quantity' => $atts['quantity'],
335 ) );
336
337 echo '</p>';
338
339 // Restore Product global in case this is shown inside a product post.
340 wc_setup_product_data( $post );
341
342 return ob_get_clean();
343 }
344
345 /**
346 * Get the add to cart URL for a product.
347 *
348 * @param array $atts Attributes.
349 * @return string
350 */
351 public static function product_add_to_cart_url( $atts ) {
352 if ( empty( $atts ) ) {
353 return '';
354 }
355
356 if ( isset( $atts['id'] ) ) {
357 $product_data = get_post( $atts['id'] );
358 } elseif ( isset( $atts['sku'] ) ) {
359 $product_id = wc_get_product_id_by_sku( $atts['sku'] );
360 $product_data = get_post( $product_id );
361 } else {
362 return '';
363 }
364
365 $product = is_object( $product_data ) && in_array( $product_data->post_type, array( 'product', 'product_variation' ), true ) ? wc_setup_product_data( $product_data ) : false;
366
367 if ( ! $product ) {
368 return '';
369 }
370
371 $_product = wc_get_product( $product_data );
372
373 return esc_url( $_product->add_to_cart_url() );
374 }
375
376 /**
377 * List all products on sale.
378 *
379 * @param array $atts Attributes.
380 * @return string
381 */
382 public static function sale_products( $atts ) {
383 $atts = array_merge( array(
384 'limit' => '12',
385 'columns' => '4',
386 'orderby' => 'title',
387 'order' => 'ASC',
388 'category' => '',
389 'cat_operator' => 'IN',
390 ), (array) $atts );
391
392 $shortcode = new WC_Shortcode_Products( $atts, 'sale_products' );
393
394 return $shortcode->get_content();
395 }
396
397 /**
398 * List best selling products on sale.
399 *
400 * @param array $atts Attributes.
401 * @return string
402 */
403 public static function best_selling_products( $atts ) {
404 $atts = array_merge( array(
405 'limit' => '12',
406 'columns' => '4',
407 'category' => '',
408 'cat_operator' => 'IN',
409 ), (array) $atts );
410
411 $shortcode = new WC_Shortcode_Products( $atts, 'best_selling_products' );
412
413 return $shortcode->get_content();
414 }
415
416 /**
417 * List top rated products on sale.
418 *
419 * @param array $atts Attributes.
420 * @return string
421 */
422 public static function top_rated_products( $atts ) {
423 $atts = array_merge( array(
424 'limit' => '12',
425 'columns' => '4',
426 'orderby' => 'title',
427 'order' => 'ASC',
428 'category' => '',
429 'cat_operator' => 'IN',
430 ), (array) $atts );
431
432 $shortcode = new WC_Shortcode_Products( $atts, 'top_rated_products' );
433
434 return $shortcode->get_content();
435 }
436
437 /**
438 * Output featured products.
439 *
440 * @param array $atts Attributes.
441 * @return string
442 */
443 public static function featured_products( $atts ) {
444 $atts = array_merge( array(
445 'limit' => '12',
446 'columns' => '4',
447 'orderby' => 'date',
448 'order' => 'DESC',
449 'category' => '',
450 'cat_operator' => 'IN',
451 ), (array) $atts );
452
453 $atts['visibility'] = 'featured';
454
455 $shortcode = new WC_Shortcode_Products( $atts, 'featured_products' );
456
457 return $shortcode->get_content();
458 }
459
460 /**
461 * Show a single product page.
462 *
463 * @param array $atts Attributes.
464 * @return string
465 */
466 public static function product_page( $atts ) {
467 if ( empty( $atts ) ) {
468 return '';
469 }
470
471 if ( ! isset( $atts['id'] ) && ! isset( $atts['sku'] ) ) {
472 return '';
473 }
474
475 $args = array(
476 'posts_per_page' => 1,
477 'post_type' => 'product',
478 'post_status' => 'publish',
479 'ignore_sticky_posts' => 1,
480 'no_found_rows' => 1,
481 );
482
483 if ( isset( $atts['sku'] ) ) {
484 $args['meta_query'][] = array(
485 'key' => '_sku',
486 'value' => sanitize_text_field( $atts['sku'] ),
487 'compare' => '=',
488 );
489
490 $args['post_type'] = array( 'product', 'product_variation' );
491 }
492
493 if ( isset( $atts['id'] ) ) {
494 $args['p'] = absint( $atts['id'] );
495 }
496
497 // Don't render titles if desired.
498 if ( isset( $atts['show_title'] ) && ! $atts['show_title'] ) {
499 remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_title', 5 );
500 }
501
502 // Change form action to avoid redirect.
503 add_filter( 'woocommerce_add_to_cart_form_action', '__return_empty_string' );
504
505 $single_product = new WP_Query( $args );
506
507 $preselected_id = '0';
508
509 // Check if sku is a variation.
510 if ( isset( $atts['sku'] ) && $single_product->have_posts() && 'product_variation' === $single_product->post->post_type ) {
511
512 $variation = new WC_Product_Variation( $single_product->post->ID );
513 $attributes = $variation->get_attributes();
514
515 // Set preselected id to be used by JS to provide context.
516 $preselected_id = $single_product->post->ID;
517
518 // Get the parent product object.
519 $args = array(
520 'posts_per_page' => 1,
521 'post_type' => 'product',
522 'post_status' => 'publish',
523 'ignore_sticky_posts' => 1,
524 'no_found_rows' => 1,
525 'p' => $single_product->post->post_parent,
526 );
527
528 $single_product = new WP_Query( $args );
529 ?>
530 <script type="text/javascript">
531 jQuery( document ).ready( function( $ ) {
532 var $variations_form = $( '[data-product-page-preselected-id="<?php echo esc_attr( $preselected_id ); ?>"]' ).find( 'form.variations_form' );
533
534 <?php foreach ( $attributes as $attr => $value ) { ?>
535 $variations_form.find( 'select[name="<?php echo esc_attr( $attr ); ?>"]' ).val( '<?php echo esc_js( $value ); ?>' );
536 <?php } ?>
537 });
538 </script>
539 <?php
540 }
541
542 // For "is_single" to always make load comments_template() for reviews.
543 $single_product->is_single = true;
544
545 ob_start();
546
547 global $wp_query;
548
549 // Backup query object so following loops think this is a product page.
550 $previous_wp_query = $wp_query;
551 // @codingStandardsIgnoreStart
552 $wp_query = $single_product;
553 // @codingStandardsIgnoreEnd
554
555 wp_enqueue_script( 'wc-single-product' );
556
557 while ( $single_product->have_posts() ) {
558 $single_product->the_post()
559 ?>
560 <div class="single-product" data-product-page-preselected-id="<?php echo esc_attr( $preselected_id ); ?>">
561 <?php wc_get_template_part( 'content', 'single-product' ); ?>
562 </div>
563 <?php
564 }
565
566 // Restore $previous_wp_query and reset post data.
567 // @codingStandardsIgnoreStart
568 $wp_query = $previous_wp_query;
569 // @codingStandardsIgnoreEnd
570 wp_reset_postdata();
571
572 // Re-enable titles if they were removed.
573 if ( isset( $atts['show_title'] ) && ! $atts['show_title'] ) {
574 add_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_title', 5 );
575 }
576
577 remove_filter( 'woocommerce_add_to_cart_form_action', '__return_empty_string' );
578
579 return '<div class="woocommerce">' . ob_get_clean() . '</div>';
580 }
581
582 /**
583 * Show messages.
584 *
585 * @return string
586 */
587 public static function shop_messages() {
588 ob_start();
589 wc_print_notices();
590 return '<div class="woocommerce">' . ob_get_clean() . '</div>';
591 }
592
593 /**
594 * Order by rating.
595 *
596 * @deprecated 3.2.0 Use WC_Shortcode_Products::order_by_rating_post_clauses().
597 * @param array $args Query args.
598 * @return array
599 */
600 public static function order_by_rating_post_clauses( $args ) {
601 return WC_Shortcode_Products::order_by_rating_post_clauses( $args );
602 }
603
604 /**
605 * List products with an attribute shortcode.
606 * Example [product_attribute attribute="color" filter="black"].
607 *
608 * @param array $atts Attributes.
609 * @return string
610 */
611 public static function product_attribute( $atts ) {
612 $atts = array_merge( array(
613 'limit' => '12',
614 'columns' => '4',
615 'orderby' => 'title',
616 'order' => 'ASC',
617 'attribute' => '',
618 'terms' => '',
619 ), (array) $atts );
620
621 if ( empty( $atts['attribute'] ) ) {
622 return '';
623 }
624
625 $shortcode = new WC_Shortcode_Products( $atts, 'product_attribute' );
626
627 return $shortcode->get_content();
628 }
629
630 /**
631 * List related products.
632 *
633 * @param array $atts Attributes.
634 * @return string
635 */
636 public static function related_products( $atts ) {
637 if ( isset( $atts['per_page'] ) ) {
638 $atts['limit'] = $atts['per_page'];
639 }
640
641 // @codingStandardsIgnoreStart
642 $atts = shortcode_atts( array(
643 'limit' => '4',
644 'columns' => '4',
645 'orderby' => 'rand',
646 ), $atts, 'related_products' );
647 // @codingStandardsIgnoreEnd
648
649 ob_start();
650
651 // Rename arg.
652 $atts['posts_per_page'] = absint( $atts['limit'] );
653
654 woocommerce_related_products( $atts );
655
656 return ob_get_clean();
657 }
658 }
659