Skip to content

Commit ca37a96

Browse files
Merge pull request #4311 from woocommerce/dev/PCP-6272-always-add-currency
Always add currency to cart (6272)
2 parents 6fbaf62 + e736ad8 commit ca37a96

5 files changed

Lines changed: 67 additions & 11 deletions

File tree

modules/ppcp-store-sync/services.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
use WooCommerce\PayPalCommerce\StoreSync\Config\AgenticWebhookConfiguration;
2020
use WooCommerce\PayPalCommerce\StoreSync\Config\IngestionConfiguration;
21+
use WooCommerce\PayPalCommerce\StoreSync\Config\StoreCurrencyValue;
2122
use WooCommerce\PayPalCommerce\StoreSync\Auth\AuthServiceProvider;
2223
use WooCommerce\PayPalCommerce\StoreSync\Auth\PayPalJwkProvider;
2324
use WooCommerce\PayPalCommerce\StoreSync\Endpoint\CreateCartEndpoint;
@@ -85,6 +86,9 @@
8586
'agentic.config.ingestion' => static function (): IngestionConfiguration {
8687
return new IngestionConfiguration();
8788
},
89+
'agentic.config.store-currency' => static function (): StoreCurrencyValue {
90+
return new StoreCurrencyValue();
91+
},
8892

8993
// Registration and merchant identification.
9094
'agentic.merchant.provider' => static function ( ContainerInterface $c ): MerchantMetadataProvider {
@@ -226,7 +230,8 @@
226230
return new ResponseFactory(
227231
$c->get( 'agentic.helper.cart-builder' ),
228232
$c->get( 'agentic.response.applied-coupons-builder' ),
229-
$c->get( 'agentic.helper.shipping-options-builder' )
233+
$c->get( 'agentic.helper.shipping-options-builder' ),
234+
$c->get( 'agentic.config.store-currency' )
230235
);
231236
},
232237

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace WooCommerce\PayPalCommerce\StoreSync\Config;
4+
5+
class StoreCurrencyValue {
6+
public function value(): string {
7+
return get_woocommerce_currency();
8+
}
9+
}

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

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use WooCommerce\PayPalCommerce\StoreSync\Validation\ValidationIssue;
1717
use WooCommerce\PayPalCommerce\StoreSync\Helper\CartHelper;
1818
use WooCommerce\PayPalCommerce\StoreSync\Enums\ErrorCode;
19+
use WooCommerce\PayPalCommerce\StoreSync\Config\StoreCurrencyValue;
1920

