From 33ea69d7c8f4a494ddd3379bd79f3a6ac12b6070 Mon Sep 17 00:00:00 2001
From: Pedro Silva
Date: Wed, 30 Aug 2023 08:37:09 +0100
Subject: [PATCH] Add CartScriptParams returning currency and country code.
---
.../src/Endpoint/CartScriptParamsEndpoint.php | 13 +++++++++++++
.../src/Endpoint/SimulateCartEndpoint.php | 1 +
2 files changed, 14 insertions(+)
diff --git a/modules/ppcp-button/src/Endpoint/CartScriptParamsEndpoint.php b/modules/ppcp-button/src/Endpoint/CartScriptParamsEndpoint.php
index 58350f6f8..971f678cb 100644
--- a/modules/ppcp-button/src/Endpoint/CartScriptParamsEndpoint.php
+++ b/modules/ppcp-button/src/Endpoint/CartScriptParamsEndpoint.php
@@ -11,6 +11,7 @@ namespace WooCommerce\PayPalCommerce\Button\Endpoint;
use Psr\Log\LoggerInterface;
use Throwable;
+use WooCommerce\PayPalCommerce\ApiClient\Entity\Money;
use WooCommerce\PayPalCommerce\Button\Assets\SmartButton;
/**
@@ -71,12 +72,24 @@ class CartScriptParamsEndpoint implements EndpointInterface {
$script_data = $this->smart_button->script_data();
+ $total = (float) WC()->cart->get_total( 'numeric' );
+
+ // Shop settings.
+ $base_location = wc_get_base_location();
+ $shop_country_code = $base_location['country'];
+ $currency_code = get_woocommerce_currency();
+
wp_send_json_success(
array(
'url_params' => $script_data['url_params'],
'button' => $script_data['button'],
'messages' => $script_data['messages'],
'amount' => WC()->cart->get_total( 'raw' ),
+
+ 'total' => $total,
+ 'total_str' => ( new Money( $total, $currency_code ) )->value_str(),
+ 'currency_code' => $currency_code,
+ 'country_code' => $shop_country_code,
)
);
diff --git a/modules/ppcp-button/src/Endpoint/SimulateCartEndpoint.php b/modules/ppcp-button/src/Endpoint/SimulateCartEndpoint.php
index 9846e2775..ba23a403d 100644
--- a/modules/ppcp-button/src/Endpoint/SimulateCartEndpoint.php
+++ b/modules/ppcp-button/src/Endpoint/SimulateCartEndpoint.php
@@ -101,6 +101,7 @@ class SimulateCartEndpoint extends AbstractCartEndpoint {
$button_enabled = $button_enabled && ! $this->smart_button->is_button_disabled( 'product', $context_data );
}
+ // Shop settings.
$base_location = wc_get_base_location();
$shop_country_code = $base_location['country'];
$currency_code = get_woocommerce_currency();