How to Hide Payment Method in Woocommerce based on Postal Code - wordpress

In this woocommerce setup, I have 2 Payment methods, Paypal and Cash on Delivery.
Now how can Cash on Delivery be hidden/disabled for certain Postal codes only.
This is the code I found on Gist
// Disable gateway based on country
function payment_gateway_disable_country( $available_gateways ) {
global $woocommerce;
if ( isset( $available_gateways['ccavenue'] ) && $woocommerce->customer->get_country() <> 'IN' ) {
unset( $available_gateways['ccavenue'] );
} else if ( isset( $available_gateways['paypal'] ) && $woocommerce->customer->get_country() == 'IN' ) {
unset( $available_gateways['paypal'] );
}
return $available_gateways;
}
add_filter( 'woocommerce_available_payment_gateways', 'payment_gateway_disable_country' );
Gist Link

To disable/hidden "Cash on Delivery", Place this code in your theme's function.php .
For more detail: woocommerce-hide-payment-gatway-based-on-visitors-country
// Disable gateway based on country
function payment_gateway_disable_country( $available_gateways ) {
global $woocommerce;
if ( isset( $available_gateways['cod'] ) && $woocommerce->customer->get_country() <> 'IN' ) {
unset( $available_gateways['cod'] );
}
return $available_gateways;
}
add_filter( 'woocommerce_available_payment_gateways', 'payment_gateway_disable_country' );

In the "checkout page" user can have two addresses - billing and shipping one.
To work correctly only with changes of Shipping one if it's filled I changed a bit the code. You have to test shipping countrycode if it's set, if not just user countrycode:
function payment_gateway_disable_country( $available_gateways ) {
global $woocommerce;
$country = !empty($woocommerce->customer->get_shipping_country()) ? $woocommerce->customer->get_shipping_country() : $woocommerce->customer->get_country();
if ( isset( $available_gateways['cod'] ) && $country <> 'CZ' ) {
unset( $available_gateways['cod'] );
}
return $available_gateways;
}
add_filter( 'woocommerce_available_payment_gateways', 'payment_gateway_disable_country' );

In the code above you used country code to disable payment gateway by, but you mentioned you would like to do it by Postal Code.
You're right about using woocommerce_available_payment_gateways, but instead of using $woocommerce->customer->get_country() you have to use WC()->customer->get_shipping_postcode() (or WC()->customer->get_billing_postcode() for some situations).
You mentioned PayPal and Cash on Delivery payment gateway, we need their IDs, there are paypal and cod accordingly.
In the code below let's deactivate Cash on Delivery for a couple postal codes, for example '1234' and '5678':
add_filter( 'woocommerce_available_payment_gateways', function( $available_gateways ) {
// if Cash on Delivery is already disabled, let's exit the function
if( empty( $available_gateways['cod'] ) ) {
return $available_gateways['cod'];
}
// get postal code
$postal_code = WC()->customer->get_billing_postcode();
// deactivate payment method
if( in_array( $postal_code, array( '1234', '5678' ) ) ) {
unset( $available_gateways['cod'] );
}
return $available_gateways;
} );
Code can be inserted to your current theme functions.php file or a custom plugin. More info you can find in this tutorial: https://rudrastyh.com/woocommerce/hide-payment-methods-based-on-postal-code.html

Related

Remove Paypal payment if customer chose COD in WooCommerce [duplicate]

