2012-12-31 18:25:09 +00:00
< ? php
/**
2015-11-03 13:53:50 +00:00
* Checkout Shortcode
2012-12-31 18:25:09 +00:00
*
* Used on the checkout page , the checkout shortcode displays the checkout process .
*
* @ author WooThemes
* @ category Shortcodes
* @ package WooCommerce / Shortcodes / Checkout
* @ version 2.0 . 0
*/
class WC_Shortcode_Checkout {
/**
* Get the shortcode content .
*
* @ param array $atts
* @ return string
*/
public static function get ( $atts ) {
2013-08-09 16:11:15 +00:00
return WC_Shortcodes :: shortcode_wrapper ( array ( __CLASS__ , 'output' ), $atts );
2012-12-31 18:25:09 +00:00
}
/**
* Output the shortcode .
*
* @ param array $atts
*/
public static function output ( $atts ) {
2014-06-08 20:33:11 +00:00
global $wp ;
2013-05-31 15:13:14 +00:00
2014-02-26 15:27:26 +00:00
// Check cart class is loaded or abort
if ( is_null ( WC () -> cart ) ) {
return ;
}
2013-05-31 15:13:14 +00:00
// Backwards compat with old pay and thanks link arguments
if ( isset ( $_GET [ 'order' ] ) && isset ( $_GET [ 'key' ] ) ) {
2016-11-23 16:15:00 +00:00
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.' );
2013-05-31 15:13:14 +00:00
// Get the order to work out what we are showing
2015-08-15 20:50:42 +00:00
$order_id = absint ( $_GET [ 'order' ] );
$order = wc_get_order ( $order_id );
2013-05-31 15:13:14 +00:00
2015-08-15 20:50:42 +00:00
if ( $order && $order -> has_status ( 'pending' ) ) {
2013-05-31 15:13:14 +00:00
$wp -> query_vars [ 'order-pay' ] = absint ( $_GET [ 'order' ] );
2014-05-30 16:43:21 +00:00
} else {
2013-05-31 15:13:14 +00:00
$wp -> query_vars [ 'order-received' ] = absint ( $_GET [ 'order' ] );
2014-05-30 16:43:21 +00:00
}
2013-05-31 15:13:14 +00:00
}
// Handle checkout actions
if ( ! empty ( $wp -> query_vars [ 'order-pay' ] ) ) {
self :: order_pay ( $wp -> query_vars [ 'order-pay' ] );
} elseif ( isset ( $wp -> query_vars [ 'order-received' ] ) ) {
self :: order_received ( $wp -> query_vars [ 'order-received' ] );
} else {
self :: checkout ();
}
}
/**
2015-11-03 13:31:20 +00:00
* Show the pay page .
2015-08-15 20:50:42 +00:00
*
* @ param int $order_id
2013-05-31 15:13:14 +00:00
*/
2013-08-15 09:22:38 +00:00
private static function order_pay ( $order_id ) {
2012-12-31 18:25:09 +00:00
2013-05-31 15:13:14 +00:00
do_action ( 'before_woocommerce_pay' );
2013-11-13 04:34:55 +00:00
wc_print_notices ();
2013-05-31 15:13:14 +00:00
$order_id = absint ( $order_id );
// Handle payment
if ( isset ( $_GET [ 'pay_for_order' ] ) && isset ( $_GET [ 'key' ] ) && $order_id ) {
// Pay for existing order
2016-08-27 03:23:21 +00:00
$order_key = $_GET [ 'key' ];
2014-08-15 12:29:21 +00:00
$order = wc_get_order ( $order_id );
2015-10-06 11:33:45 +00:00
2014-02-26 11:25:32 +00:00
if ( ! current_user_can ( 'pay_for_order' , $order_id ) ) {
2016-10-12 10:16:30 +00:00
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>' ;
2013-09-06 14:39:45 +00:00
return ;
}
2016-08-09 13:15:04 +00:00
if ( $order -> get_id () == $order_id && $order -> get_order_key () == $order_key ) {
2013-05-31 15:13:14 +00:00
2015-09-05 12:06:57 +00:00
if ( $order -> needs_payment () ) {
2016-08-30 16:59:41 +00:00
WC () -> customer -> set_props ( array (
2016-08-30 17:03:00 +00:00
'billing_country' => $order -> get_billing_country () ? $order -> get_billing_country () : null ,
'billing_state' => $order -> get_billing_state () ? $order -> get_billing_state () : null ,
'billing_postcode' => $order -> get_billing_postcode () ? $order -> get_billing_postcode () : null ,
2016-08-30 16:59:41 +00:00
) );
2016-03-09 20:49:02 +00:00
WC () -> customer -> save ();
2015-10-06 11:33:45 +00:00
$available_gateways = WC () -> payment_gateways -> get_available_payment_gateways ();
2013-05-31 15:13:14 +00:00
2015-10-06 11:33:45 +00:00
if ( sizeof ( $available_gateways ) ) {
current ( $available_gateways ) -> set_current ();
}
wc_get_template ( 'checkout/form-pay.php' , array (
'order' => $order ,
'available_gateways' => $available_gateways ,
2016-08-27 01:46:45 +00:00
'order_button_text' => apply_filters ( 'woocommerce_pay_order_button_text' , __ ( 'Pay for order' , 'woocommerce' ) ),
2015-10-06 11:33:45 +00:00
) );
2013-05-31 15:13:14 +00:00
2013-07-19 12:30:27 +00:00
} else {
2014-05-30 16:43:21 +00:00
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' );
2013-07-19 12:30:27 +00:00
}
} else {
2013-11-13 04:29:03 +00:00
wc_add_notice ( __ ( 'Sorry, this order is invalid and cannot be paid for.' , 'woocommerce' ), 'error' );
2013-05-31 15:13:14 +00:00
}
} elseif ( $order_id ) {
2013-04-23 15:42:34 +00:00
2013-05-31 15:13:14 +00:00
// Pay for order after checkout step
2013-11-25 13:34:21 +00:00
$order_key = isset ( $_GET [ 'key' ] ) ? wc_clean ( $_GET [ 'key' ] ) : '' ;
2014-08-15 12:29:21 +00:00
$order = wc_get_order ( $order_id );
2015-10-06 11:33:45 +00:00
2016-08-09 13:15:04 +00:00
if ( $order -> get_id () == $order_id && $order -> get_order_key () == $order_key ) {
2013-07-19 12:30:27 +00:00
2015-09-05 12:06:57 +00:00
if ( $order -> needs_payment () ) {
2013-07-19 12:30:27 +00:00
?>
< ul class = " order_details " >
< li class = " order " >
2016-10-12 10:16:30 +00:00
< ? php _e ( 'Order number:' , 'woocommerce' ); ?>
2013-07-19 12:30:27 +00:00
< strong >< ? php echo $order -> get_order_number (); ?> </strong>
</ li >
< li class = " date " >
< ? php _e ( 'Date:' , 'woocommerce' ); ?>
2016-09-02 01:51:31 +00:00
< strong >< ? php echo date_i18n ( get_option ( 'date_format' ), strtotime ( $order -> order_date ) ); ?> </strong>
2013-07-19 12:30:27 +00:00
</ li >
< li class = " total " >
< ? php _e ( 'Total:' , 'woocommerce' ); ?>
< strong >< ? php echo $order -> get_formatted_order_total (); ?> </strong>
</ li >
2016-08-27 04:38:29 +00:00
< ? php if ( $order -> get_payment_method_title () ) : ?>
2013-07-19 12:30:27 +00:00
< li class = " method " >
2016-10-12 10:16:30 +00:00
< ? php _e ( 'Payment method:' , 'woocommerce' ); ?>
2013-07-19 12:30:27 +00:00
< strong >< ? php
2016-08-05 14:59:55 +00:00
echo $order -> get_payment_method_title ();
2013-07-19 12:30:27 +00:00
?> </strong>
</ li >
< ? php endif ; ?>
</ ul >
2016-08-05 15:04:41 +00:00
< ? php do_action ( 'woocommerce_receipt_' . $order -> get_payment_method (), $order_id ); ?>
2013-07-19 12:30:27 +00:00
< div class = " clear " ></ div >
< ? php
} else {
2014-05-30 16:43:21 +00:00
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' );
2013-07-19 12:30:27 +00:00
}
} else {
2013-11-13 04:29:03 +00:00
wc_add_notice ( __ ( 'Sorry, this order is invalid and cannot be paid for.' , 'woocommerce' ), 'error' );
2013-05-31 15:13:14 +00:00
}
} else {
2013-11-13 04:29:03 +00:00
wc_add_notice ( __ ( 'Invalid order.' , 'woocommerce' ), 'error' );
2013-05-31 15:13:14 +00:00
}
2013-11-13 04:34:55 +00:00
wc_print_notices ();
2013-07-19 12:30:27 +00:00
2013-05-31 15:13:14 +00:00
do_action ( 'after_woocommerce_pay' );
}
/**
2015-11-03 13:31:20 +00:00
* Show the thanks page .
2015-08-15 20:50:42 +00:00
*
* @ param int $order_id
2013-05-31 15:13:14 +00:00
*/
2013-08-15 09:22:38 +00:00
private static function order_received ( $order_id = 0 ) {
2013-05-31 15:13:14 +00:00
2013-11-13 04:34:55 +00:00
wc_print_notices ();
2013-05-31 15:13:14 +00:00
$order = false ;
// Get the order
$order_id = apply_filters ( 'woocommerce_thankyou_order_id' , absint ( $order_id ) );
2013-11-25 13:34:21 +00:00
$order_key = apply_filters ( 'woocommerce_thankyou_order_key' , empty ( $_GET [ 'key' ] ) ? '' : wc_clean ( $_GET [ 'key' ] ) );
2013-05-31 15:13:14 +00:00
if ( $order_id > 0 ) {
2014-08-15 12:29:21 +00:00
$order = wc_get_order ( $order_id );
2016-08-09 13:15:04 +00:00
if ( $order -> get_order_key () != $order_key ) {
2016-02-15 15:11:04 +00:00
$order = false ;
}
2013-04-23 15:42:34 +00:00
}
2013-05-31 15:13:14 +00:00
// Empty awaiting payment session
2013-11-25 14:01:32 +00:00
unset ( WC () -> session -> order_awaiting_payment );
2013-05-31 15:13:14 +00:00
2016-06-09 09:38:55 +00:00
// Empty current cart
wc_empty_cart ();
2013-11-25 12:45:04 +00:00
wc_get_template ( 'checkout/thankyou.php' , array ( 'order' => $order ) );
2013-05-31 15:13:14 +00:00
}
/**
2015-11-03 13:31:20 +00:00
* Show the checkout .
2013-05-31 15:13:14 +00:00
*/
2013-08-15 09:22:38 +00:00
private static function checkout () {
2013-05-31 15:13:14 +00:00
2012-12-31 18:25:09 +00:00
// Show non-cart errors
2013-11-13 04:34:55 +00:00
wc_print_notices ();
2012-12-31 18:25:09 +00:00
// Check cart has contents
2015-05-14 21:18:53 +00:00
if ( WC () -> cart -> is_empty () ) {
2013-04-23 15:42:34 +00:00
return ;
2014-11-12 15:47:27 +00:00
}
// Check cart contents for errors
do_action ( 'woocommerce_check_cart_items' );
2012-12-31 18:25:09 +00:00
// Calc totals
2013-11-25 14:01:32 +00:00
WC () -> cart -> calculate_totals ();
2012-12-31 18:25:09 +00:00
// Get checkout object
2013-11-25 14:01:32 +00:00
$checkout = WC () -> checkout ();
2012-12-31 18:25:09 +00:00
2013-11-27 16:15:53 +00:00
if ( empty ( $_POST ) && wc_notice_count ( 'error' ) > 0 ) {
2012-12-31 18:25:09 +00:00
2013-11-25 12:45:04 +00:00
wc_get_template ( 'checkout/cart-errors.php' , array ( 'checkout' => $checkout ) );
2012-12-31 18:25:09 +00:00
} else {
$non_js_checkout = ! empty ( $_POST [ 'woocommerce_checkout_update_totals' ] ) ? true : false ;
2013-11-27 16:15:53 +00:00
if ( wc_notice_count ( 'error' ) == 0 && $non_js_checkout )
2016-10-12 10:16:30 +00:00
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' ) );
2012-12-31 18:25:09 +00:00
2013-11-25 12:45:04 +00:00
wc_get_template ( 'checkout/form-checkout.php' , array ( 'checkout' => $checkout ) );
2012-12-31 18:25:09 +00:00
}
}
2014-01-26 09:19:17 +00:00
}