Add hash check when resuming order to detect changes.
@claudiosmweb
This commit is contained in:
parent
a78edb2af2
commit
a71a4de1b8
|
@ -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 );
|
||||
|
|
|
@ -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'] ) ) {
|
||||
|
|
Loading…
Reference in New Issue