Skip to content

Commit 02b67e6

Browse files
Merge pull request #4320 from woocommerce/dev/PCP-6285-return-cart-id
CreateCartEndpoint always returns the generated cart ID (6285)
2 parents b6d96d6 + 03ba7f0 commit 02b67e6

2 files changed

Lines changed: 23 additions & 1 deletion

File tree

modules/ppcp-store-sync/src/Response/CartResponse.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,9 @@ public function to_array(): array {
181181
$data['applied_coupons'] = $this->applied_coupons;
182182
}
183183

184-
$data = array_merge( $data, $this->cart->to_array() );
184+
$data = array_merge( $data, $this->cart->to_array() );
185+
$data['id'] = $this->cart_id;
186+
185187
$totals = $this->calculate_totals();
186188

187189
if ( $totals ) {

tests/PHPUnit/StoreSync/Response/CartResponseTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -684,6 +684,26 @@ public function test_create_new_excludes_payment_confirmation(): void {
684684
// create_completed — status and payment_confirmation
685685
// -------------------------------------------------------------------------
686686

687+
/**
688+
* @scenario Server-generated cart ID is not overwritten by a caller-supplied ID in the request
689+
* body
690+
*
691+
* Given a PayPal cart built from request body data that already contains an "id" key
692+
* When the response is built via create_new() with a distinct server-generated cart ID
693+
* Then to_array()['id'] equals the server-generated ID
694+
* And the caller-supplied ID from the request body is ignored
695+
*/
696+
public function test_create_new_id_is_not_overwritten_by_caller_supplied_id(): void {
697+
$cart_data = $this->make_cart_data();
698+
$cart_data['id'] = 'client-supplied-id';
699+
$cart = PayPalCart::from_array( $cart_data );
700+
$response = CartResponse::create_new( $cart, 'server-generated-id', 'tok_test' );
701+
702+
$result = $response->to_array();
703+
704+
$this->assertSame( 'server-generated-id', $result['id'] );
705+
}
706+
687707
/**
688708
* @scenario Completed checkout sets status to COMPLETED
689709
*

0 commit comments

Comments
 (0)