'Disable COD when exceeded specific amount
I need to disable Woocommerce COD option and make it unchecked when total price exceeded certain amount.
I tried this code, but does nothing!
add_filter('woocommerce_available_payment_gateways', 'unsetting_payment_gateway', 10, 1);
function unsetting_payment_gateway( $available_gateways ) {
global $woocommerce;
$shipping_cost = WC()->cart->get_cart_shipping_total();
$amount = $woocommerce->cart->cart_contents_total + $woocommerce->cart->tax_total + $shipping_cost;
$max = 999.9 * WCPBC()->customer->exchange_rate;
if($amount >= $max){ ?>
<script type="text/javascript">
jQuery(document).ready(function ($) {
$("#payment_method_cod").disabled = true;
$("#payment_method_cod").checked = false;
});
</script>
<?php
add_action( 'woocommerce_review_order_before_payment', 'COD_exceed_amount_before_paying_notice' );
}
return $available_gateways;
}
function COD_exceed_amount_before_paying_notice() {
wc_print_notice( __( 'COD amount exceeded!', 'woocommerce' ), 'notice' );
}
Solution 1:[1]
add_filter( 'woocommerce_available_payment_gateways' , 'hide_payment_gateway', 20, 1);
/**
* remove cod gateway if cart total > 100
* @param $gateways
* @return mixed
*/
function hide_payment_gateway( $gateways ){
//change whatever amount you want
if( WC()->cart->subtotal < 699 ){
// then unset the 'cod' key (cod is the unique id of COD Gateway)
unset( $gateways['cod'] );
add_action( 'woocommerce_review_order_before_payment', 'COD_exceed_amount_before_paying_notice' );
}
return $gateways;
}
function COD_exceed_amount_before_paying_notice() {
wc_print_notice( __( 'COD option not available on orders below 699.00', 'woocommerce' ), 'notice' );
}
Solution 2:[2]
First you need to deregister the woocommerce/assets/js/frontend/checkout.js
Then include checkout.js in your theme assets folder and register it again after theme script files
then your code will work as you expect.
for deregister use below code,
wp_deregister_script( $handle );
again register by,
wp_enqueue_script(parameters);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | baiju jha |