I would like to hide some payment method and enable another one when I select a specified “Shipping Method" in flexible Shipping plugin form wpdesk.
I have already tried that code:
add_filter( 'woocommerce_available_payment_gateways', 'gateway_disable_shipping_326' );
function gateway_disable_shipping_326( $available_gateways ) {
global $woocommerce;
if ( !is_admin() ) {
$chosen_methods = WC()->session->get( 'chosen_shipping_methods' );
$chosen_shipping = $chosen_methods[0];
if ( isset( $available_gateways['payment_method_cod'] ) && 0 === strpos( $chosen_shipping, 'flat_rate:6' ) ) {
unset( $available_gateways['payment_method_cod'] );
}
}
return $available_gateways;
}
and this one
function my_custom_available_payment_gateways( $gateways ) {
$chosen_shipping_rates = WC()->session->get( 'chosen_shipping_methods' );
if ( in_array( 'flat_rate:6', $chosen_shipping_rates ) ) :
unset( $gateways['payment_method_cod'] );
endif;
if ( in_array( 'flat_rate:8', $chosen_shipping_rates ) ) :
unset( $gateways['payment_method_przelewy24'] );
endif;
return $gateways;
}
add_filter( 'woocommerce_available_payment_gateways', 'my_custom_available_payment_gateways' );
The link to my website: [www.dajati.pl][1]
The following code example will enable / disable payment gateways based on chosen shipping method.
In this example, we have 3 shipping methods and 3 payment gateways. Each selected shipping method will enable only one different payment gateway.
add_filter( 'woocommerce_available_payment_gateways', 'payment_gateways_based_on_chosen_shipping_method' );
function payment_gateways_based_on_chosen_shipping_method( $available_gateways ) {
// Not in backend (admin) and Not in order pay page
if( is_admin() || is_wc_endpoint_url('order-pay') )
return $available_gateways;
// Get chosen shipping methods
$chosen_shipping_methods = (array) WC()->session->get( 'chosen_shipping_methods' );
if ( in_array( 'flat_rate:12', $chosen_shipping_methods ) )
{
unset( $gateways['bacs'] );
unset( $gateways['cod'] );
}
elseif ( in_array( 'flat_rate:14', $chosen_shipping_methods ) )
{
unset( $gateways['bacs'] );
unset( $gateways['paypal'] );
}
elseif ( in_array( 'free_shipping:10', $chosen_shipping_methods ) )
{
unset( $gateways['cod'] );
unset( $gateways['paypal'] );
}
return $gateways;
}
Code goes in functions.php file of your active child theme (or active theme). Tested and works.
To be able to get the correct shipping method ID you can use your browser inspector, this way:
Since I was looking for solution to this also, and LoicTheAztec's answer were removing all payments, here is a slightly modified working solution.
add_filter( 'woocommerce_available_payment_gateways', 'payment_gateways_based_on_chosen_shipping_method' );
function payment_gateways_based_on_chosen_shipping_method( $available_gateways ) {
// Not in backend (admin) and Not in order pay page
if( is_admin() || is_wc_endpoint_url('order-pay') )
return $available_gateways;
// Get chosen shipping methods
$chosen_shipping_methods = (array) WC()->session->get( 'chosen_shipping_methods' );
if ( in_array( 'flat_rate:12', $chosen_shipping_methods ) )
{
unset( $available_gateways['bacs'] );
unset( $available_gateways['cod'] );
}
elseif ( in_array( 'flat_rate:14', $chosen_shipping_methods ) )
{
unset( $available_gateways['bacs'] );
unset( $available_gateways['paypal'] );
}
elseif ( in_array( 'free_shipping:10', $chosen_shipping_methods ) )
{
unset( $available_gateways['cod'] );
unset( $available_gateways['paypal'] );
}
return $available_gateways;
}
The only difference is that all $gateways had to be changed to $available_gateways, or vice versa.

allow Cash On Delivery Option only for specific cities

I am using the follow code to allow Cash On Delivery option only for specific cities in WooCommerce. But I want to change it a bit and want that if billing city slightly matches with target city then system still allow COD. What's the alternate of exact match in arrays?
//COD for specific cities
add_filter( 'woocommerce_available_payment_gateways', 'payment_gateway_disable_country' );
function payment_gateway_disable_country( $available_gateways ) {
if ( is_admin() ) {
return $available_gateways;
}
if ( ! isset( $available_gateways['cod'] ) ) {
return $available_gateways;
}
global $woocommerce;
$targeted_cities = array( 'Lahore', 'Islamabad', 'Rawalpindi', 'Karachi', 'Gujranwala', 'Kamoki - Gujranwala', 'Faisalabad', 'Multan' );
if ( !in_array( $woocommerce->customer->get_billing_city(), $targeted_cities ) ) {
unset( $available_gateways['cod'] );
}
return $available_gateways;
}
And what should I do if I want to add allowed cities from the settings in dashboard?

Disable specific payment methods depending on Woocommerce order status syntax error, unexpected 'elseif' (T_ELSEIF) [duplicate]

This question already has answers here:
Disable specific payment methods depending on Woocommerce order status
(2 answers)
Closed 2 years ago.
help i get this error
I am doing the same function Disable specific payment methods depending on Woocommerce order status I get
Copy and paste the same code and it didn't work
syntax error, unexpected 'elseif' (T_ELSEIF)
add_filter( 'woocommerce_available_payment_gateways', 'conditionally_hide_payment_gateways', 100, 1 );
function conditionally_hide_payment_gateways( $available_gateways ) {
// 1. On Order Pay page
if( is_wc_endpoint_url( 'order-pay' ) ) {
// Get an instance of the WC_Order Object
$order = wc_get_order( get_query_var('order-pay') );
// Loop through payment gateways 'pending', 'on-hold', 'processing'
foreach( $available_gateways as $gateways_id => $gateways ){
// Keep paypal only for "pending" order status
elseif ($gateways_id !== 'paypal' && $order->has_status('pending') ) {
unset($available_gateways[$gateways_id]);
}
}
}
// 2. On Checkout page
elseif( is_checkout() && ! is_wc_endpoint_url() ) {
// Disable paypal
if( isset($available_gateways['paypal']) ) {
unset($available_gateways['paypal']);
}
}
return $available_gateways;
}
enter image description here
The elseif inside the foreach doesn't have a preceding if, so that's your error.
add_filter( 'woocommerce_available_payment_gateways', 'conditionally_hide_payment_gateways', 100, 1 );
function conditionally_hide_payment_gateways( $available_gateways ) {
// 1. On Order Pay page
if( is_wc_endpoint_url( 'order-pay' ) ) {
// Get an instance of the WC_Order Object
$order = wc_get_order( get_query_var('order-pay') );
// Loop through payment gateways 'pending', 'on-hold', 'processing'
foreach( $available_gateways as $gateways_id => $gateways ){
// Keep paypal only for "pending" order status
if ($gateways_id !== 'paypal' && $order->has_status('pending') ) {
unset($available_gateways[$gateways_id]);
}
}
}
// 2. On Checkout page
elseif( is_checkout() && ! is_wc_endpoint_url() ) {
// Disable paypal
if( isset($available_gateways['paypal']) ) {
unset($available_gateways['paypal']);
}
}
return $available_gateways;
}

