class-wc-shortcodes.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658
  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( array(
  116. 'limit' => '12',
  117. 'columns' => '4',
  118. 'orderby' => 'menu_order title',
  119. 'order' => 'ASC',
  120. 'category' => '',
  121. 'cat_operator' => 'IN',
  122. ), (array) $atts );
  123. $shortcode = new WC_Shortcode_Products( $atts, 'product_category' );
  124. return $shortcode->get_content();
  125. }
  126. /**
  127. * List all (or limited) product categories.
  128. *
  129. * @param array $atts Attributes.
  130. * @return string
  131. */
  132. public static function product_categories( $atts ) {
  133. if ( isset( $atts['number'] ) ) {
  134. $atts['limit'] = $atts['number'];
  135. }
  136. $atts = shortcode_atts( array(
  137. 'limit' => '-1',
  138. 'orderby' => 'name',
  139. 'order' => 'ASC',
  140. 'columns' => '4',
  141. 'hide_empty' => 1,
  142. 'parent' => '',
  143. 'ids' => '',
  144. ), $atts, 'product_categories' );
  145. $ids = array_filter( array_map( 'trim', explode( ',', $atts['ids'] ) ) );
  146. $hide_empty = ( true === $atts['hide_empty'] || 'true' === $atts['hide_empty'] || 1 === $atts['hide_empty'] || '1' === $atts['hide_empty'] ) ? 1 : 0;
  147. // Get terms and workaround WP bug with parents/pad counts.
  148. $args = array(
  149. 'orderby' => $atts['orderby'],
  150. 'order' => $atts['order'],
  151. 'hide_empty' => $hide_empty,
  152. 'include' => $ids,
  153. 'pad_counts' => true,
  154. 'child_of' => $atts['parent'],
  155. );
  156. $product_categories = get_terms( 'product_cat', $args );
  157. if ( '' !== $atts['parent'] ) {
  158. $product_categories = wp_list_filter( $product_categories, array(
  159. 'parent' => $atts['parent'],
  160. ) );
  161. }
  162. if ( $hide_empty ) {
  163. foreach ( $product_categories as $key => $category ) {
  164. if ( 0 === $category->count ) {
  165. unset( $product_categories[ $key ] );
  166. }
  167. }
  168. }
  169. $atts['limit'] = '-1' === $atts['limit'] ? null : intval( $atts['limit'] );
  170. if ( $atts['limit'] ) {
  171. $product_categories = array_slice( $product_categories, 0, $atts['limit'] );
  172. }
  173. $columns = absint( $atts['columns'] );
  174. wc_set_loop_prop( 'columns', $columns );
  175. wc_set_loop_prop( 'is_shortcode', true );
  176. ob_start();
  177. if ( $product_categories ) {
  178. woocommerce_product_loop_start();
  179. foreach ( $product_categories as $category ) {
  180. wc_get_template( 'content-product_cat.php', array(
  181. 'category' => $category,
  182. ) );
  183. }
  184. woocommerce_product_loop_end();
  185. }
  186. woocommerce_reset_loop();
  187. return '<div class="woocommerce columns-' . $columns . '">' . ob_get_clean() . '</div>';
  188. }
  189. /**
  190. * Recent Products shortcode.
  191. *
  192. * @param array $atts Attributes.
  193. * @return string
  194. */
  195. public static function recent_products( $atts ) {
  196. $atts = array_merge( array(
  197. 'limit' => '12',
  198. 'columns' => '4',
  199. 'orderby' => 'date',
  200. 'order' => 'DESC',
  201. 'category' => '',
  202. 'cat_operator' => 'IN',
  203. ), (array) $atts );
  204. $shortcode = new WC_Shortcode_Products( $atts, 'recent_products' );
  205. return $shortcode->get_content();
  206. }
  207. /**
  208. * List multiple products shortcode.
  209. *
  210. * @param array $atts Attributes.
  211. * @return string
  212. */
  213. public static function products( $atts ) {
  214. $atts = (array) $atts;
  215. $type = 'products';
  216. // Allow list product based on specific cases.
  217. if ( isset( $atts['on_sale'] ) && wc_string_to_bool( $atts['on_sale'] ) ) {
  218. $type = 'sale_products';
  219. } elseif ( isset( $atts['best_selling'] ) && wc_string_to_bool( $atts['best_selling'] ) ) {
  220. $type = 'best_selling_products';
  221. } elseif ( isset( $atts['top_rated'] ) && wc_string_to_bool( $atts['top_rated'] ) ) {
  222. $type = 'top_rated_products';
  223. }
  224. $shortcode = new WC_Shortcode_Products( $atts, $type );
  225. return $shortcode->get_content();
  226. }
  227. /**
  228. * Display a single product.
  229. *
  230. * @param array $atts Attributes.
  231. * @return string
  232. */
  233. public static function product( $atts ) {
  234. if ( empty( $atts ) ) {
  235. return '';
  236. }
  237. $atts['skus'] = isset( $atts['sku'] ) ? $atts['sku'] : '';
  238. $atts['ids'] = isset( $atts['id'] ) ? $atts['id'] : '';
  239. $atts['limit'] = '1';
  240. $shortcode = new WC_Shortcode_Products( (array) $atts, 'product' );
  241. return $shortcode->get_content();
  242. }
  243. /**
  244. * Display a single product price + cart button.
  245. *
  246. * @param array $atts Attributes.
  247. * @return string
  248. */
  249. public static function product_add_to_cart( $atts ) {
  250. global $post;
  251. if ( empty( $atts ) ) {
  252. return '';
  253. }
  254. $atts = shortcode_atts( array(
  255. 'id' => '',
  256. 'class' => '',
  257. 'quantity' => '1',
  258. 'sku' => '',
  259. 'style' => 'border:4px solid #ccc; padding: 12px;',
  260. 'show_price' => 'true',
  261. ), $atts, 'product_add_to_cart' );
  262. if ( ! empty( $atts['id'] ) ) {
  263. $product_data = get_post( $atts['id'] );
  264. } elseif ( ! empty( $atts['sku'] ) ) {
  265. $product_id = wc_get_product_id_by_sku( $atts['sku'] );
  266. $product_data = get_post( $product_id );
  267. } else {
  268. return '';
  269. }
  270. $product = is_object( $product_data ) && in_array( $product_data->post_type, array( 'product', 'product_variation' ), true ) ? wc_setup_product_data( $product_data ) : false;
  271. if ( ! $product ) {
  272. return '';
  273. }
  274. ob_start();
  275. echo '<p class="product woocommerce add_to_cart_inline ' . esc_attr( $atts['class'] ) . '" style="' . ( empty( $atts['style'] ) ? '' : esc_attr( $atts['style'] ) ) . '">';
  276. if ( wc_string_to_bool( $atts['show_price'] ) ) {
  277. // @codingStandardsIgnoreStart
  278. echo $product->get_price_html();
  279. // @codingStandardsIgnoreEnd
  280. }
  281. woocommerce_template_loop_add_to_cart( array(
  282. 'quantity' => $atts['quantity'],
  283. ) );
  284. echo '</p>';
  285. // Restore Product global in case this is shown inside a product post.
  286. wc_setup_product_data( $post );
  287. return ob_get_clean();
  288. }
  289. /**
  290. * Get the add to cart URL for a product.
  291. *
  292. * @param array $atts Attributes.
  293. * @return string
  294. */
  295. public static function product_add_to_cart_url( $atts ) {
  296. if ( empty( $atts ) ) {
  297. return '';
  298. }
  299. if ( isset( $atts['id'] ) ) {
  300. $product_data = get_post( $atts['id'] );
  301. } elseif ( isset( $atts['sku'] ) ) {
  302. $product_id = wc_get_product_id_by_sku( $atts['sku'] );
  303. $product_data = get_post( $product_id );
  304. } else {
  305. return '';
  306. }
  307. $product = is_object( $product_data ) && in_array( $product_data->post_type, array( 'product', 'product_variation' ), true ) ? wc_setup_product_data( $product_data ) : false;
  308. if ( ! $product ) {
  309. return '';
  310. }
  311. $_product = wc_get_product( $product_data );
  312. return esc_url( $_product->add_to_cart_url() );
  313. }
  314. /**
  315. * List all products on sale.
  316. *
  317. * @param array $atts Attributes.
  318. * @return string
  319. */
  320. public static function sale_products( $atts ) {
  321. $atts = array_merge( array(
  322. 'limit' => '12',
  323. 'columns' => '4',
  324. 'orderby' => 'title',
  325. 'order' => 'ASC',
  326. 'category' => '',
  327. 'cat_operator' => 'IN',
  328. ), (array) $atts );
  329. $shortcode = new WC_Shortcode_Products( $atts, 'sale_products' );
  330. return $shortcode->get_content();
  331. }
  332. /**
  333. * List best selling products on sale.
  334. *
  335. * @param array $atts Attributes.
  336. * @return string
  337. */
  338. public static function best_selling_products( $atts ) {
  339. $atts = array_merge( array(
  340. 'limit' => '12',
  341. 'columns' => '4',
  342. 'category' => '',
  343. 'cat_operator' => 'IN',
  344. ), (array) $atts );
  345. $shortcode = new WC_Shortcode_Products( $atts, 'best_selling_products' );
  346. return $shortcode->get_content();
  347. }
  348. /**
  349. * List top rated products on sale.
  350. *
  351. * @param array $atts Attributes.
  352. * @return string
  353. */
  354. public static function top_rated_products( $atts ) {
  355. $atts = array_merge( array(
  356. 'limit' => '12',
  357. 'columns' => '4',
  358. 'orderby' => 'title',
  359. 'order' => 'ASC',
  360. 'category' => '',
  361. 'cat_operator' => 'IN',
  362. ), (array) $atts );
  363. $shortcode = new WC_Shortcode_Products( $atts, 'top_rated_products' );
  364. return $shortcode->get_content();
  365. }
  366. /**
  367. * Output featured products.
  368. *
  369. * @param array $atts Attributes.
  370. * @return string
  371. */
  372. public static function featured_products( $atts ) {
  373. $atts = array_merge( array(
  374. 'limit' => '12',
  375. 'columns' => '4',
  376. 'orderby' => 'date',
  377. 'order' => 'DESC',
  378. 'category' => '',
  379. 'cat_operator' => 'IN',
  380. ), (array) $atts );
  381. $atts['visibility'] = 'featured';
  382. $shortcode = new WC_Shortcode_Products( $atts, 'featured_products' );
  383. return $shortcode->get_content();
  384. }
  385. /**
  386. * Show a single product page.
  387. *
  388. * @param array $atts Attributes.
  389. * @return string
  390. */
  391. public static function product_page( $atts ) {
  392. if ( empty( $atts ) ) {
  393. return '';
  394. }
  395. if ( ! isset( $atts['id'] ) && ! isset( $atts['sku'] ) ) {
  396. return '';
  397. }
  398. $args = array(
  399. 'posts_per_page' => 1,
  400. 'post_type' => 'product',
  401. 'post_status' => 'publish',
  402. 'ignore_sticky_posts' => 1,
  403. 'no_found_rows' => 1,
  404. );
  405. if ( isset( $atts['sku'] ) ) {
  406. $args['meta_query'][] = array(
  407. 'key' => '_sku',
  408. 'value' => sanitize_text_field( $atts['sku'] ),
  409. 'compare' => '=',
  410. );
  411. $args['post_type'] = array( 'product', 'product_variation' );
  412. }
  413. if ( isset( $atts['id'] ) ) {
  414. $args['p'] = absint( $atts['id'] );
  415. }
  416. // Don't render titles if desired.
  417. if ( isset( $atts['show_title'] ) && ! $atts['show_title'] ) {
  418. remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_title', 5 );
  419. }
  420. // Change form action to avoid redirect.
  421. add_filter( 'woocommerce_add_to_cart_form_action', '__return_empty_string' );
  422. $single_product = new WP_Query( $args );
  423. $preselected_id = '0';
  424. // Check if sku is a variation.
  425. if ( isset( $atts['sku'] ) && $single_product->have_posts() && 'product_variation' === $single_product->post->post_type ) {
  426. $variation = new WC_Product_Variation( $single_product->post->ID );
  427. $attributes = $variation->get_attributes();
  428. // Set preselected id to be used by JS to provide context.
  429. $preselected_id = $single_product->post->ID;
  430. // Get the parent product object.
  431. $args = array(
  432. 'posts_per_page' => 1,
  433. 'post_type' => 'product',
  434. 'post_status' => 'publish',
  435. 'ignore_sticky_posts' => 1,
  436. 'no_found_rows' => 1,
  437. 'p' => $single_product->post->post_parent,
  438. );
  439. $single_product = new WP_Query( $args );
  440. ?>
  441. <script type="text/javascript">
  442. jQuery( document ).ready( function( $ ) {
  443. var $variations_form = $( '[data-product-page-preselected-id="<?php echo esc_attr( $preselected_id ); ?>"]' ).find( 'form.variations_form' );
  444. <?php foreach ( $attributes as $attr => $value ) { ?>
  445. $variations_form.find( 'select[name="<?php echo esc_attr( $attr ); ?>"]' ).val( '<?php echo esc_js( $value ); ?>' );
  446. <?php } ?>
  447. });
  448. </script>
  449. <?php
  450. }
  451. // For "is_single" to always make load comments_template() for reviews.
  452. $single_product->is_single = true;
  453. ob_start();
  454. global $wp_query;
  455. // Backup query object so following loops think this is a product page.
  456. $previous_wp_query = $wp_query;
  457. // @codingStandardsIgnoreStart
  458. $wp_query = $single_product;
  459. // @codingStandardsIgnoreEnd
  460. wp_enqueue_script( 'wc-single-product' );
  461. while ( $single_product->have_posts() ) {
  462. $single_product->the_post()
  463. ?>
  464. <div class="single-product" data-product-page-preselected-id="<?php echo esc_attr( $preselected_id ); ?>">
  465. <?php wc_get_template_part( 'content', 'single-product' ); ?>
  466. </div>
  467. <?php
  468. }
  469. // Restore $previous_wp_query and reset post data.
  470. // @codingStandardsIgnoreStart
  471. $wp_query = $previous_wp_query;
  472. // @codingStandardsIgnoreEnd
  473. wp_reset_postdata();
  474. // Re-enable titles if they were removed.
  475. if ( isset( $atts['show_title'] ) && ! $atts['show_title'] ) {
  476. add_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_title', 5 );
  477. }
  478. remove_filter( 'woocommerce_add_to_cart_form_action', '__return_empty_string' );
  479. return '<div class="woocommerce">' . ob_get_clean() . '</div>';
  480. }
  481. /**
  482. * Show messages.
  483. *
  484. * @return string
  485. */
  486. public static function shop_messages() {
  487. ob_start();
  488. wc_print_notices();
  489. return '<div class="woocommerce">' . ob_get_clean() . '</div>';
  490. }
  491. /**
  492. * Order by rating.
  493. *
  494. * @deprecated 3.2.0 Use WC_Shortcode_Products::order_by_rating_post_clauses().
  495. * @param array $args Query args.
  496. * @return array
  497. */
  498. public static function order_by_rating_post_clauses( $args ) {
  499. return WC_Shortcode_Products::order_by_rating_post_clauses( $args );
  500. }
  501. /**
  502. * List products with an attribute shortcode.
  503. * Example [product_attribute attribute="color" filter="black"].
  504. *
  505. * @param array $atts Attributes.
  506. * @return string
  507. */
  508. public static function product_attribute( $atts ) {
  509. $atts = array_merge( array(
  510. 'limit' => '12',
  511. 'columns' => '4',
  512. 'orderby' => 'title',
  513. 'order' => 'ASC',
  514. 'attribute' => '',
  515. 'terms' => '',
  516. ), (array) $atts );
  517. if ( empty( $atts['attribute'] ) ) {
  518. return '';
  519. }
  520. $shortcode = new WC_Shortcode_Products( $atts, 'product_attribute' );
  521. return $shortcode->get_content();
  522. }
  523. /**
  524. * List related products.
  525. *
  526. * @param array $atts Attributes.
  527. * @return string
  528. */
  529. public static function related_products( $atts ) {
  530. if ( isset( $atts['per_page'] ) ) {
  531. $atts['limit'] = $atts['per_page'];
  532. }
  533. // @codingStandardsIgnoreStart
  534. $atts = shortcode_atts( array(
  535. 'limit' => '4',
  536. 'columns' => '4',
  537. 'orderby' => 'rand',
  538. ), $atts, 'related_products' );
  539. // @codingStandardsIgnoreEnd
  540. ob_start();
  541. // Rename arg.
  542. $atts['posts_per_page'] = absint( $atts['limit'] );
  543. woocommerce_related_products( $atts );
  544. return ob_get_clean();
  545. }
  546. }