From f6f98204c1c6b91dda546b6650cdf372ab003330 Mon Sep 17 00:00:00 2001
From: Philipp Stracker
Date: Mon, 26 Aug 2024 13:37:47 +0200
Subject: [PATCH 1/2] =?UTF-8?q?=F0=9F=90=9B=20Fix=20fatal=20error=20in=20P?=
=?UTF-8?q?ayPalPaymentMethod=20class?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
modules/ppcp-blocks/services.php | 11 ++++++++++-
modules/ppcp-blocks/src/PayPalPaymentMethod.php | 14 ++++++++++++--
2 files changed, 22 insertions(+), 3 deletions(-)
diff --git a/modules/ppcp-blocks/services.php b/modules/ppcp-blocks/services.php
index 21a97f984..4f2bba04c 100644
--- a/modules/ppcp-blocks/services.php
+++ b/modules/ppcp-blocks/services.php
@@ -13,6 +13,7 @@ use WooCommerce\PayPalCommerce\Blocks\Endpoint\GetPayPalOrderFromSession;
use WooCommerce\PayPalCommerce\Blocks\Endpoint\UpdateShippingEndpoint;
use WooCommerce\PayPalCommerce\Vendor\Psr\Container\ContainerInterface;
use WooCommerce\PayPalCommerce\Button\Assets\SmartButtonInterface;
+use WC_Cart;
return array(
'blocks.url' => static function ( ContainerInterface $container ): string {
@@ -27,6 +28,13 @@ return array(
);
},
'blocks.method' => static function ( ContainerInterface $container ): PayPalPaymentMethod {
+ /**
+ * Cart instance; might be null, esp. in customizer or in Block Editor.
+ *
+ * @var null|WC_Cart $cart
+ */
+ $cart = WC()->cart;
+
return new PayPalPaymentMethod(
$container->get( 'blocks.url' ),
$container->get( 'ppcp.asset-version' ),
@@ -43,7 +51,8 @@ return array(
$container->get( 'wcgateway.use-place-order-button' ),
$container->get( 'wcgateway.place-order-button-text' ),
$container->get( 'wcgateway.place-order-button-description' ),
- $container->get( 'wcgateway.all-funding-sources' )
+ $container->get( 'wcgateway.all-funding-sources' ),
+ $cart && $cart->needs_shipping()
);
},
'blocks.advanced-card-method' => static function( ContainerInterface $container ): AdvancedCardPaymentMethod {
diff --git a/modules/ppcp-blocks/src/PayPalPaymentMethod.php b/modules/ppcp-blocks/src/PayPalPaymentMethod.php
index 1d0654bac..50340b574 100644
--- a/modules/ppcp-blocks/src/PayPalPaymentMethod.php
+++ b/modules/ppcp-blocks/src/PayPalPaymentMethod.php
@@ -122,6 +122,13 @@ class PayPalPaymentMethod extends AbstractPaymentMethodType {
*/
private $all_funding_sources;
+ /**
+ * Whether shipping details must be collected during checkout; i.e. paying for physical goods?
+ *
+ * @var bool
+ */
+ private $need_shipping;
+
/**
* Assets constructor.
*
@@ -139,6 +146,7 @@ class PayPalPaymentMethod extends AbstractPaymentMethodType {
* @param string $place_order_button_text The text for the standard "Place order" button.
* @param string $place_order_button_description The text for additional "Place order" description.
* @param array $all_funding_sources All existing funding sources for PayPal buttons.
+ * @param bool $need_shipping Whether shipping details are required for the purchase.
*/
public function __construct(
string $module_url,
@@ -154,7 +162,8 @@ class PayPalPaymentMethod extends AbstractPaymentMethodType {
bool $use_place_order,
string $place_order_button_text,
string $place_order_button_description,
- array $all_funding_sources
+ array $all_funding_sources,
+ bool $need_shipping
) {
$this->name = PayPalGateway::ID;
$this->module_url = $module_url;
@@ -171,6 +180,7 @@ class PayPalPaymentMethod extends AbstractPaymentMethodType {
$this->place_order_button_text = $place_order_button_text;
$this->place_order_button_description = $place_order_button_description;
$this->all_funding_sources = $all_funding_sources;
+ $this->need_shipping = $need_shipping;
}
/**
@@ -254,7 +264,7 @@ class PayPalPaymentMethod extends AbstractPaymentMethodType {
),
),
'scriptData' => $script_data,
- 'needShipping' => WC()->cart->needs_shipping(),
+ 'needShipping' => $this->need_shipping,
);
}
From f5792b1719a28ce27a671e145d3086e7ae87aaec Mon Sep 17 00:00:00 2001
From: Philipp Stracker
Date: Mon, 26 Aug 2024 13:38:45 +0200
Subject: [PATCH 2/2] =?UTF-8?q?=F0=9F=90=9B=20Fix=20fatal=20error=20in=20S?=
=?UTF-8?q?martButton=20class?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../ppcp-button/src/Assets/SmartButton.php | 20 ++++++++++++++++++-
1 file changed, 19 insertions(+), 1 deletion(-)
diff --git a/modules/ppcp-button/src/Assets/SmartButton.php b/modules/ppcp-button/src/Assets/SmartButton.php
index e5fc8cfc6..86a4ae448 100644
--- a/modules/ppcp-button/src/Assets/SmartButton.php
+++ b/modules/ppcp-button/src/Assets/SmartButton.php
@@ -51,6 +51,8 @@ use WooCommerce\PayPalCommerce\WcGateway\Gateway\CreditCardGateway;
use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayPalGateway;
use WooCommerce\PayPalCommerce\WcGateway\Helper\SettingsStatus;
use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings;
+use WC_Shipping_Method;
+use WC_Cart;
/**
* Class SmartButton
@@ -1085,6 +1087,22 @@ document.querySelector("#payment").before(document.querySelector(".ppcp-messages
return apply_filters( 'woocommerce_paypal_payments_three_d_secure_contingency', $contingency );
}
+ /**
+ * Whether the current cart contains a product that requires physical shipping.
+ *
+ * @return bool True, if any cart item requires shipping.
+ */
+ private function need_shipping() : bool {
+ /**
+ * Cart instance; might be null, esp. in customizer or in Block Editor.
+ *
+ * @var null|WC_Cart $cart
+ */
+ $cart = WC()->cart;
+
+ return $cart && $cart->needs_shipping();
+ }
+
/**
* The configuration for the smart buttons.
*
@@ -1297,7 +1315,7 @@ document.querySelector("#payment").before(document.querySelector(".ppcp-messages
'has_wc_card_payment_tokens' => $this->user_has_wc_card_payment_tokens( get_current_user_id() ),
),
'should_handle_shipping_in_paypal' => $this->should_handle_shipping_in_paypal && ! $this->is_checkout(),
- 'needShipping' => WC()->cart->needs_shipping(),
+ 'needShipping' => $this->need_shipping(),
'vaultingEnabled' => $this->settings->has( 'vault_enabled' ) && $this->settings->get( 'vault_enabled' ),
);