On this submit I’m going to point out you tips on how to show WooCommerce fee strategies conditionally. And, better of all – we’re going to do it with none fancy plugin.
Video: How one can Show Woocommerce Cost Strategies Conditionally?
Necessities: How one can Show Woocommerce Cost Strategies Conditionally?
With a purpose to make it work that you must paste the snippets proven beneath utilizing a Code Snippets plugin (or your baby theme’s features.php file). That’s, simply paste the code and activate it.
Should you’re a professional then check out the WpCodeBox plugin which is is a significantly better various to Code Snippets.
WpCodeBox is my favourite code snippets supervisor for WordPress. This can be a premium plugin and when you’re , then seize WPCodeBox with a pleasant 20% low cost right here (SAVE 20% Coupon WPSH20).
Now, some explanations for the code snippets right here beneath:
- Check out the feedback contained in the code
- You have to change your fee strategies and different slugs (classes, tags and so forth) if wanted
How one can disable all Woocommerce fee gateways?
The rationale you would want to disable all fee gateways is that possibly you want it only for some catalogue or ordering system. Therefore, use this small piece of code.
// Disable all Woocommerce fee gateways
add_filter( 'woocommerce_cart_needs_payment', '__return_false' );
And that is the end result.
How one can Disable Woocommerce fee gateways for particular person roles?
Let’s think about that your store has a bunch of person roles. For instance, buyer, Gold member, Silver package deal and so forth. This snippet right here permits you to disable Woocommerce fee gateways for particular person roles.
Take note of line 8 for person position and features 11-15 for fee gateways to be eliminated. On this instance buyer can use onluy BACS and all different gateways are eliminated.
// Disable Woocommerce fee gateways for particular person roles
add_filter('woocommerce_available_payment_gateways', 'wpsh_hide_payment_roles', 1);
perform wpsh_hide_payment_roles($gateways)
{
$current_user = wp_get_current_user();
$position = $current_user->roles;
international $woocommerce;
// Right here goes your person position
if ($position[0] == 'buyer') {
// Add fee gateways that you must disable. On this instance solely BACS is activated
unset($gateways['cheque']); // Cheque
unset($gateways['cod']); // Money on supply
unset($gateways['paypal']); // Paypal
// unset($gateways['bacs']); // Direct financial institution switch (BACS)
}
return $gateways;
}
How one can Disable Woocommerce fee gateways for particular person roles (various technique)?
If for some motive earlier technique didn’t work out for you then use this snippet right here beneath.
// Disable Woocommerce fee gateways for particular person roles
add_filter( 'woocommerce_available_payment_gateways', 'wpsh_disable:payment_for_roles' );
perform wpsh_disable:payment_for_roles( $available_gateways ) {
/* Right here goes your person position */
if( current_user_can( 'buyer' ) ) {
/* Right here goes your fee gateway */
if ( isset( $available_gateways[ 'paypal' ] ) ) {
unset( $available_gateways[ 'paypal' ] );
}
/* Should you don’t must dsable multiple gateways, then delete subsequent three rows.
If that you must add extra gateways then simply copy subsequent three rows and exchange your gateway identify. */
if ( isset( $available_gateways[ 'bacs' ] ) ) {
unset( $available_gateways[ 'bacs' ] );
}
}
return $available_gateways;
}
How one can Disable Woocommerce fee gateways for logged-out customers?
Generally, if you want to show WooCommerce fee strategies conditionally, that you must arrange some guidelines in your logged-out customers.
For instance, I’ve a buyer who wanted to cover direct financial institution funds (BACS) for logged-out customers. So, if that you must disable some particular Woocommerce fee gateway for logged-out customers then this snippet right here helps you out.
As soon as once more, take note of line 7 which defines roles which might be allowed to make use of gateways. Different roles don’t have any entry to those gateways. On this instance buyer and administrator roles can use BACS and Test funds, all different person roles don’t.
// Disable Woocommerce fee gateways for logged-out customers
add_filter('woocommerce_available_payment_gateways', 'wpsh_disable_payment_for_logged_out_users', 90, 1);
perform wpsh_disable_payment_for_logged_out_users( $available_payment_gateways ) {
$person = wp_get_current_user();
// Outline roles which might be allowed to make use of gateways. Different roles don't have any entry to those gateways.
$allowed_roles = array('buyer', 'administrator');
if (!array_intersect($allowed_roles, $person->roles )) {
if (isset($available_payment_gateways['bacs'])) {
unset($available_payment_gateways['bacs']);
}
if (isset($available_payment_gateways['cheque'])) {
unset($available_payment_gateways['cheque']);
}
}
return $available_payment_gateways;
}
How one can Disable Woocommerce Cost Strategies for Particular Class?
Now let’s have a look on tips on how to disable Woocommerce fee strategies for particular class or classes. On this instance, we’re going to take away Money on supply (COD), Paypal and Test funds (see strains 16-19) for classes Equipment and Music (see line 6).
Simply exchange the class slugs and edit fee gateways that you must be eliminated.
// Disable Woocommerce Cost Strategies for Particular Class
add_filter('woocommerce_available_payment_gateways','wpsh_disable_payment_for_categories');
perform wpsh_disable_payment_for_categories($gateways){
international $woocommerce;
foreach ($woocommerce->cart->cart_contents as $key => $values ) {
// Add your product class ID numbers right here beneath
$category_ids = array( 48,47 );
$phrases = get_the_terms( $values['product_id'], 'product_cat' );
foreach ($phrases as $time period) {
if(in_array($time period->term_id,$category_ids)){
// Outline your fee gateways you wish to take away
unset($gateways['cod']); // Money on supply
unset($gateways['bacs']); // BACS
unset($gateways['cheque']); // Cheque
unset($gateways['paypal']); // Paypal
break;
}
break;
}
}
return $gateways;
}
How one can conceal Woocommerce fee gateways if “ship to totally different tackle” is chosen?
Since we already know tips on how to take away Woocommerce fee gateways if “ship to totally different tackle” is chosen.
This time, take note of line 62 which leaves solely Money on supply technique lively, all different fee gateways will probably be deactivated.
add_action( 'wp_footer', 'wpsh_ship_to_different' );
perform wpsh_ship_to_different() {
// Work on solely on checkout web page
if( is_checkout() && ! is_wc_endpoint_url() ):
// Take away "ship_different" customized WC session on load
if( WC()->session->get('ship_different') ){
WC()->session->__unset('ship_different');
}
// jQuery Ajax code beneath
?>
<script sort="textual content/javascript">
jQuery( perform($){
if (typeof wc_checkout_params === 'undefined')
return false;
var a = '#ship-to-different-address-checkbox', b = '';
// Ajax perform
perform triggerSTDA( worth ){
$.ajax({
sort: 'POST',
url: wc_checkout_params.ajax_url,
information: {
'motion': 'ship_different_address',
'ship_different': worth,
},
success: perform (end result) {
$('physique').set off('update_checkout');
console.log(end result); // For testing (to be eliminated)
}
});
}
$(a).on( 'change', perform(){
b = $(this).prop('checked') === true ? 'sure' : 'no';
triggerSTDA( b );
});
});
</script>
// Disguise Woocommerce fee gateways if "ship to totally different tackle" is chosen
add_action( 'wp_footer', 'wpsh_ship_to_different' );
perform wpsh_ship_to_different() {
// Work on solely on checkout web page
if( is_checkout() && ! is_wc_endpoint_url() ):
// Take away "ship_different" customized WC session on load
if( WC()->session->get('ship_different') ){
WC()->session->__unset('ship_different');
}
// jQuery Ajax code beneath
?>
<script sort="textual content/javascript">
jQuery( perform($){
if (typeof wc_checkout_params === 'undefined')
return false;
var a = '#ship-to-different-address-checkbox', b = '';
// Ajax perform
perform triggerSTDA( worth ){
$.ajax({
sort: 'POST',
url: wc_checkout_params.ajax_url,
information: {
'motion': 'ship_different_address',
'ship_different': worth,
},
success: perform (end result) {
$('physique').set off('update_checkout');
console.log(end result); // For testing (to be eliminated)
}
});
}
$(a).on( 'change', perform(){
b = $(this).prop('checked') === true ? 'sure' : 'no';
triggerSTDA( b );
});
});
</script>
<?php
endif;
}
// The WordPress Ajax PHP receiver
add_action( 'wp_ajax_ship_different_address', 'get_ajax_ship_different_address' );
add_action( 'wp_ajax_nopriv_ship_different_address', 'get_ajax_ship_different_address' );
perform get_ajax_ship_different_address() {
if ( isset($_POST['ship_different']) ){
WC()->session->set('ship_different', esc_attr($_POST['ship_different']));
echo $_POST['ship_different'];
}
die();
}
// Present/conceal fee gateways
add_filter( 'woocommerce_available_payment_gateways', 'wpsh_hide_gateways', 100, 1 );
perform wpsh_hide_gateways( $available_gateways ) {
if( WC()->session->get('ship_different') == 'sure' ) {
foreach( $available_gateways as $gateways_id => $gateways ){
if( $gateways_id !== 'cod' ) {
unset($available_gateways[$gateways_id]);
}
}
}
return $available_gateways;
}
How one can Disable Woocommerce Cost Gateways for Particular Transport Strategies?
Subsequent let’s transfer to the delivery technique primarily based show guidelines. That’s, I’m going to point out you tips on how to disable Woocommerce fee gateways for particular delivery strategies.
On this instance, we’re going to take away Paypal and Money on supply (see line 8) for Flat charge and Native pickup delivery strategies (see line 11).
Now, when you don’t know tips on how to discover out your delivery technique ID-s then check out the video above or make a proper click on on the delivery technique, choose Examine and the discover your delivery ID as proven on the screenshot.
// Disable Woocommerce Cost Gateways for Particular Transport Strategies
add_filter( 'woocommerce_available_payment_gateways', 'wpsh_hide_payment_for_shipping', 10, 1 );
perform wpsh_hide_payment_for_shipping( $available_gateways ) {
if ( ! ( is_checkout_pay_page() ) ) {
// Add your paymente gateways you wish to disable
$gateways_to_disable = array( 'paypal', 'cod' );
// Set delivery strategies
$shipping_methods = array( 'flat_rate:1', 'local_pickup:2');
$disable_gateways = false;
// Test if we have to disable gateways
foreach ( $shipping_methods as $shipping_method ) {
if ( strpos( WC()->session->get( 'chosen_shipping_methods' )[0], $shipping_method ) !== false ) $disable_gateways = true;
}
// If delivery mehtod is chosen then the fee gateway will probably be disabled
if ( $disable_gateways ) {
foreach ( $available_gateways as $id => $gateway ) {
if ( in_array( $id, $gateways_to_disable ) ) {
unset( $available_gateways[$id] );
}
}
}
return $available_gateways;
}
else { return $available_gateways;
}
}
How one can Disable Woocommerce Cost Gateways for Particular Transport Class?
Now it’s time to check out tips on how to disable Woocommerce fee gateways for particular delivery class. For instance, this snippet right here beneath removes Paypal and Money on supply strategies (see strains 9-13) if cart comprises product with “Demo class” delivery class (see line 7).
// Disable Woocommerce Cost Gateways for Particular Transport Class
add_filter( 'woocommerce_available_payment_gateways', 'wpsh_hide_payment_for_specific_class' );
perform wpsh_hide_payment_for_specific_class( $gateways ) {
if( !is_admin() ) {
foreach ( WC()->cart->get_cart_contents() as $key => $values ) {
// Exchange demo-class with your personal delivery class slug
if ( 'demo-class' === $values[ 'data' ]->get_shipping_class() ) {
if( isset( $gateways[ 'paypal' ] ) ) {
unset( $gateways[ 'paypal' ] );
}
if( isset( $gateways[ 'cod' ] ) ) {
unset( $gateways[ 'cod' ] );
// Don’t take away this line
break;
}
}
}
}
return $gateways;
}
How one can Disable Woocommerce Cost Gateways if Coupon is Utilized?
Let’s get inventive now and see tips on how to disable Woocommerce fee gateways if coupon is utilized. Doesn’t matter what coupon as a result of this time we’ll disable technique for any coupon.
On this instance I’m going to take away Money on supply and Test fee fee gateways provided that a coupon is utilized. If you need to know tips on how to disable Woocommerce fee gateways if a selected oupon is utilized then check out subsequent chapter beneath.
// Disable Woocommerce fee gateways if coupon is utilized
//
add_filter('woocommerce_available_payment_gateways', 'wpsh_remove_payment_if_coupon_applied');
perform wpsh_remove_payment_if_coupon_applied($available_gateways)
{
$coupons = WC()->cart->applied_coupons;
foreach ($coupons as $coupon) {
// Take away Money on supply
if(isset($available_gateways['cod'])){
unset($available_gateways['cod']);
}
// Take away Cheque
if(isset($available_gateways['cheque'])){
unset($available_gateways['cheque']);
}
}
return $available_gateways;
}
How one can disable Woocommerce fee gateways if a selected coupon is utilized?
On this instance right here beneath, BACS and COD fee strategies (see strains 13 and 14) will probably be eliminated provided that somebody makes use of coupon “test20” (see line 10).
So, if you want to disable Woocommerce fee gateways if a selected coupon is utilized, then simply use this snippet right here.
// Disable Woocommerce fee gateways if a selected coupon is utilized
add_filter('woocommerce_available_payment_gateways', 'wpsh_remove_payment_for_coupon');
perform wpsh_remove_payment_for_coupon($available_gateways){
$coupons = WC()->cart->applied_coupons;
foreach ($coupons as $coupon) {
// Outline your coupon code right here
if ($coupon == 'test20') {
// Outline fee gateways to be eliminated
unset($available_gateways['bacs']);
unset($available_gateways['cod']);
}
}
return $available_gateways;
}
How one can disable Woocommerce fee gateways for particular merchandise?
Nearly there, with a few extra snippets left. This time, we’ll disable Woocommerce fee gateways for particular merchandise in your cart.
On this instance, we’ll take away Paypal and COD strategies (see strains 21-25) provided that merchandise with ID-numbers 14 and 32 (see line 11) are added to the cart.
// Disable Woocommerce fee gateways for particular merchandise
add_filter( 'woocommerce_available_payment_gateways', 'rudr_payment_methods_by_product_ids' );
perform rudr_payment_methods_by_product_ids( $gateways ){
if( is_admin() ) {
return $gateways;
}
// Add product IDs you wish to unset fee gateways for
$product_ids = array( 14, 32);
if( is_wc_endpoint_url( 'order-pay' ) ) {
return $gateways;
}
foreach ( WC()->cart->get_cart_contents() as $key => $cart_item ) {
// depend variety of objects if wanted (non-compulsory)
if( in_array( $cart_item[ 'data' ]->get_id(), $product_ids ) ) {
// Take away Paypal
if( isset( $gateways[ 'paypal' ] ) ) {
unset( $gateways[ 'paypal' ] );
// Take away Money on Supply
if( isset( $gateways[ 'cod' ] ) ) {
unset( $gateways[ 'cod' ] );
}
break; // Don’t take away this line
}
}
}
return $gateways;
}
How one can disable Woocommerce fee strategies primarily based on cart complete?
This time it’s a easy one: we’re going to disable Woocommerce fee strategies primarily based on cart complete. That’s, if the full is greater than 80 (see line 8) euros then COD, BACS and Test funds will probably be eliminated (see strains 11-14).
// Disable Woocommerce fee strategies primarily based on cart complete
add_filter( 'woocommerce_available_payment_gateways', 'wpsh_disable_gateways_based_on_total', 1);
perform wpsh_disable_gateways_based_on_total( $gateways ){
international $woocommerce;
// Set your cart quantity right here. You possibly can set rule primarily based on "is greater than" and "is lower than".
if($woocommerce->cart->complete > 80) {
// Add fee gateways that you must disable. On this instance solely BACS is activated. You possibly can take away those you don’t want
unset($gateways['cod']);
unset($gateways['cheque']);
// unset($gateways['paypal']);
// unset($gateways['bacs']);
}
return $gateways;
}
How one can disable Woocommerce fee strategies for a selected nation?
Why would that you must disable Woocommerce fee strategies for a selected nation? Effectively, if my store is in Europe, and also you’re in USA then I don’t see any level to utilizing Money on supply (COD) fee technique.
Due to this fact, on this instance, I’ll take away COD for USA (see line 12-15). Additionally, I’ll take away Paypal and Test funds for Estonia, Finland and Norway (see strains 19-24).
// Disable Woocommerce fee strategies for a selected nation
add_filter( 'woocommerce_available_payment_gateways', 'wpsh_hide_payment_for_countries', 10, 1 );
perform wpsh_hide_payment_for_countries( $payment_gateways ) {
if ( is_admin() ) return $payment_gateways;
// Get nation
$customer_country = WC()->buyer->get_shipping_country() ? WC()->buyer->get_shipping_country() : WC()->buyer->get_billing_country();
// Disguise COD for USA
if ( in_array( $customer_country, array( 'US' ) ) ) {
// Disguise Money on supply for United States
if ( isset( $payment_gateways['cod'] ) ) {
unset( $payment_gateways['cod'] );
}
}
// Disguise Paypal and Cheque funds for Estonia, Finland and Norway
if ( in_array( $customer_country, array( 'EE', 'FI', 'NO' ) ) ) {
if ( isset( $payment_gateways['paypal'] ) ) {
unset( $payment_gateways['paypal'] );
}
if ( isset( $payment_gateways['cheque'] ) ) {
unset( $payment_gateways['cheque'] );
}
}
return $payment_gateways;
}
How one can disable Woocommerce fee gateways for bacordered merchandise?
Check out this snippet right here beneath. This one will conceal BACS and Paypal fee strategies if cart comprises a backordered product. Strains 9 and 10 are those to switch accordingly.
// Disable Woocommerce fee gateways for bacordered merchandise
add_filter( 'woocommerce_available_payment_gateways', 'wpsh_backordered_hide', 90, 1 );
perform wpsh_backordered_hide( $available_gateways ) {
if ( is_admin() )
return $available_gateways;
// Loop by cart objects
foreach( WC()->cart->get_cart() as $cart_item ){
if( $cart_item['data']->is_on_backorder( $cart_item['quantity'] ) ) {
unset($available_gateways['bacs']); // Disguise BACS fee gateway
unset($available_gateways['paypal']); // Disguise Paypal fee gateway
break; // Cease the loop
}
}
return $available_gateways;
}
How one can add Cost Gateway Primarily based Charges in Woocommerce?
Subsequent a part of our “How one can show WooCommerce fee strategies conditionally?” submit is right here. Meaning, I’ll present you tips on how to add Cost Gateway Primarily based Charges in Woocommerce. For instance, let’s add two sorts of charges with the assistance of this code:
- Mounted price (2 euros) for utilizing Money on supply price (see line 15)
- Proportion price (5%) for utilizing Paypal (see line 16).
Simply add your personal charges and fee strategies, and also you’re good to go.
// Add Cost Gateway Primarily based Charges in Woocommerce
add_action( 'woocommerce_cart_calculate_fees', 'wpsh_add_handling_fee' );
perform wpsh_add_handling_fee ( $cart ) {
if ( is_admin() && ! outlined( 'DOING_AJAX' ) )
return;
$chosen_payment_id = WC()->session->get('chosen_payment_method');
if ( empty( $chosen_payment_id ) )
return;
$subtotal = $cart->subtotal;
// Right here you may arrange your charges (mounted or proportion)
$targeted_payment_ids = array(
'cod' => 2, // Mounted price
'paypal' => 5 * $subtotal / 100, // Proportion price
);
// Loop by outlined fee Ids array
foreach ( $targeted_payment_ids as $payment_id => $fee_cost ) {
if ( $chosen_payment_id === $payment_id ) {
$cart->add_fee( __('Dealing with price', 'woocommerce'), $fee_cost, true );
}
}
}
// Replace checkout on fee technique change
add_action( 'woocommerce_checkout_init', 'wpsh_refresh_checkout_page' );
perform wpsh_refresh_checkout_page() {
wc_enqueue_js( "jQuery( perform($){
$('kind.checkout').on('change', 'enter[name=payment_method]', perform(){
$(doc.physique).set off('update_checkout');
});
});");
}
How one can add Cost A Gateway Primarily based Low cost in Woocommerce?
The ultimate snippet for right this moment is right here and this time we’ll check out tips on how to add a fee gateway primarily based low cost in Woocommerce. On this instance, we’ll add a 5% low cost (see line 13) provided that Money on supply is chosen (see line 10).
If that you must change the label textual content (COD low cost) then see line 21.
add_action( 'woocommerce_cart_calculate_fees','wpsh_cod_discount', 20, 1 );
perform wpsh_cod_discount( $cart_object ) {
if ( is_admin() && ! outlined( 'DOING_AJAX' ) ) return;
// Outline your personal fee technique right here
$payment_method = 'cod';
// Set your low cost percentagey
$% = 5; // It will give 5% low cost
$cart_total = $cart_object->subtotal_ex_tax;
$chosen_payment_method = WC()->session->get('chosen_payment_method');
if( $payment_method == $chosen_payment_method ){
$label_text = __( "COD Low cost" );
// Calculating proportion
$low cost = number_format(($cart_total / 100) * $%, 2);
// Including the low cost
$cart_object->add_fee( $label_text, -$low cost, false );
}
}
add_action( 'woocommerce_review_order_before_payment', 'wpsh_refresh_checkout' );
perform wpsh_refresh_checkout(){
// jQuery
?>
<script sort="textual content/javascript">
(perform($){
$( 'kind.checkout' ).on( 'change', 'enter[name^="payment_method"]', perform() {
$('physique').set off('update_checkout');
});
})(jQuery);
</script>
// Add Cost A Gateway Primarily based Low cost in Woocommerce?
add_action( 'woocommerce_cart_calculate_fees','wpsh_cod_discount', 20, 1 );
perform wpsh_cod_discount( $cart_object ) {
if ( is_admin() && ! outlined( 'DOING_AJAX' ) ) return;
// Outline your personal fee technique right here
$payment_method = 'cod';
// Set your low cost percentagey
$% = 5; // It will give 5% low cost
$cart_total = $cart_object->subtotal_ex_tax;
$chosen_payment_method = WC()->session->get('chosen_payment_method');
if( $payment_method == $chosen_payment_method ){
$label_text = __( "COD Low cost" );
// Calculating proportion
$low cost = number_format(($cart_total / 100) * $%, 2);
// Including the low cost
$cart_object->add_fee( $label_text, -$low cost, false );
}
}
add_action( 'woocommerce_review_order_before_payment', 'wpsh_refresh_checkout' );
perform wpsh_refresh_checkout(){
// jQuery
?>
<script sort="textual content/javascript">
(perform($){
$( 'kind.checkout' ).on( 'change', 'enter[name^="payment_method"]', perform() {
$('physique').set off('update_checkout');
});
})(jQuery);
</script>
<?php
}
As you see, there isn’t a want for a elaborate plugin. Just a few copy and paste is required to show WooCommerce fee strategies conditionally.