woocommerce / woocommerce-paypal-payments

https://wordpress.org/plugins/woocommerce-paypal-payments/
GNU General Public License v2.0
62 stars 47 forks source link

PayPal button doesn't show unless Checkout is reloaded (295) #1337

Open metinucar opened 1 year ago

metinucar commented 1 year ago

Describe the Bug

We have PayPal and Stripe. We'd like to enable PayPal for US, CA and PR and Stripe for the RoW. We're using woocommerce_available_payment_gateways hook to enable / disable providers based on county. This works as expected and it toggles correct payment provider. But for PayPal the yellow pay button doesn't show up until the user reloads the checkout page.

add_filter( 'woocommerce_available_payment_gateways', 'payment_gateway_disable_countries' );
if ( ! function_exists( 'payment_gateway_disable_countries' ) ) {
  function payment_gateway_disable_countries( $gateways ) {

    if ( is_admin() ) {
      return $gateways;
    }

    if ( ! is_object( WC()->customer ) OR ! method_exists( WC()->customer, 'get_billing_country' ) ) {
      return $gateways;
    }

    $paypal_countries = array('US', 'CA', 'PR');

    if ( is_wc_endpoint_url( 'order-pay' ) ) { // Pay for order page
      $order = wc_get_order( wc_get_order_id_by_order_key( $_GET[ 'key' ] ) );
      $country = $order->get_billing_country();
    } else { // Cart page
      $country = WC()->customer->get_billing_country();
    }

    // Disable PayPal
    if ( ! in_array( $country, $paypal_countries ) ) {
      if ( isset( $gateways['ppcp-gateway'] ) ) {
        unset( $gateways['ppcp-gateway'] );
      }
    } else {
      if ( isset( $gateways['stripe'] ) ) {
        unset( $gateways['stripe'] );
      }
    }

    return $gateways;
  }
}

To Reproduce

  1. Go to 'Checkout'
  2. If the billing country is not US, CA or Canada click on 'Country selector' and select any of these countries
  3. Now Stripe is disabled and PayPal is enabled. However the PayPal button is not shown.
  4. Reload Checkout and now PayPal button is shown

Screenshots

Correct behaviour: Billing country is not US, CA or PR. Stripe is enabled and PayPal is disabled. Screenshot 2023-04-19 at 11 42 47

Wrong behaviour: Billing country is US. Stripe is disabled and PayPal is enabled. But PayPal button doesn't show. Even after filling all the checkout fields, the button doesn't show. Screenshot 2023-04-19 at 11 43 50

After reloading the page, the PayPal button shows. Screenshot 2023-04-19 at 11 44 42

Environment

System status ``` ```
fullaineven commented 1 year ago

hello i have also the same problem how can i fix the problem? ^_^ Can possible share some information^__^

metinucar commented 1 year ago

Hi @fullaineven,

@InpsydeNiklas shared a snippet with me in WordPress Forums and it worked for us after a few small tweaks. Hope it works for you.