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.
Related
Am trying to hide paypal payment gateway for a digital product. For some complex reason I have to hide the payment gateway not entirely for the product, but if the user proceeds to checkout from another page (referrer page). Here is the code I tried to put together, but it doesnot hide the gateway. Any assistance will be appretiated.
//hide paypal if user access checkout from "send money" page
add_filter( 'woocommerce_available_payment_gateways', 'dealion_payment_gateway_disable_onpage' );
function dealion_payment_gateway_disable_onpage( $available_gateways ) {
$referer=wp_get_referer();
var_dump($referer);
if ( isset( $available_gateways['paypal'] ) && $referer== "https://www.myshop.store/send-money/") {
unset( $available_gateways['paypal'] );
}
return $available_gateways;
}
You can try using the WC()->session and store a custom variable so that it doesn't clear your referrer.
add_filter( 'woocommerce_available_payment_gateways', 'dealion_payment_gateway_disable_onpage' );
function dealion_payment_gateway_disable_onpage( $available_gateways ) {
// Don't check gateways on Admin.
if ( is_admin() ) {
return $available_gateways;
}
if ( null === WC()->session->get( 'dealion_referer' ) ) {
// Get the Referer and Store in a WC Session Var.
$referer = wp_get_referer();
WC()->session->set( 'dealion_referer', $referer );
} else {
$referer = WC()->session->get( 'dealion_referer' );
}
if ( isset( $available_gateways['paypal'] ) && 'https://www.myshop.store/send-money/' === $referer ) {
unset( $available_gateways['paypal'] );
}
return $available_gateways;
}
Please Guys,
I use two payment gateways on the checkout page:
1° bacs = Bank transfer
2° cod = Cash on Delivery
i need to hide the payment gateway COD = Cash on Delivery, If the user has No Admin Profile (!is_user_admin()), || or it's not logged, || ! is_user_logged_in() then hide.
This is the code that i'm using and it's working.
add_filter( 'woocommerce_available_payment_gateways', 'bbloomer_cod_hide' );
function bbloomer_cod_hide( $available_gateways ) {
if ( isset( $available_gateways['cod']) && !is_user_admin() ) {
unset( $available_gateways['cod'] );
}
return $available_gateways;
}
The problem is...the payment gateway bacs = Bank transfer is also affected and hidden : )
so i tried with this another hook
add_filter( 'woocommerce_available_payment_gateways', 'transfer_enable_bacs' );
function transfer_enable_bacs( $available_gateways ) {
if ( isset( $available_gateways['bacs']) && !is_user_admin() ) {
//unset( $available_gateways['bacs'] );
}
return $available_gateways;
}
But this is not the correct solution. Both payment gateways are hidden.
What am i doing wrong please?
Gratitude!
I think you need to replace is_user_admin with current_user_can
add_filter( 'woocommerce_available_payment_gateways', 'bbloomer_cod_hide' );
function bbloomer_cod_hide( $available_gateways ) {
if ( isset( $available_gateways['cod'] ) && ! current_user_can( 'administrator' ) ) {
unset( $available_gateways['cod'] );
}
return $available_gateways;
}
is_user_admin does not check if the user is an administrator; use current_user_can() for checking roles and capabilities.
https://developer.wordpress.org/reference/functions/is_user_admin/
try the following code:
add_filter( 'woocommerce_available_payment_gateways', 'bbloomer_cod_hide' );
function bbloomer_cod_hide( $available_gateways ) {
if ( !is_user_logged_in() ) || !current_user_can( 'install_themes' ) ) {
unset( $available_gateways['cod'] );
}
return $available_gateways;
}
I am trying to remove the / month or / year on the cart and checkout page for Woocommerce Subscriptions.
So Instead of $99 / year I only want to show $99
Hoping someone can point me i. the right direction.
Thank you
You can try using the following, that will clean everything except the recurring price and totals:
// Items pricing
add_filter( 'woocommerce_subscriptions_product_price_string', 'filter_wc_subscriptions_product_price_string', 10, 3 );
function filter_wc_subscriptions_product_price_string( $price_string, $product, $args ) {
if ( is_cart() || ( is_checkout() && ! is_wc_endpoint_url() ) ) {
return $args['price'];
}
return $price_string;
}
// Total lines
add_filter( 'woocommerce_subscription_price_string', 'filter_wc_subscription_price_string', 10, 2 );
function filter_wc_subscription_price_string( $subscription_string, $subscription_details ) {
if ( is_cart() || ( is_checkout() && ! is_wc_endpoint_url() ) ) {
$recurring_amount = $subscription_details['recurring_amount'];
if( is_numeric( $recurring_amount ) ) {
return strip_tags( wc_price($recurring_amount) ); // For shipping methods
}
else {
return $recurring_amount;
}
}
return $subscription_string;
}
Based on:
WooCommerce Subscriptions Filter Reference documentation
Hide the "free trial" text from Woocommerce Subscriptions price
I made a two-step payment on the site. Payment occurs after confirmation of the order by the manager. First, the user selects the payment method "for confirmation"(renamed "cash on delivery") and pay only after receiving the invoice for payment. On the checkout page, I hide paypal via js. I would like paypal to be hidden when on-hold status. When the status of "Pending payment" is disabled "for confirmation"(renamed "cash on delivery") and payment via paypal is available.
Update July 2020
The following code will show hide payment gateways:
On checkout page it will remove "paypal" payment option (So you can remove your jQuery code)
On Order Pay page it will:
Keep "paypal" only payment option if the order status is "pending" (removing all other options)
For others order statuses than "pending", the payment is not allowed by Woocommerce…
The code:
// Show/hide payment gateways
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;
}
Code goes in function.php file of your active child theme (or active theme). Tested and work.
Copy and paste the same code and it didn't work
syntax error, unexpected 'elseif' (T_ELSEIF)
I correct the code
// Show/hide payment gateways
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;
}
enter image description here
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