WC_Shortcode_Checkout

Checkout Shortcode.

Defined (1)

The class is defined in the following location(s).

/includes/shortcodes/class-wc-shortcode-checkout.php  
  1. class WC_Shortcode_Checkout { 
  2.  
  3. /** 
  4. * Get the shortcode content. 
  5. * @param array $atts 
  6. * @return string 
  7. */ 
  8. public static function get( $atts ) { 
  9. return WC_Shortcodes::shortcode_wrapper( array( __CLASS__, 'output' ), $atts ); 
  10.  
  11. /** 
  12. * Output the shortcode. 
  13. * @param array $atts 
  14. */ 
  15. public static function output( $atts ) { 
  16. global $wp; 
  17.  
  18. // Check cart class is loaded or abort 
  19. if ( is_null( WC()->cart ) ) { 
  20. return; 
  21.  
  22. $atts = shortcode_atts( array(), $atts, 'woocommerce_checkout' ); 
  23.  
  24. // Backwards compat with old pay and thanks link arguments 
  25. if ( isset( $_GET['order'] ) && isset( $_GET['key'] ) ) { 
  26. wc_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.' ); 
  27.  
  28. // Get the order to work out what we are showing 
  29. $order_id = absint( $_GET['order'] ); 
  30. $order = wc_get_order( $order_id ); 
  31.  
  32. if ( $order && $order->has_status( 'pending' ) ) { 
  33. $wp->query_vars['order-pay'] = absint( $_GET['order'] ); 
  34. } else { 
  35. $wp->query_vars['order-received'] = absint( $_GET['order'] ); 
  36.  
  37. // Handle checkout actions 
  38. if ( ! empty( $wp->query_vars['order-pay'] ) ) { 
  39.  
  40. self::order_pay( $wp->query_vars['order-pay'] ); 
  41.  
  42. } elseif ( isset( $wp->query_vars['order-received'] ) ) { 
  43.  
  44. self::order_received( $wp->query_vars['order-received'] ); 
  45.  
  46. } else { 
  47.  
  48. self::checkout(); 
  49.  
  50.  
  51. /** 
  52. * Show the pay page. 
  53. * @param int $order_id 
  54. */ 
  55. private static function order_pay( $order_id ) { 
  56.  
  57. do_action( 'before_woocommerce_pay' ); 
  58.  
  59. wc_print_notices(); 
  60.  
  61. $order_id = absint( $order_id ); 
  62.  
  63. // Handle payment 
  64. if ( isset( $_GET['pay_for_order'] ) && isset( $_GET['key'] ) && $order_id ) { 
  65.  
  66. // Pay for existing order 
  67. $order_key = $_GET['key']; 
  68. $order = wc_get_order( $order_id ); 
  69.  
  70. if ( ! current_user_can( 'pay_for_order', $order_id ) ) { 
  71. echo '<div class="woocommerce-error">' . __( 'Invalid order. If you have an account please log in and try again.', 'woocommerce' ) . ' <a href="' . wc_get_page_permalink( 'myaccount' ) . '" class="wc-forward">' . __( 'My account', 'woocommerce' ) . '</a>' . '</div>'; 
  72. return; 
  73.  
  74. if ( $order->get_id() == $order_id && $order->get_order_key() == $order_key ) { 
  75.  
  76. if ( $order->needs_payment() ) { 
  77. WC()->customer->set_props( array( 
  78. 'billing_country' => $order->get_billing_country() ? $order->get_billing_country() : null,  
  79. 'billing_state' => $order->get_billing_state() ? $order->get_billing_state() : null,  
  80. 'billing_postcode' => $order->get_billing_postcode() ? $order->get_billing_postcode() : null,  
  81. ) ); 
  82. WC()->customer->save(); 
  83.  
  84. $available_gateways = WC()->payment_gateways->get_available_payment_gateways(); 
  85.  
  86. if ( sizeof( $available_gateways ) ) { 
  87. current( $available_gateways )->set_current(); 
  88.  
  89. wc_get_template( 'checkout/form-pay.php', array( 
  90. 'order' => $order,  
  91. 'available_gateways' => $available_gateways,  
  92. 'order_button_text' => apply_filters( 'woocommerce_pay_order_button_text', __( 'Pay for order', 'woocommerce' ) ),  
  93. ) ); 
  94.  
  95. } else { 
  96. wc_add_notice( sprintf( __( 'This order’s status is “%s”—it cannot be paid for. Please contact us if you need assistance.', 'woocommerce' ), wc_get_order_status_name( $order->get_status() ) ), 'error' ); 
  97. } else { 
  98. wc_add_notice( __( 'Sorry, this order is invalid and cannot be paid for.', 'woocommerce' ), 'error' ); 
  99. } elseif ( $order_id ) { 
  100.  
  101. // Pay for order after checkout step 
  102. $order_key = isset( $_GET['key'] ) ? wc_clean( $_GET['key'] ) : ''; 
  103. $order = wc_get_order( $order_id ); 
  104.  
  105. if ( $order->get_id() == $order_id && $order->get_order_key() == $order_key ) { 
  106.  
  107. if ( $order->needs_payment() ) { 
  108.  
  109. ?> 
  110. <ul class="order_details"> 
  111. <li class="order"> 
  112. <?php _e( 'Order number:', 'woocommerce' ); ?> 
  113. <strong><?php echo $order->get_order_number(); ?></strong> 
  114. </li> 
  115. <li class="date"> 
  116. <?php _e( 'Date:', 'woocommerce' ); ?> 
  117. <strong><?php echo wc_format_datetime( $order->get_date_created() ); ?></strong> 
  118. </li> 
  119. <li class="total"> 
  120. <?php _e( 'Total:', 'woocommerce' ); ?> 
  121. <strong><?php echo $order->get_formatted_order_total(); ?></strong> 
  122. </li> 
  123. <?php if ( $order->get_payment_method_title() ) : ?> 
  124. <li class="method"> 
  125. <?php _e( 'Payment method:', 'woocommerce' ); ?> 
  126. <strong><?php 
  127. echo wp_kses_post( $order->get_payment_method_title() ); 
  128. ?></strong> 
  129. </li> 
  130. <?php endif; ?> 
  131. </ul> 
  132.  
  133. <?php do_action( 'woocommerce_receipt_' . $order->get_payment_method(), $order_id ); ?> 
  134.  
  135. <div class="clear"></div> 
  136. <?php 
  137.  
  138. } else { 
  139. wc_add_notice( sprintf( __( 'This order’s status is “%s”—it cannot be paid for. Please contact us if you need assistance.', 'woocommerce' ), wc_get_order_status_name( $order->get_status() ) ), 'error' ); 
  140. } else { 
  141. wc_add_notice( __( 'Sorry, this order is invalid and cannot be paid for.', 'woocommerce' ), 'error' ); 
  142. } else { 
  143. wc_add_notice( __( 'Invalid order.', 'woocommerce' ), 'error' ); 
  144.  
  145. wc_print_notices(); 
  146.  
  147. do_action( 'after_woocommerce_pay' ); 
  148.  
  149. /** 
  150. * Show the thanks page. 
  151. * @param int $order_id 
  152. */ 
  153. private static function order_received( $order_id = 0 ) { 
  154.  
  155. wc_print_notices(); 
  156.  
  157. $order = false; 
  158.  
  159. // Get the order 
  160. $order_id = apply_filters( 'woocommerce_thankyou_order_id', absint( $order_id ) ); 
  161. $order_key = apply_filters( 'woocommerce_thankyou_order_key', empty( $_GET['key'] ) ? '' : wc_clean( $_GET['key'] ) ); 
  162.  
  163. if ( $order_id > 0 ) { 
  164. $order = wc_get_order( $order_id ); 
  165. if ( ! $order || $order->get_order_key() !== $order_key ) { 
  166. $order = false; 
  167.  
  168. // Empty awaiting payment session 
  169. unset( WC()->session->order_awaiting_payment ); 
  170.  
  171. // Empty current cart 
  172. wc_empty_cart(); 
  173.  
  174. wc_get_template( 'checkout/thankyou.php', array( 'order' => $order ) ); 
  175.  
  176. /** 
  177. * Show the checkout. 
  178. */ 
  179. private static function checkout() { 
  180.  
  181. // Show non-cart errors 
  182. wc_print_notices(); 
  183.  
  184. // Check cart has contents 
  185. if ( WC()->cart->is_empty() ) { 
  186. return; 
  187.  
  188. // Check cart contents for errors 
  189. do_action( 'woocommerce_check_cart_items' ); 
  190.  
  191. // Calc totals 
  192. WC()->cart->calculate_totals(); 
  193.  
  194. // Get checkout object 
  195. $checkout = WC()->checkout(); 
  196.  
  197. if ( empty( $_POST ) && wc_notice_count( 'error' ) > 0 ) { 
  198.  
  199. wc_get_template( 'checkout/cart-errors.php', array( 'checkout' => $checkout ) ); 
  200.  
  201. } else { 
  202.  
  203. $non_js_checkout = ! empty( $_POST['woocommerce_checkout_update_totals'] ) ? true : false; 
  204.  
  205. if ( wc_notice_count( 'error' ) == 0 && $non_js_checkout ) 
  206. wc_add_notice( __( 'The order totals have been updated. Please confirm your order by pressing the "Place order" button at the bottom of the page.', 'woocommerce' ) ); 
  207.  
  208. wc_get_template( 'checkout/form-checkout.php', array( 'checkout' => $checkout ) ); 
  209.