超过特定数量时禁用 COD

Disable COD when exceeded specific amount

我需要禁用 Woocommerce COD 选项,并在总价超过一定金额时取消选中它。

我试过这段代码,但什么也没做!

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' );
}

首先你需要注销 woocommerce/assets/js/frontend/checkout.js

然后将 checkout.js 包含在您的主题资产文件夹中,并在主题脚本文件后重新注册

那么您的代码将按预期工作。

注销使用下面的代码,

wp_deregister_script($句柄);

再次注册,

wp_enqueue_script(参数);

您应该更改 jquery 代码。你能试试下面的代码并再次检查你的代码吗?因为这对我有用。

<script type="text/javascript">
        jQuery( document ).ready(function($) {          
          $("#payment_method_cod").prop('checked', false);                 
          $("#payment_method_cod").attr('disabled', true);        
        });       
    </script>    
 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' );
}