2021
class CartResponse {
2122
private const ALLOWED_STATUS = array(
@@ -33,6 +34,8 @@ class CartResponse {
3334

3435
private PayPalCart $cart;
3536

37+
private string $default_currency = '';
38+
3639
private ?WC_Cart $wc_cart = null;
3740

3841
private array $applied_coupons = array();
@@ -160,6 +163,18 @@ public function wc_cart( ?WC_Cart $wc_cart ): self {
160163
return $this;
161164
}
162165

166+
/**
167+
* Configures the CartResponse instance - only used by the ResponseFactory.
168+
*
169+
* @param StoreCurrencyValue $store_currency Resolves the WooCommerce currency code.
170+
* @return $this
171+
*/
172+
public function store_currency( StoreCurrencyValue $store_currency ): self {
173+
$this->default_currency = $store_currency->value();
174+
175+
return $this;
176+
}
177+
163178
/**
164179
* Convert to array for API response.
165180
*
@@ -218,9 +233,11 @@ private function calculate_totals(): ?array {
218233
return null;
219234
}
220235

221-
$currency_code = CartHelper::currency( $this->cart );
236+
return CartHelper::calculate_totals( $this->wc_cart, $this->currency_code() );
237+
}
222238

223-
return CartHelper::calculate_totals( $this->wc_cart, $currency_code );
239+
private function currency_code(): string {
240+
return CartHelper::currency( $this->cart, $this->default_currency );
224241
}
225242

226243
private function status(): string {

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,28 +15,25 @@
1515
use WooCommerce\PayPalCommerce\StoreSync\Helper\AgenticCartBuilder;
1616
use WooCommerce\PayPalCommerce\StoreSync\Helper\ShippingOptionsBuilder;
1717
use WooCommerce\PayPalCommerce\StoreSync\Schema\PayPalCart;
18+
use WooCommerce\PayPalCommerce\StoreSync\Config\StoreCurrencyValue;
1819

1920
class ResponseFactory {
2021

2122
private AgenticCartBuilder $cart_builder;
2223
private AppliedCouponsBuilder $applied_coupons_builder;
2324
private ShippingOptionsBuilder $shipping_options_builder;
25+
private StoreCurrencyValue $store_currency;
2426

25-
/**
26-
* Constructor.
27-
*
28-
* @param AgenticCartBuilder $cart_builder Cart builder service.
29-
* @param AppliedCouponsBuilder $applied_coupons_builder Applied coupons builder service.
30-
* @param ShippingOptionsBuilder $shipping_options_builder Shipping options builder service.
31-
*/
3227
public function __construct(
3328
AgenticCartBuilder $cart_builder,
3429
AppliedCouponsBuilder $applied_coupons_builder,
35-
ShippingOptionsBuilder $shipping_options_builder
30+
ShippingOptionsBuilder $shipping_options_builder,
31+
StoreCurrencyValue $store_currency
3632
) {
3733
$this->cart_builder = $cart_builder;
3834
$this->applied_coupons_builder = $applied_coupons_builder;
3935
$this->shipping_options_builder = $shipping_options_builder;
36+
$this->store_currency = $store_currency;
4037
}
4138

4239
/**
@@ -52,6 +49,7 @@ public function new_cart( PayPalCart $cart, string $cart_id, string $token ): Ca
5249

5350
return CartResponse::create_new( $cart, $cart_id, $token )
5451
->wc_cart( $wc_cart )
52+
->store_currency( $this->store_currency )
5553
->applied_coupons( $this->build_applied_coupons( $cart ) )
5654
->shipping_options( $this->shipping_options_builder->build( $wc_cart ) );
5755
}
@@ -69,6 +67,7 @@ public function from_order( WC_Order $order, PayPalCart $cart, string $cart_id )
6967

7068
return CartResponse::create_completed( $cart, $cart_id, $order )
7169
->wc_cart( $wc_cart )
70+
->store_currency( $this->store_currency )
7271
->applied_coupons( $this->build_applied_coupons( $cart ) )
7372
->shipping_options( $this->shipping_options_builder->build( $wc_cart ) );
7473
}
@@ -85,6 +84,7 @@ public function from_cart( PayPalCart $cart, string $cart_id ): CartResponse {
8584

8685
return CartResponse::create( $cart, $cart_id )
8786
->wc_cart( $wc_cart )
87+
->store_currency( $this->store_currency )
8888
->applied_coupons( $this->build_applied_coupons( $cart ) )
8989
->shipping_options( $this->shipping_options_builder->build( $wc_cart ) );
9090
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
declare( strict_types = 1 );
3+
4+
namespace WooCommerce\PayPalCommerce\StoreSync\Config;
5+
6+
use WooCommerce\PayPalCommerce\TestCase;
7+
use function Brain\Monkey\Functions\when;
8+
9+
/**
10+
* @covers \WooCommerce\PayPalCommerce\StoreSync\Config\StoreCurrencyValue
11+
*/
12+
class StoreCurrencyValueTest extends TestCase {
13+
/**
14+
* GIVEN the WooCommerce store is configured with USD as the active currency
15+
* WHEN value() is called
16+
* THEN it returns the string 'USD'
17+
*/
18+
public function test_value_returns_woocommerce_currency_code(): void {
19+
when( 'get_woocommerce_currency' )->justReturn( 'USD' );
20+
21+
$testee = new StoreCurrencyValue();
22+
23+
$this->assertSame( 'USD', $testee->value() );
24+
}
25+
}

0 commit comments

Comments
 (0)