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 
 13 class WC_Shortcode_Checkout {
 14 
 15     /**
 16      * Get the shortcode content.
 17      *
 18      * @access public
 19      * @param array $atts
 20      * @return string
 21      */
 22     public static function get( $atts ) {
 23         return WC_Shortcodes::shortcode_wrapper( array( __CLASS__, 'output' ), $atts );
 24     }
 25 
 26     /**
 27      * Output the shortcode.
 28      *
 29      * @access public
 30      * @param array $atts
 31      * @return void
 32      */
 33     public static function output( $atts ) {
 34         global $woocommerce, $wp;
 35 
 36         // Check cart class is loaded or abort
 37         if ( is_null( WC()->cart ) ) {
 38             return;
 39         }
 40 
 41         // Backwards compat with old pay and thanks link arguments
 42         if ( isset( $_GET['order'] ) && isset( $_GET['key'] ) ) {
 43             _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.' );
 44 
 45             // Get the order to work out what we are showing
 46             $order_id             = absint( $_GET['order'] );
 47             $order                = new WC_Order( $order_id );
 48 
 49             if ( $order->status == 'pending' )
 50                 $wp->query_vars['order-pay'] = absint( $_GET['order'] );
 51             else
 52                 $wp->query_vars['order-received'] = absint( $_GET['order'] );
 53         }
 54 
 55         // Handle checkout actions
 56         if ( ! empty( $wp->query_vars['order-pay'] ) ) {
 57 
 58             self::order_pay( $wp->query_vars['order-pay'] );
 59 
 60         } elseif ( isset( $wp->query_vars['order-received'] ) ) {
 61 
 62             self::order_received( $wp->query_vars['order-received'] );
 63 
 64         } else {
 65 
 66             self::checkout();
 67 
 68         }
 69     }
 70 
 71     /**
 72      * Show the pay page
 73      */
 74     private static function order_pay( $order_id ) {
 75 
 76         do_action( 'before_woocommerce_pay' );
 77 
 78         wc_print_notices();
 79 
 80         $order_id = absint( $order_id );
 81 
 82         // Handle payment
 83         if ( isset( $_GET['pay_for_order'] ) && isset( $_GET['key'] ) && $order_id ) {
 84 
 85             // Pay for existing order
 86             $order_key            = $_GET[ 'key' ];
 87             $order                = new WC_Order( $order_id );
 88             $valid_order_statuses = apply_filters( 'woocommerce_valid_order_statuses_for_payment', array( 'pending', 'failed' ), $order );
 89 
 90             if ( ! current_user_can( 'pay_for_order', $order_id ) ) {
 91                 echo '<div class="woocommerce-error">' . __( 'Invalid order. If you have an account please log in and try again.', 'woocommerce' ) . ' <a href="' . get_permalink( wc_get_page_id( 'myaccount' ) ) . '" class="wc-forward">' . __( 'My Account', 'woocommerce' ) . '</a>' . '</div>';
 92                 return;
 93             }
 94 
 95             if ( $order->id == $order_id && $order->order_key == $order_key ) {
 96 
 97                 if ( in_array( $order->status, $valid_order_statuses ) ) {
 98 
 99                     // Set customer location to order location
100                     if ( $order->billing_country )
101                         WC()->customer->set_country( $order->billing_country );
102                     if ( $order->billing_state )
103                         WC()->customer->set_state( $order->billing_state );
104                     if ( $order->billing_postcode )
105                         WC()->customer->set_postcode( $order->billing_postcode );
106 
107                     wc_get_template( 'checkout/form-pay.php', array( 'order' => $order ) );
108 
109                 } else {
110 
111                     $status = get_term_by('slug', $order->status, 'shop_order_status');
112 
113                     wc_add_notice( sprintf( __( 'This order&rsquo;s status is &ldquo;%s&rdquo;&mdash;it cannot be paid for. Please contact us if you need assistance.', 'woocommerce' ), $status->name ), 'error' );
114                 }
115 
116             } else {
117                 wc_add_notice( __( 'Sorry, this order is invalid and cannot be paid for.', 'woocommerce' ), 'error' );
118             }
119 
120         } elseif ( $order_id ) {
121 
122             // Pay for order after checkout step
123             $order_key            = isset( $_GET['key'] ) ? wc_clean( $_GET['key'] ) : '';
124             $order                = new WC_Order( $order_id );
125             $valid_order_statuses = apply_filters( 'woocommerce_valid_order_statuses_for_payment', array( 'pending', 'failed' ), $order );
126 
127             if ( $order->id == $order_id && $order->order_key == $order_key ) {
128 
129                 if ( in_array( $order->status, $valid_order_statuses ) ) {
130 
131                     ?>
132                     <ul class="order_details">
133                         <li class="order">
134                             <?php _e( 'Order:', 'woocommerce' ); ?>
135                             <strong><?php echo $order->get_order_number(); ?></strong>
136                         </li>
137                         <li class="date">
138                             <?php _e( 'Date:', 'woocommerce' ); ?>
139                             <strong><?php echo date_i18n(get_option('date_format'), strtotime($order->order_date)); ?></strong>
140                         </li>
141                         <li class="total">
142                             <?php _e( 'Total:', 'woocommerce' ); ?>
143                             <strong><?php echo $order->get_formatted_order_total(); ?></strong>
144                         </li>
145                         <?php if ($order->payment_method_title) : ?>
146                         <li class="method">
147                             <?php _e( 'Payment method:', 'woocommerce' ); ?>
148                             <strong><?php
149                                 echo $order->payment_method_title;
150                             ?></strong>
151                         </li>
152                         <?php endif; ?>
153                     </ul>
154 
155                     <?php do_action( 'woocommerce_receipt_' . $order->payment_method, $order_id ); ?>
156 
157                     <div class="clear"></div>
158                     <?php
159 
160                 } else {
161 
162                     $status = get_term_by('slug', $order->status, 'shop_order_status');
163 
164                     wc_add_notice( sprintf( __( 'This order&rsquo;s status is &ldquo;%s&rdquo;&mdash;it cannot be paid for. Please contact us if you need assistance.', 'woocommerce' ), $status->name ), 'error' );
165                 }
166 
167             } else {
168                 wc_add_notice( __( 'Sorry, this order is invalid and cannot be paid for.', 'woocommerce' ), 'error' );
169             }
170 
171         } else {
172             wc_add_notice( __( 'Invalid order.', 'woocommerce' ), 'error' );
173         }
174 
175         wc_print_notices();
176 
177         do_action( 'after_woocommerce_pay' );
178     }
179 
180     /**
181      * Show the thanks page
182      */
183     private static function order_received( $order_id = 0 ) {
184 
185         wc_print_notices();
186 
187         $order = false;
188 
189         // Get the order
190         $order_id  = apply_filters( 'woocommerce_thankyou_order_id', absint( $order_id ) );
191         $order_key = apply_filters( 'woocommerce_thankyou_order_key', empty( $_GET['key'] ) ? '' : wc_clean( $_GET['key'] ) );
192 
193         if ( $order_id > 0 ) {
194             $order = new WC_Order( $order_id );
195             if ( $order->order_key != $order_key )
196                 unset( $order );
197         }
198 
199         // Empty awaiting payment session
200         unset( WC()->session->order_awaiting_payment );
201 
202         wc_get_template( 'checkout/thankyou.php', array( 'order' => $order ) );
203     }
204 
205     /**
206      * Show the checkout
207      */
208     private static function checkout() {
209 
210         // Show non-cart errors
211         wc_print_notices();
212 
213         // Check cart has contents
214         if ( sizeof( WC()->cart->get_cart() ) == 0 )
215             return;
216 
217         // Calc totals
218         WC()->cart->calculate_totals();
219 
220         // Check cart contents for errors
221         do_action('woocommerce_check_cart_items');
222 
223         // Get checkout object
224         $checkout = WC()->checkout();
225 
226         if ( empty( $_POST ) && wc_notice_count( 'error' ) > 0 ) {
227 
228             wc_get_template( 'checkout/cart-errors.php', array( 'checkout' => $checkout ) );
229 
230         } else {
231 
232             $non_js_checkout = ! empty( $_POST['woocommerce_checkout_update_totals'] ) ? true : false;
233 
234             if ( wc_notice_count( 'error' ) == 0 && $non_js_checkout )
235                 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' ) );
236 
237             wc_get_template( 'checkout/form-checkout.php', array( 'checkout' => $checkout ) );
238 
239         }
240     }
241 }
242 
WooCommerce API documentation generated by ApiGen 2.8.0