Nessuna descrizione

class-wc-shortcodes.php 17KB

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