Merge pull request #3553 from woocommerce/PCP-4976-new-ui-on-goole-pay-popup-no-shipping-methods-visible-when-pay-now-is-enabled

New UI - On Goole Pay popup no shipping methods visible when Pay Now is enabled (4976)
This commit is contained in:
Emili Castells 2025-08-19 09:16:39 +02:00 committed by GitHub
commit 184b5d1caa
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 73 additions and 5 deletions

View file

@ -99,7 +99,7 @@ export const handleApprove = async (
order = json.data;
}
const shippingAddress = order.purchase_units?.[ 0 ]?.shipping?.address;
const shippingAddress = order?.purchase_units?.[ 0 ]?.shipping?.address;
if ( shippingAddress ) {
const addresses = paypalOrderToWcAddresses( order );

View file

@ -102,4 +102,34 @@ class ChangeCartEndpoint extends AbstractCartEndpoint {
$pu = $this->purchase_unit_factory->from_wc_cart();
return array( $pu->to_array() );
}
/**
* Adds products to cart with shipping data preservation.
*
* @param array $products Array of products to be added to cart.
* @return bool
* @throws Exception Add to cart methods throw an exception on fail.
*/
protected function add_products( array $products ): bool {
// Preserve shipping data before emptying cart.
$chosen_shipping_methods = WC()->session->get( 'chosen_shipping_methods' );
$this->cart->empty_cart( false );
try {
$this->cart_products->add_products( $products );
if ( $chosen_shipping_methods ) {
WC()->session->set( 'chosen_shipping_methods', $chosen_shipping_methods );
$this->cart->calculate_shipping();
$this->cart->calculate_fees();
$this->cart->calculate_totals();
}
} catch ( Exception $e ) {
$this->handle_error();
}
return true;
}
}

View file

@ -190,7 +190,8 @@ return array(
$container->get( 'wcgateway.settings' ),
$container->get( 'settings.environment' ),
$container->get( 'wcgateway.settings.status' ),
$container->get( 'woocommerce.logger.woocommerce' )
$container->get( 'woocommerce.logger.woocommerce' ),
$container->has( 'settings.data.settings' ) ? $container->get( 'settings.data.settings' ) : null
);
},

View file

@ -16,6 +16,7 @@ use WooCommerce\PayPalCommerce\Button\Assets\ButtonInterface;
use WooCommerce\PayPalCommerce\Button\Helper\ContextTrait;
use WooCommerce\PayPalCommerce\Googlepay\Endpoint\UpdatePaymentDataEndpoint;
use WooCommerce\PayPalCommerce\Googlepay\GooglePayGateway;
use WooCommerce\PayPalCommerce\Settings\Data\SettingsModel;
use WooCommerce\PayPalCommerce\WcGateway\Helper\Environment;
use WooCommerce\PayPalCommerce\Session\SessionHandler;
use WooCommerce\PayPalCommerce\WcGateway\Exception\NotFoundException;
@ -93,6 +94,11 @@ class Button implements ButtonInterface {
*/
private $subscription_helper;
/**
* @var SettingsModel|null New settings UI model.
*/
private ?SettingsModel $new_settings;
/**
* SmartButton constructor.
*
@ -101,10 +107,11 @@ class Button implements ButtonInterface {
* @param string $version The assets version.
* @param SessionHandler $session_handler The Session handler.
* @param SubscriptionHelper $subscription_helper The subscription helper.
* @param Settings $settings The Settings.
* @param Settings $settings The legacy settings.
* @param Environment $environment The environment object.
* @param SettingsStatus $settings_status The Settings status helper.
* @param LoggerInterface $logger The logger.
* @param SettingsModel|null $new_settings The new settings model.
*/
public function __construct(
string $module_url,
@ -115,7 +122,8 @@ class Button implements ButtonInterface {
Settings $settings,
Environment $environment,
SettingsStatus $settings_status,
LoggerInterface $logger
LoggerInterface $logger,
SettingsModel $new_settings = null
) {
$this->module_url = $module_url;
@ -127,6 +135,7 @@ class Button implements ButtonInterface {
$this->environment = $environment;
$this->settings_status = $settings_status;
$this->logger = $logger;
$this->new_settings = $new_settings;
}
/**
@ -445,9 +454,10 @@ class Button implements ButtonInterface {
* The configuration for the smart buttons.
*
* @return array
* @throws NotFoundException If the settings are not found.
*/
public function script_data(): array {
$use_shipping_form = $this->settings->has( 'googlepay_button_shipping_enabled' ) && $this->settings->get( 'googlepay_button_shipping_enabled' );
$use_shipping_form = $this->should_use_shipping();
// On the product page, only show the shipping form for physical products.
$context = $this->context();
@ -531,4 +541,19 @@ class Button implements ButtonInterface {
private function wc_countries(): WC_Countries {
return new WC_Countries();
}
/**
* Check if new settings model exist and if so check enable pay now setting,
* if none of the above is true, check legacy settings for shipping enabled.
*
* @return bool Whether shipping should be used or not.
* @throws NotFoundException If the settings are not found.
*/
private function should_use_shipping(): bool {
if ( ! is_null( $this->new_settings ) && $this->new_settings->get_enable_pay_now() === true ) {
return true;
}
return $this->settings->has( 'googlepay_button_shipping_enabled' ) && $this->settings->get( 'googlepay_button_shipping_enabled' );
}
}

View file

@ -84,6 +84,18 @@ class ChangeCartEndpointTest extends TestCase
->with(ChangeCartEndpoint::nonce())
->andReturn($data);
// Mock WC session for shipping data preservation
$session = Mockery::mock(\WC_Session::class);
$session->shouldReceive('get')
->with('chosen_shipping_methods')
->andReturn(null);
$session->shouldReceive('set')
->with('chosen_shipping_methods', Mockery::any());
$wc = Mockery::mock();
$wc->session = $session;
expect('WC')->andReturn($wc);
$pu = Mockery::mock(PurchaseUnit::class);
$pu
->shouldReceive('to_array')