/includes/shortcodes/class-wc-shortcode-checkout.php

https://bitbucket.org/pless84/woocommerce · PHP · 231 lines · 123 code · 60 blank · 48 comment · 34 complexity · 8906041e67c4cf7c5f1cdfc1db32af23 MD5 · raw file

  1. <?php
  2. /**
  3. * Checkout Shortcode
  4. *
  5. * Used on the checkout page, the checkout shortcode displays the checkout process.
  6. *
  7. * @author WooThemes
  8. * @category Shortcodes
  9. * @package WooCommerce/Shortcodes/Checkout
  10. * @version 2.0.0
  11. */
  12. class WC_Shortcode_Checkout {
  13. /**
  14. * Get the shortcode content.
  15. *
  16. * @access public
  17. * @param array $atts
  18. * @return string
  19. */
  20. public static function get( $atts ) {
  21. global $woocommerce;
  22. return $woocommerce->get_helper( 'shortcode' )->shortcode_wrapper( array( __CLASS__, 'output' ), $atts );
  23. }
  24. /**
  25. * Output the shortcode.
  26. *
  27. * @access public
  28. * @param array $atts
  29. * @return void
  30. */
  31. public static function output( $atts ) {
  32. global $woocommerce, $wp;
  33. // Backwards compat with old pay and thanks link arguments
  34. if ( isset( $_GET['order'] ) && isset( $_GET['key'] ) ) {
  35. _deprecated_argument( __CLASS__ . '->' . __FUNCTION__, '2.1', '"order" is no longer used to pass an order ID. Use the order-pay or order-received endpoint instead.' );
  36. // Get the order to work out what we are showing
  37. $order_id = absint( $_GET['order'] );
  38. $order = new WC_Order( $order_id );
  39. if ( $order->status == 'pending' )
  40. $wp->query_vars['order-pay'] = absint( $_GET['order'] );
  41. else
  42. $wp->query_vars['order-received'] = absint( $_GET['order'] );
  43. }
  44. // Handle checkout actions
  45. if ( ! empty( $wp->query_vars['order-pay'] ) ) {
  46. self::order_pay( $wp->query_vars['order-pay'] );
  47. } elseif ( isset( $wp->query_vars['order-received'] ) ) {
  48. self::order_received( $wp->query_vars['order-received'] );
  49. } else {
  50. self::checkout();
  51. }
  52. }
  53. /**
  54. * Show the pay page
  55. */
  56. private function order_pay( $order_id ) {
  57. global $woocommerce;
  58. do_action( 'before_woocommerce_pay' );
  59. wc_print_messages();
  60. $order_id = absint( $order_id );
  61. // Handle payment
  62. if ( isset( $_GET['pay_for_order'] ) && isset( $_GET['key'] ) && $order_id ) {
  63. // Pay for existing order
  64. $order_key = urldecode( $_GET[ 'key' ] );
  65. $order_id = absint( $order_id );
  66. $order = new WC_Order( $order_id );
  67. $valid_order_statuses = apply_filters( 'woocommerce_valid_order_statuses_for_payment', array( 'pending', 'failed' ), $order );
  68. if ( $order->id == $order_id && $order->order_key == $order_key && in_array( $order->status, $valid_order_statuses ) ) {
  69. // Set customer location to order location
  70. if ( $order->billing_country )
  71. $woocommerce->customer->set_country( $order->billing_country );
  72. if ( $order->billing_state )
  73. $woocommerce->customer->set_state( $order->billing_state );
  74. if ( $order->billing_postcode )
  75. $woocommerce->customer->set_postcode( $order->billing_postcode );
  76. // Show form
  77. woocommerce_get_template( 'checkout/form-pay.php', array( 'order' => $order ) );
  78. } elseif ( ! in_array( $order->status, $valid_order_statuses ) ) {
  79. wc_add_error( __( 'Your order has already been paid for. Please contact us if you need assistance.', 'woocommerce' ) );
  80. wc_print_messages();
  81. } else {
  82. wc_add_error( __( 'Invalid order.', 'woocommerce' ) );
  83. wc_print_messages();
  84. }
  85. } elseif ( $order_id ) {
  86. // Pay for order after checkout step
  87. $order_key = isset( $_GET['key'] ) ? woocommerce_clean( $_GET['key'] ) : '';
  88. $order = new WC_Order( $order_id );
  89. $valid_order_statuses = apply_filters( 'woocommerce_valid_order_statuses_for_payment', array( 'pending', 'failed' ), $order );
  90. if ( $order->order_key == $order_key && in_array( $order->status, $valid_order_statuses ) ) {
  91. ?>
  92. <ul class="order_details">
  93. <li class="order">
  94. <?php _e( 'Order:', 'woocommerce' ); ?>
  95. <strong><?php echo $order->get_order_number(); ?></strong>
  96. </li>
  97. <li class="date">
  98. <?php _e( 'Date:', 'woocommerce' ); ?>
  99. <strong><?php echo date_i18n(get_option('date_format'), strtotime($order->order_date)); ?></strong>
  100. </li>
  101. <li class="total">
  102. <?php _e( 'Total:', 'woocommerce' ); ?>
  103. <strong><?php echo $order->get_formatted_order_total(); ?></strong>
  104. </li>
  105. <?php if ($order->payment_method_title) : ?>
  106. <li class="method">
  107. <?php _e( 'Payment method:', 'woocommerce' ); ?>
  108. <strong><?php
  109. echo $order->payment_method_title;
  110. ?></strong>
  111. </li>
  112. <?php endif; ?>
  113. </ul>
  114. <?php do_action( 'woocommerce_receipt_' . $order->payment_method, $order_id ); ?>
  115. <div class="clear"></div>
  116. <?php
  117. } elseif ( ! in_array( $order->status, $valid_order_statuses ) ) {
  118. wc_add_error( __( 'Your order has already been paid for. Please contact us if you need assistance.', 'woocommerce' ) );
  119. wc_print_messages();
  120. }
  121. } else {
  122. wc_add_error( __( 'Invalid order.', 'woocommerce' ) );
  123. wc_print_messages();
  124. }
  125. do_action( 'after_woocommerce_pay' );
  126. }
  127. /**
  128. * Show the thanks page
  129. */
  130. private function order_received( $order_id = 0 ) {
  131. global $woocommerce;
  132. wc_print_messages();
  133. $order = false;
  134. // Get the order
  135. $order_id = apply_filters( 'woocommerce_thankyou_order_id', absint( $order_id ) );
  136. $order_key = apply_filters( 'woocommerce_thankyou_order_key', empty( $_GET['key'] ) ? '' : woocommerce_clean( $_GET['key'] ) );
  137. if ( $order_id > 0 ) {
  138. $order = new WC_Order( $order_id );
  139. if ( $order->order_key != $order_key )
  140. unset( $order );
  141. }
  142. // Empty awaiting payment session
  143. unset( $woocommerce->session->order_awaiting_payment );
  144. woocommerce_get_template( 'checkout/thankyou.php', array( 'order' => $order ) );
  145. }
  146. /**
  147. * Show the checkout
  148. */
  149. private function checkout() {
  150. global $woocommerce;
  151. // Show non-cart errors
  152. wc_print_messages();
  153. // Check cart has contents
  154. if ( sizeof( $woocommerce->cart->get_cart() ) == 0 )
  155. return;
  156. // Calc totals
  157. $woocommerce->cart->calculate_totals();
  158. // Check cart contents for errors
  159. do_action('woocommerce_check_cart_items');
  160. // Get checkout object
  161. $checkout = $woocommerce->checkout();
  162. if ( empty( $_POST ) && wc_error_count() > 0 ) {
  163. woocommerce_get_template( 'checkout/cart-errors.php', array( 'checkout' => $checkout ) );
  164. } else {
  165. $non_js_checkout = ! empty( $_POST['woocommerce_checkout_update_totals'] ) ? true : false;
  166. if ( wc_error_count() == 0 && $non_js_checkout )
  167. wc_add_message( __( 'The order totals have been updated. Please confirm your order by pressing the Place Order button at the bottom of the page.', 'woocommerce' ) );
  168. woocommerce_get_template( 'checkout/form-checkout.php', array( 'checkout' => $checkout ) );
  169. }
  170. }
  171. }