Skip to content

Commit

Permalink
Use print_r() to stringify cart session
Browse files Browse the repository at this point in the history
Instead of json_encode(), because json_encode() takes types into
account, meaning numbers may end up being represented in the
encoded JSON as a string, e.g. "1" instead of 1, which can lead
to the md5() being different between requests.

This issue surfaces when creating a cart hash in a different
request to the request to pay for the order, because after adding
line items to the cart, the items quantity will be encoded with
json_encode() as an int, but when encoding them in a different
request, where they are coming from the database, they will be
encoded as a string.
  • Loading branch information
thenbrent committed May 17, 2016
1 parent a7b9528 commit 671ae32
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion includes/class-wc-checkout.php
Expand Up @@ -184,7 +184,7 @@ public function create_order() {
'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 ),
'cart_hash' => md5( print_r( WC()->cart->get_cart_for_session(), true ) . WC()->cart->total ),
'created_via' => 'checkout'
);

Expand Down

0 comments on commit 671ae32

Please sign in to comment.