Black Friday & Cyber Monday SUPER SALE ALL WEEK:
Grab 40% OFF on plugins
Days
Hours
Minutes
Seconds

How to enable a WooCommerce payment method when the order total is above a certain value?

As a WooCommerce store owner, you may have the necessity to get partnered with payment gateways to provide additional benefits such as free shipping upgrades, exclusive discounts, or special promotions to customers based on the order total. Based on the discounts associated with the payment method and the order total threshold set, you can enable or disable a particular payment method.

This post covers how to enable a payment method when the WooCommerce cart total exceeds a certain value. Besides this, If you are looking to provide percentage-based discounts for a certain payment method, then look into our post on adding percentage discounts for different payment methods.

Where to Add Custom Code in WooCommerce?

It is advisable to add the code snippets to the functions.php file of your child theme. Access the file directly from Appearance->Theme File Editor->Locating the child theme’s functions.php from the right sidebar. You can also access it from your theme’s directory file. Insert the following code snippet in functions.php. The alternative & easy option is to install & activate the Code Snippets plugin. You can then add the code as a new snippet via the plugin.

Preliminary steps

You need to decide two things before you start to implement the below code. 

  • Make sure which payment method you are going to enable when the order total is above a certain threshold.
  • Determine the order total threshold.

Solution: Enabling WooCommerce Payment Method Based on Order Total

The code snippet provided below functions in a similar manner to the following use case example:

Let’s consider you are selling popular gadget for a digital store. Such platforms will set restrictions while placing an order, these platforms often disable the Cash on Delivery (COD) payment method if the order total is greater than the threshold amount they have set. Here the below code snippet helps you to disable the Cash on Delivery option if the order total is above $1000, while the rest of the payment methods remain unaffected. If the order total is below $1000, the cash-on-delivery option is enabled.

add_filter( 'woocommerce_available_payment_gateways', 'ts_disable_cod' );

function ts_disable_cod( $available_gateways ) {
	$minimum = 1000;
	if ( WC()->cart->total > $minimum ) {
		unset( $available_gateways['cod'] );
	}
	return $available_gateways;
}

Code Output

The first output shows us that the Cash on Delivery (cod) payment gateway is disabled when the order total is above $1000.

How to enable a WooCommerce payment method when the order total is above a certain value? - Tyche Softwares

And the second result shows us that the Cash on Delivery (cod) payment gateway is enabled when the order total is below $1000.

How to enable a WooCommerce payment method when the order total is above a certain value? - Tyche Softwares
How to enable a WooCommerce payment method when the order total is above a certain value? - Tyche Softwares

Easily Set Fees & Discounts For Payment Gateways

Finding it difficult to manage all payment gateway fees and adjusting your product pricing accordingly?

The Payment Gateway Based Fees & Discounts plugin is designed to set up fees & discounts for each of your payment modes and simplify your payment process.

Code Explanation

  1. The code starts by using the add_filter function to hook into the woocommerce_available_payment_gateways filter. This allows us to modify the available payment gateways on the checkout page.
  2. The callback function ts_disable_cod takes the $available_gateways as a parameter. This variable holds an array of all the available payment gateways.
  3. The minimum threshold amount of $1000 is set using the variable $minimum. 
  4. The condition if ( WC()->cart->total > $minimum ) checks if the total amount of the cart is greater than the defined minimum threshold. 
  5. The line unset( $available_gateways[‘cod’] ); is used to remove the Cash on Delivery payment method from the $available_gateways array. 
  6. Finally, the modified $available_gateways array is returned from the function using return $available_gateways; This ensures that the updated list of payment gateways is used on the checkout page.

Related Article: How to add WooCommerce checkout fees based on shipping method & payment method?

Method of Implementation via Plugin

One popular option to enable a certain payment method based on order total is the WooCommerce Disable Payment Methods based on cart conditions plugin. The free version allows you to access multiple features creating unlimited conditions for every gateway or multiple gateways at once & based on cart totals. It works fine for all payment gateways. It allows you to set conditions based on order Subtotal, Product categories, Product brand, Product attributes, Product height, and more on with the PRO features.

Conclusion

This code snippet enables Cash on Delivery when the order total is below the defined order total threshold. Instead of having a fixed order total threshold, you can use an AND condition (&&) to specify a range for the order total. Also, this code can enable/disable WooCommerce payment methods for all payment gateways.

Browse more in: Code Snippets, WooCommerce How Tos, WooCommerce Tutorials

Share It:

Subscribe
Notify of
2 Comments
Newest
Oldest
Inline Feedbacks
View all comments
khaled
6 months ago

what do i need to change for this to work with other payment gateway?

2
0
Would love your thoughts, please comment.x
()
x