Woocommerce hide payment gateway for user roles - php

Hey guys I have a cash on delivery payment method on my wordpress/woocomerce website that I want to hide from the customer user role and non-logged in users.
I've been searching up and down and the only thing I found close was this bit of code.
function paypal_disable_manager( $available_gateways )
{global $woocommerce;
if ( isset( $available_gateways['paypal'] ) && current_user_can('customer') ) {
unset( $available_gateways['paypal'] );
}
return $available_gateways;
}
add_filter( 'woocommerce_available_payment_gateways','paypal_disable_manager' );
Would someone be able to help me modify this code to make it work for my use. Thank you in advance!

Have mention the code which is tried and tested for you. It works well. Lemme know if the same works for you too.
function wdm_disable_cod( $available_gateways ) {
//check whether the avaiable payment gateways have Cash on delivery and user is not logged in or he is a user with role customer
if ( isset($available_gateways['cod']) && (current_user_can('customer') || ! is_user_logged_in()) ) {
//remove the cash on delivery payment gateway from the available gateways.
unset($available_gateways['cod']);
}
return $available_gateways;
}
add_filter('woocommerce_available_payment_gateways', 'wdm_disable_cod', 99, 1);

<?php
//--- Filter for remove any payment gateway as per the user role selected --
add_filter('woocommerce_available_payment_gateways','filter_gateways',1);
function filter_gateways($gateways){
global $woocommerce, $current_user;
if ( is_user_logged_in() ) {
$userRole = implode(',',$current_user->roles);
if($userRole == 'my_user_role'){
//-- Remove casho on delivery if following user have logged in
unset($gateways['cod']);
}
}else{
//-- Hide COD if user not logged in
unset($gateways['cod']);
}
return $gateways;
}
?>
//-- Try this one, I have already make use of this code against minimum order limit

Related

Disable payment methods based on WooCommerce cart total

I tried this code below to hide/disable Credit/Debit card and Direct bank transfer payment method on Woo commerce(WordPress) when the checkout total == 400 but did not work. Please any idea on how to achieve this? Thank you so kindly.
function payment_gateway_disable_total_amount( $available_gateways ) {
global $woocommerce;
if ( isset( $available_gateways['bacs'] ) && $woocommerce->cart->total == 400 ) {
unset( $available_gateways['bacs'] );
}
if ( isset( $available_gateways['youpay'] ) && $woocommerce->cart->total == 400 ) {
unset( $available_gateways['youpay'] );
}
return $available_gateways;
}
add_filter( 'woocommerce_available_payment_gateways', 'payment_gateway_disable_total_amount' );
Why using a fixed total? There is very few chances that any customer get speifically 400 as total. It should be "up to 400" instead, so something like if( $tolal >= 400 ).
Also "Debit/Credit Cards" doesn't seem to be the right Payment method Id… See [this thread][1] to find out the right Payment method Id for "Debit/Credit Cards" payment gateway.
Try the following (assuming that "Debit/Credit Cards" payment method id is correct):
add_filter( 'woocommerce_available_payment_gateways', 'show_hide_payment_methods' );
function show_hide_payment_methods( $available_gateways ) {
if ( WC()->cart->total >= 400 ) {
if ( isset($available_gateways['bacs']) ) {
unset($available_gateways['bacs']);
}
if ( isset($available_gateways['Debit/Credit Cards']) ) {
unset($available_gateways['Debit/Credit Cards']);
}
}
return $available_gateways;
}
Code goes in functions.php file of the active child theme (or active theme). It should works.

Only show BACS payment gateway for logged in users if a coupon is applied to cart in WooCommerce

