在 WooCommerce 中隐藏一定金额以下订单的 COD 的最佳方法是什么?

问题描述 投票:0回答:1

当订单金额低于 10 美元时,我尝试禁用/删除 WooCommerce 提供的货到付款选项

我正在使用的脚本正在运行,事实上它隐藏了结账页面中的选项,但我收到了这样的错误:

[STDERR] PHP 警告:尝试在

中读取 null 属性“小计”
add_filter( 'woocommerce_available_payment_gateways', 'ts_disable_cod' );
function ts_disable_cod( $available_gateways ) {
    $minimum = 11;
    if ( WC()->cart->subtotal < $minimum ) {
        unset( $available_gateways['cod'] );
    }
    return $available_gateways;
}

实现这一目标的最佳实践是什么?我尝试过 WC()->cart->get_cart_subtotal() 但我再次看到不同的错误集。

我在上面列出了我尝试做的事情

php woocommerce hook-woocommerce checkout payment-method
1个回答
0
投票

您需要专门定位结账页面,以避免警告或错误。尝试以下操作:

add_filter( 'woocommerce_available_payment_gateways', 'ts_disable_cod' );
function ts_disable_cod( $available_gateways ) {
    // Target Checkout to avoid warnings or errors
    if( is_checkout() && ! is_wc_endpoint_url() ) {
        if ( WC()->cart->subtotal <= 10 ) {
            unset( $available_gateways['cod'] );
        }
    }
    return $available_gateways;
}

它应该可以正常工作。

© www.soinside.com 2019 - 2024. All rights reserved.