Completed
Push — master ( a91a1e...5f9786 )
by Mike
10:42
created

WC_Shortcode_Checkout::order_received()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 22
Code Lines 11

Duplication

Lines 6
Ratio 27.27 %
Metric Value
dl 6
loc 22
rs 8.9197
cc 4
eloc 11
nc 3
nop 1
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
	/**
15
	 * Get the shortcode content.
16
	 *
17
	 * @param array $atts
18
	 * @return string
19
	 */
20
	public static function get( $atts ) {
21
		return WC_Shortcodes::shortcode_wrapper( array( __CLASS__, 'output' ), $atts );
22
	}
23
24
	/**
25
	 * Output the shortcode.
26
	 *
27
	 * @param array $atts
28
	 */
29
	public static function output( $atts ) {
0 ignored issues
show
Unused Code introduced by
The parameter $atts is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
30
		global $wp;
31
32
		// Check cart class is loaded or abort
33
		if ( is_null( WC()->cart ) ) {
34
			return;
35
		}
36
37
		// Backwards compat with old pay and thanks link arguments
38
		if ( isset( $_GET['order'] ) && isset( $_GET['key'] ) ) {
39
			_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.' );
40
41
			// Get the order to work out what we are showing
42
			$order_id = absint( $_GET['order'] );
43
			$order    = wc_get_order( $order_id );
44
45
			if ( $order && $order->has_status( 'pending' ) ) {
46
				$wp->query_vars['order-pay'] = absint( $_GET['order'] );
47
			} else {
48
				$wp->query_vars['order-received'] = absint( $_GET['order'] );
49
			}
50
		}
51
52
		// Handle checkout actions
53
		if ( ! empty( $wp->query_vars['order-pay'] ) ) {
54
55
			self::order_pay( $wp->query_vars['order-pay'] );
56
57
		} elseif ( isset( $wp->query_vars['order-received'] ) ) {
58
59
			self::order_received( $wp->query_vars['order-received'] );
60
61
		} else {
62
63
			self::checkout();
64
65
		}
66
	}
67
68
	/**
69
	 * Show the pay page.
70
	 *
71
	 * @param int $order_id
72
	 */