The code I have works for hiding the BACS payment gateway for guests and customers, but I need to change it so that the BACS gateway only becomes available IF the customer/admin apply a certain coupon code called FOOD on the CART or CHECKOUT.
In other words: hide the BACS gateway until the COUPON called FOOD is applied on the CART or CHECKOUT.
Here's the code that I have:
add_filter('woocommerce_available_payment_gateways', 'show_bacs_if_coupon_is_used', 99, 1);
function show_bacs_if_coupon_is_used( $available_gateways ) {
$current_user = wp_get_current_user();
if ( isset($available_gateways['bacs']) && (current_user_can('customer'))) {
unset($available_gateways['bacs']);
} else if ( isset($available_gateways['bacs']) && !is_user_logged_in()) {
unset($available_gateways['bacs']);
}
return $available_gateways;
}
Only show BACS payment method when a specific coupon is applied to cart for logged in users only (using WC_Cart get_applied_coupons() method):
add_filter('woocommerce_available_payment_gateways', 'show_bacs_for_specific_applied_coupon', 99, 1);
function show_bacs_for_specific_applied_coupon( $available_gateways ) {
if ( is_admin() ) return $available_gateways; // Only on frontend
$coupon_code = 'FOOD'; // <== Set here the coupon code
if ( isset($available_gateways['bacs']) && ! ( is_user_logged_in() &&
in_array( strtolower($coupon_code), WC()->cart->get_applied_coupons() ) ) ) {
unset($available_gateways['bacs']);
}
return $available_gateways;
}
Code goes in functions.php file of your active child theme (or active theme). Tested and works.

Remove some payment gateways if any coupon code is applied in Woocommerce

I started to work on small Woocommerce project. I have 3 payment gateways into this store: Paypal, Credit Card and Direct bank Transfer.
What I would like is: If coupon code is used, I would like to disable (or remove) Paypal and Credit Card from available payment gateways, and just keep "Direct bank Transfer" as available payment gateway choice.
To show how is look current state from checkout page:
I found a similar solution, but this is for removing gateway based on product category.
add_filter( 'woocommerce_available_payment_gateways', 'unset_payment_gateways_by_category' );
function unset_payment_gateways_by_category( $available_gateways ) {
global $woocommerce;
$unset = false;
$category_ids = array( 8, 37 );
foreach ( $woocommerce->cart->cart_contents as $key => $values ) {
$terms = get_the_terms( $values['product_id'], 'product_cat' );
foreach ( $terms as $term ) {
if ( in_array( $term->term_id, $category_ids ) ) {
$unset = true;
break;
}
}
}
if ( $unset == true )
unset( $available_gateways['cheque'] );
return $available_gateways;
}
So I think that this function can be used, but slightly modified per my problem.
Any help is appreciated.
The following code will remove all payment gateways except "Direct bank Transfer" (bacs) only if at least one coupon code has been applied by the customer:
add_filter('woocommerce_available_payment_gateways', 'applied_coupons_hide_payment_gateways', 20, 1 );
function applied_coupons_hide_payment_gateways( $available_gateways){
// Not in backend (admin)
if( is_admin() )
return $available_gateways;
// If at least a coupon is applied
if( sizeof( WC()->cart->get_applied_coupons() ) > 0 ){
// Loop through payment gateways
foreach ( $available_gateways as $gateway_id => $gateway ) {
// Remove all payment gateways except BACS (Bank Wire)
if( $gateway_id != 'bacs' )
unset($available_gateways[$gateway_id]);
}
}
return $available_gateways;
}
Code goes in function.php file of the active child theme (or active theme). Tested and works.
here you go :
add_filter('woocommerce_available_payment_gateways', 'unset_gatway_by_applied_coupons');
function unset_gatway_by_applied_coupons($available_gateways)
{
$coupons = WC()->cart->applied_coupons;
if (!empty($coupons)) {
unset($available_gateways['bacs']);
}
return $available_gateways;
}
what we did here we checked if any coupons is applied trough WC()->cart->applied_coupons; which will return an array of coupons if coupons array not empty remove specific payment gateway
if you want to check if certain coupon is applied and remove gatway based on your condition you can use the following:
add_filter('woocommerce_available_payment_gateways', 'unset_gatway_by_applied_coupons');
function unset_gatway_by_applied_coupons($available_gateways)
{
$coupons = WC()->cart->applied_coupons;
foreach ($coupons as $coupon) {
if ($coupon == 'my_coupon') { //here you can specific your coupon name
unset($available_gateways['bacs']);
}
}
return $available_gateways;
}
of course both functions are tested, you just need to place them in your functions.php

Disable tax for non logged in users