Hide shipping and payment methods in WooCommerce

I'm building a WooCommerce e-shop and I need to tweak my checkout page by doing the following:
Hide a certain shipping method (only one) if the order total > 100€.
Hide the cash on delivery payment method if local pickup is selected.
Does anyone know how to do that? I have the Code Snippets plugin so I can easily add any custom code.
To hide specific shipping method based on the cart total, you can use below code snippet. You need to update your shipping method name in the code.
Disable shipping method as per cart total
Add this snippet in your theme's functions.php file or custom plugin file.
add_filter( 'woocommerce_package_rates', 'shipping_based_on_price', 10, 2 );
function shipping_based_on_price( $rates, $package ) {
$total = WC()->cart->cart_contents_total;
//echo $total;
if ( $total > 100 ) {
unset( $rates['local_delivery'] ); // Unset your shipping method
}
return $rates;
}
Disable Payment Gateway For Specific Shipping Method
Use below code snippet. Update code as per your payment method & shipping method.
add_filter( 'woocommerce_available_payment_gateways', 'x34fg_gateway_disable_shipping' );
function x34fg_gateway_disable_shipping( $available_gateways ) {
global $woocommerce;
if ( !is_admin() ) {
$chosen_methods = WC()->session->get( 'chosen_shipping_methods' );
$chosen_shipping = $chosen_methods[0];
if ( isset( $available_gateways['cod'] ) && 0 === strpos( $chosen_shipping, 'local_pickup' ) ) {
unset( $available_gateways['cod'] );
}
}
return $available_gateways;
}
There are a number of plugins that will do this for you, take a look at this one WooCommerce Conditional Shipping and Payments
You'll want to tie in to the "woocommerce_payment_gateways" action
Something along these lines:
function alter_payment_gateways( $gateways ){
$chosen_rates = ( isset( WC()->session ) ) ? WC()->session->get( 'chosen_shipping_methods' ) : array();
if( in_array( 'local-pickup:6', $chosen_rates ) ) {
$array_diff = array('cod');
$list = array_diff( $list, $array_diff );
}
return $list;
}
add_action('woocommerce_payment_gateways', 'alter_payment_gateways', 50, 1);
The number on the end of 'local-pickup' on line 4 will depend on your woocommerce setup. You can find the string you need to put in here by adding something to a basket, going to the checkout, right clicking on the "Local Pickup" option in the delivery methods and looking at the value attribute.

Wordpress: Cash on Delivery restrict to one country only

I am selling products to 4 countries; US, Canada, UK and France. I have enabled 2 checkout methods; Cash on Delivery (cod) and Paypal. I want to restrict cod to Canada only and hide from other countries. I have tried the below code but it makes cod disappear from all countries (including Canada).I know i am doing something wrong here, i am not an expert so need ur help. Thanks
/**
* #snippet WooCommerce Disable Payment Gateway for a Specific Country
*/
function payment_gateway_disable_country( $available_gateways ) {
global $woocommerce;
if ( isset( $available_gateways['cod'] ) && $woocommerce->customer->get_country() <> 'France' ) {
unset( $available_gateways['cod'] );
} else if ( isset( $available_gateways['cod'] ) && $woocommerce->customer->get_country() == 'France' ) {
unset( $available_gateways['cod'] );
}
return $available_gateways;
}
add_filter( 'woocommerce_available_payment_gateways', 'payment_gateway_disable_country' );
This should do it.
function payment_gateway_disable_country( $available_gateways ) {
global $woocommerce;
if ( isset( $available_gateways['cod'] ) && $woocommerce->customer->get_country() <> 'Canada' ) {
unset( $available_gateways['cod'] );
}
return $available_gateways;
}
add_filter( 'woocommerce_available_payment_gateways', 'payment_gateway_disable_country' );

Resources