From a71a4de1b899eac899d6862f552be8781ec4c0e5 Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Fri, 5 Feb 2016 09:41:25 +0000 Subject: [PATCH] Add hash check when resuming order to detect changes. @claudiosmweb --- includes/class-wc-checkout.php | 10 ++++++++-- includes/wc-core-functions.php | 4 +++- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/includes/class-wc-checkout.php b/includes/class-wc-checkout.php index 90f3210381c..51fb97bdee1 100644 --- a/includes/class-wc-checkout.php +++ b/includes/class-wc-checkout.php @@ -184,14 +184,20 @@ class WC_Checkout { 'status' => apply_filters( 'woocommerce_default_order_status', 'pending' ), 'customer_id' => $this->customer_id, 'customer_note' => isset( $this->posted['order_comments'] ) ? $this->posted['order_comments'] : '', + 'cart_hash' => md5( json_encode( WC()->cart->get_cart_for_session() ) . WC()->cart->total ), 'created_via' => 'checkout' ); // Insert or update the post data $order_id = absint( WC()->session->order_awaiting_payment ); - // Resume the unpaid order if its pending - if ( $order_id > 0 && ( $order = wc_get_order( $order_id ) ) && $order->has_status( array( 'pending', 'failed' ) ) ) { + /** + * If there is an order pending payment, we can resume it here so + * long as it has not changed. If the order has changed, i.e. + * different items or cost, create a new order. We use a hash to + * detect changes which is based on cart items + order total. + */ + if ( $order_id && $order_data['cart_hash'] === get_post_meta( $order_id, '_cart_hash', true ) && ( $order = wc_get_order( $order_id ) ) && $order->has_status( array( 'pending', 'failed' ) ) ) { $order_data['order_id'] = $order_id; $order = wc_update_order( $order_data ); diff --git a/includes/wc-core-functions.php b/includes/wc-core-functions.php index b4a3a8c4212..ade6c76a49f 100644 --- a/includes/wc-core-functions.php +++ b/includes/wc-core-functions.php @@ -62,7 +62,8 @@ function wc_create_order( $args = array() ) { 'customer_note' => null, 'order_id' => 0, 'created_via' => '', - 'parent' => 0 + 'cart_hash' => '', + 'parent' => 0, ); $args = wp_parse_args( $args, $default_args ); @@ -111,6 +112,7 @@ function wc_create_order( $args = array() ) { update_post_meta( $order_id, '_customer_user_agent', isset( $_SERVER['HTTP_USER_AGENT'] ) ? $_SERVER['HTTP_USER_AGENT'] : '' ); update_post_meta( $order_id, '_customer_user', 0 ); update_post_meta( $order_id, '_created_via', sanitize_text_field( $args['created_via'] ) ); + update_post_meta( $order_id, '_cart_hash', sanitize_text_field( $args['cart_hash'] ) ); } if ( is_numeric( $args['customer_id'] ) ) {