• Resolved ucarmetin

    (@ucarmetin)


    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.

    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.

    After reloading the page, the PayPal button shows.

    Environment

    • WordPress Version: 6.2
    • WooCommerce Version: 7.4.0
    • Plugin Version: 2.0.4

    So, PayPal button needs to show without the need for reloading. How can I accomplish this?

    • This topic was modified 1 year, 7 months ago by ucarmetin.
Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Support Syde Niklas

    (@niklasinpsyde)

    Hi @ucarmetin

    The PayPal smart buttons are not initialized when the PayPal gateway is unset on page load and enabled again via AJAX.

    So when any location other than US, CA, or PR is selected as a default billing country during the page load, the PayPal buttons won’t be available without reloading the page even after changing the country.

    A different approach would be needed to hide the PayPal gateway and smart buttons on the Checkout page dynamically. So instead of unsetting the gateway, you could try hiding it with CSS and JavaScript, as in this example snippet.

    There may be other ways to work around this, but unsetting the gateway on page load would result in the reported behavior.
    I hope this provides you with some direction to work around the limitation.

    Kind regards,
    Niklas

    Thread Starter ucarmetin

    (@ucarmetin)

    Thank you @niklasinpsyde.

    I’ve tried your alternative solution and confirm that it works as expected.

    Thank you

    Plugin Support Syde Niklas

    (@niklasinpsyde)

    Hi @ucarmetin

    Thank you for the feedback. I’m glad to hear that the alternative solution worked for you.

    The team plans to enhance the button rendering logic to accommodate use cases like yours better. Please note that the provided workaround is a temporary solution until the necessary improvements are made to the plugin. Refining this logic requires significant refactoring and thorough testing to ensure seamless functionality. But once the necessary changes are made, you can use your original code without the mentioned workaround.

    Kind regards,
    Niklas

    Thread Starter ucarmetin

    (@ucarmetin)

    Totally. I’m certain that the team will address this in upcoming releases and then I’ll revert to my original solution.

    Much appreciated!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘PayPal button doesn’t show unless Checkout is reloaded’ is closed to new replies.