73
	private static function order_pay( $order_id ) {
74
75
		do_action( 'before_woocommerce_pay' );
76
77
		wc_print_notices();
78
79
		$order_id = absint( $order_id );
80
81
		// Handle payment
82
		if ( isset( $_GET['pay_for_order'] ) && isset( $_GET['key'] ) && $order_id ) {
83
84
			// Pay for existing order
85
			$order_key            = $_GET[ 'key' ];
86
			$order                = wc_get_order( $order_id );
87
88 View Code Duplication
			if ( ! current_user_can( 'pay_for_order', $order_id ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
89
				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>';
90
				return;
91
			}
92
93
			if ( $order->id == $order_id && $order->order_key == $order_key ) {
94
95
				if ( $order->needs_payment() ) {
96
97
					// Set customer location to order location
98
					if ( $order->billing_country ) {
99
						WC()->customer->set_country( $order->billing_country );
100
					}
101
					if ( $order->billing_state ) {
102
						WC()->customer->set_state( $order->billing_state );
103
					}
104
					if ( $order->billing_postcode ) {
105
						WC()->customer->set_postcode( $order->billing_postcode );
106
					}
107
108
					$available_gateways = WC()->payment_gateways->get_available_payment_gateways();
109
110
					if ( sizeof( $available_gateways ) ) {
111
						current( $available_gateways )->set_current();
112
					}
113
114
					wc_get_template( 'checkout/form-pay.php', array(
115
						'order'              => $order,
116
						'available_gateways' => $available_gateways,
117
						'order_button_text'  => apply_filters( 'woocommerce_pay_order_button_text', __( 'Pay for order', 'woocommerce' ) )
118
					) );
119
120
				} else {
121
					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' ), wc_get_order_status_name( $order->get_status() ) ), 'error' );
122
				}
123
124
			} else {
125
				wc_add_notice( __( 'Sorry, this order is invalid and cannot be paid for.', 'woocommerce' ), 'error' );
126
			}
127
128
		} elseif ( $order_id ) {
129
130
			// Pay for order after checkout step
131
			$order_key            = isset( $_GET['key'] ) ? wc_clean( $_GET['key'] ) : '';
132
			$order                = wc_get_order( $order_id );
133
134
			if ( $order->id == $order_id && $order->order_key == $order_key ) {
135
136
				if ( $order->needs_payment() ) {
137
138
					?>
139
					<ul class="order_details">
140
						<li class="order">
141
							<?php _e( 'Order Number:', 'woocommerce' ); ?>
142
							<strong><?php echo $order->get_order_number(); ?></strong>
143
						</li>
144
						<li class="date">
145
							<?php _e( 'Date:', 'woocommerce' ); ?>
146
							<strong><?php echo date_i18n(get_option('date_format'), strtotime($order->order_date)); ?></strong>
147
						</li>
148
						<li class="total">
149
							<?php _e( 'Total:', 'woocommerce' ); ?>
150
							<strong><?php echo $order->get_formatted_order_total(); ?></strong>
151
						</li>
152
						<?php if ($order->payment_method_title) : ?>
153
						<li class="method">
154
							<?php _e( 'Payment Method:', 'woocommerce' ); ?>
155
							<strong><?php
156
								echo $order->payment_method_title;
157
							?></strong>
158
						</li>
159
						<?php endif; ?>
160
					</ul>
161
162
					<?php do_action( 'woocommerce_receipt_' . $order->payment_method, $order_id ); ?>
163
164
					<div class="clear"></div>
165
					<?php
166
167
				} else {
168
					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' ), wc_get_order_status_name( $order->get_status() ) ), 'error' );
169
				}
170
171
			} else {
172
				wc_add_notice( __( 'Sorry, this order is invalid and cannot be paid for.', 'woocommerce' ), 'error' );
173
			}
174
175
		} else {
176
			wc_add_notice( __( 'Invalid order.', 'woocommerce' ), 'error' );
177
		}
178
179
		wc_print_notices();
180
181
		do_action( 'after_woocommerce_pay' );
182
	}
183
184
	/**
185
	 * Show the thanks page.
186
	 *
187
	 * @param int $order_id
188
	 */
189
	private static function order_received( $order_id = 0 ) {
190
191
		wc_print_notices();
192
193
		$order = false;
194
195
		// Get the order
196
		$order_id  = apply_filters( 'woocommerce_thankyou_order_id', absint( $order_id ) );
197
		$order_key = apply_filters( 'woocommerce_thankyou_order_key', empty( $_GET['key'] ) ? '' : wc_clean( $_GET['key'] ) );
198
199 View Code Duplication
		if ( $order_id > 0 ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
200
			$order = wc_get_order( $order_id );
201
			if ( $order->order_key != $order_key ) {
202
				$order = false;
203
			}
204
		}
205
206
		// Empty awaiting payment session
207
		unset( WC()->session->order_awaiting_payment );
208
209
		wc_get_template( 'checkout/thankyou.php', array( 'order' => $order ) );
210
	}
211
212
	/**
213
	 * Show the checkout.
214
	 */
215
	private static function checkout() {
216
217
		// Show non-cart errors
218
		wc_print_notices();
219
220
		// Check cart has contents
221
		if ( WC()->cart->is_empty() ) {
222
			return;
223
		}
224
225
		// Check cart contents for errors
226
		do_action( 'woocommerce_check_cart_items' );
227
228
		// Calc totals
229
		WC()->cart->calculate_totals();
230
231
		// Get checkout object
232
		$checkout = WC()->checkout();
233
234
		if ( empty( $_POST ) && wc_notice_count( 'error' ) > 0 ) {
235
236
			wc_get_template( 'checkout/cart-errors.php', array( 'checkout' => $checkout ) );
237
238
		} else {
239
240
			$non_js_checkout = ! empty( $_POST['woocommerce_checkout_update_totals'] ) ? true : false;
241
242
			if ( wc_notice_count( 'error' ) == 0 && $non_js_checkout )
243
				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' ) );
244
245
			wc_get_template( 'checkout/form-checkout.php', array( 'checkout' => $checkout ) );
246
247
		}
248
	}
249
}
250