1 <?php
2 3 4 5 6 7 8 9 10 11
12
13 class WC_Shortcode_Checkout {
14
15 16 17 18 19 20 21
22 public static function get( $atts ) {
23 return WC_Shortcodes::shortcode_wrapper( array( __CLASS__, 'output' ), $atts );
24 }
25
26 27 28 29 30 31 32
33 public static function output( $atts ) {
34 global $woocommerce, $wp;
35
36
37 if ( is_null( WC()->cart ) ) {
38 return;
39 }
40
41
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
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
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 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
83 if ( isset( $_GET['pay_for_order'] ) && isset( $_GET['key'] ) && $order_id ) {
84
85
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
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’s status is “%s”—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
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’s status is “%s”—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 182
183 private static function order_received( $order_id = 0 ) {
184
185 wc_print_notices();
186
187 $order = false;
188
189
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
200 unset( WC()->session->order_awaiting_payment );
201
202 wc_get_template( 'checkout/thankyou.php', array( 'order' => $order ) );
203 }
204
205 206 207
208 private static function checkout() {
209
210
211 wc_print_notices();
212
213
214 if ( sizeof( WC()->cart->get_cart() ) == 0 )
215 return;
216
217
218 WC()->cart->calculate_totals();
219
220
221 do_action('woocommerce_check_cart_items');
222
223
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