In WooCommerce, I'm trying to apply set_is_vat_exempt() method for customer and guest user.
For logged customer it is working fine. Can anyone suggest how can I do it?
One issue could be like as the user is not logged in so may be $woocommerce->customer could not enable it.
Here's my code:
$bilalId = get_current_user_id();
add_filter( 'woocommerce_cart_totals_order_total_html', 'wc_cart_totals_order_total_html_bn');
function wc_cart_totals_order_total_html_bn() {
global $woocommerce;
if( current_user_can('customer' || $bilalId == 0) ) {
$woocommerce->customer->set_is_vat_exempt(true);
}
}
At the end, I just want to disable any tax rates for non-logged in user.
even I tried "Zero rate" but doesn't worked for me.
Any kind of guidance would be appreciated.
Thanks.
So what you need is to use the wordpress conditional is_user_logged_in() in a custom function hooked in init action hook, this way:
add_action( 'init', 'wc_tax_exempt_unlogged' );
function wc_tax_exempt_unlogged() {
// Getting user data for logged users
if( is_user_logged_in() ){
$current_user = wp_get_current_user();
$current_user_id = $current_user->ID;
$current_user_roles = $current_user->roles;
$bilal_id = 0;
}
// Exempting of VAT non logged users, customers and the main admin ID (you)
if( ! is_user_logged_in() || in_array( 'customer', $current_user_roles ) || $bilal_id == $current_user_id ){
WC()->customer->set_is_vat_exempt(true);
}
}
The Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
This code is tested and works.

Disable Local Delivery for everyone except "wholesale_customer"

Alright, I've been pulling my hair out the last couple of days trying to figure this out.
I have a wholesale plugin in wordpress install with woocommerce. It gives the user "wholesale_customer" special rates over everyone else. I want to be able to offer local delivery to only the "wholesale_customer" user role but can't seem to figure out how to do it.
I've gotten this code from #mcorkum but it's still not working.
/**
* Add local delivery for wholesale customers
*/
function wholesale_local_delivery($available_methods) {
global $woocommerce;
global $current_user;
$user_roles = $current_user->roles;
$user_role = array_shift($user_roles);
if ( isset( $available_methods['local_delivery'] ) ) {
if ($user_role == 'wholesale_customer' ) {
unset( $available_methods['local_delivery'] );
}
}
return $available_methods;
}
add_filter( 'woocommerce_package_rates', 'wholesale_local_delivery', 10, 1);
I know this achievable with a plugin, but I'd rather not use plugins or pay for it for that matter.
Does anyone see anything that I'm not seeing?
/**
* Add local delivery for wholesale customers
*/
function wholesale_local_delivery($available_methods) {
global $woocommerce;
global $current_user;
if ( isset( $available_methods['local_delivery'] ) ) {
if ( !current_user_can( 'wholesale_customer' ) ) {
unset( $available_methods['local_delivery'] );
}
}
return $available_methods;
}
add_filter( 'woocommerce_package_rates', 'wholesale_local_delivery', 10, 1);
Try the above code by pasting it in your theme's functions.php file. And let me know if this worked for you.
I'm not a wordpress dev, but that code doesn't look like it is providing a user with role "wholesale_customer" the "local_delivery" option. On the contrary in fact, it looks to be removing the local delivery option if the user role IS "wholesale_customer":
if ( isset( $available_methods['local_delivery'] ) ) {
if ($user_role == 'wholesale_customer' ) {
unset( $available_methods['local_delivery'] );
}
}
If I was to take this code simply at face value (As I am not a wordpress dev) I would re-write this function to be easier to understand and read:
function wholesale_local_delivery($available_methods)
{
global $woocommerce;
global $current_user;
// Return early if no local delivery option is available
if (!isset($available_methods['local_delivery'])) {
return $available_methods;
}
// Determine if the user has a user role of wholesale customer
$hasRoleWholeSaleCustomer = false;
foreach ($current_user->roles as $role) {
if ($role === 'wholesale_customer') {
$hasRoleWholeSaleCustomer = true;
break;
}
}
// If the user does not have the role wholesale customer
// And for the code here to be being processed the local delivery
// option must be available
if (!$hasRoleWholeSaleCustomer) {
unset($available_methods['local_delivery']);
}
// Return the available methods applicable to the users roles
return $available_methods;
}
Hope someone else with experience in woocomerce, can give a better answer. But in the meantime, you can try this re-write and see if it works for you.
Goodluck.

Categories