在 Woocommerce 中隐藏基于总重量的特定支付方式

Hide specific payment method based on total weight in Woocommerce

在 woocommerce 中,我想隐藏特定总重量的 "Cash on delivery" 付款方式。

例如,如果购物车总重量不超过 15 公斤,"Cash on delivery" 结帐时的付款方式。

感谢任何帮助。

如果购物车总重量达到 15 Kg,以下简单代码将隐藏“COD”付款方式。
我假设 Woocommerce 中设置的重量单位是 Kg (kilos).

代码:

add_filter( 'woocommerce_available_payment_gateways', 'hide_payment_gateways_based_on_weight', 11, 1 );
function hide_payment_gateways_based_on_weight( $available_gateways ) {
    if ( is_admin() ) return $available_gateways; // Only on frontend

    $total_weight = WC()->cart->get_cart_contents_weight();

    if( $total_weight >= 15 && isset($available_gateways['cod']) )
        unset($available_gateways['cod']); // unset 'cod'

    return $available_gateways;
}

代码进入您的活动子主题(或活动主题)的 functions.php 文件。测试和工作。