WooCommerce How to Display Stripe Based on PostCode

1 day ago 4
ARTICLE AD BOX

In WordPress 6.9 version and WooCommerce version 10.3.6,

I want to display the Stripe based on the shipping zone.

Shipping zone 1 consists of Post code 3000

Shipping zone 2 consists of Post code 4000.

ex: Post Code 3000 should display Stripe. Post Code 4000 or any other postcodes should hide Stripe.

Currently, I am using woocommerce_available_payment_gateways webhook to display.

add_filter('woocommerce_available_payment_gateways', 'payment_gateways_based_on_chosen_shipping_method'); function payment_gateways_based_on_chosen_shipping_method($available_gateways) { $shouldReturnAll = true; if( is_checkout() && ! is_wc_endpoint_url() && WC()->cart ) { $shouldReturnAll = false; } if($shouldReturnAll){ return $available_gateways; } $chosen_shipping_methods = (array) WC()->session->get('chosen_shipping_methods'); $insideSelectiveDelivery = in_array('flat_rate:1', $chosen_shipping_methods); // This should display Stripe $insideAustraliaDelivery = in_array('flat_rate:6', $chosen_shipping_methods); // This should hide Stripe $outsideAustraliaDelivery = !$insideSelectiveDelivery && !$insideAustraliaDelivery; if($outsideAustraliaDelivery && !$isLocalPickup){ return []; } ...... if($insideSelectiveDelivery){ unset($available_gateways['cod']); } else { unset($available_gateways['stripe']); } return $available_gateways; }

Doing so hides Stripe if the user adds the postcode to 3000 (or refreshes the page with 3000 as in session). It also works if postcode is 3000 -> changes to 3001 (hides it) -> changes to 3000 (shows).

The issue occurs if the user types postcode other than 3000 (eg: 3001) and again changes to 3000. During that time, it does not show Stripe.

I have also tried using

$available_gateways['stripe']->settings['enabled'] = 'no'; $available_gateways['stripe']->enabled = 'no';

and enabling it to

$available_gateways['stripe']->settings['enabled'] = 'yes'; $available_gateways['stripe']->enabled = 'yes';

But the result is same. It only works if the initial postcode is 3000. If the user starts or session postcode is 3001, it does not work.

Read Entire Article