免费送货应用在Woocommerce订单中,但不应该

时间:2019-07-31 09:30:22

标签: wordpress woocommerce

我在Woocommerce网站上有一个自定义代码,我在该网站上检查购物车的总大小,如果总大小超过一定数量,我将启用免费送货。如果在下方,我会取消免费送货选项。

问题是代码中的某个地方存在我找不到的漏洞。我收到的订单低于起付额度,并且有免费送货等级,但应该有付费送货等级。我尝试自己复制相同的订单,并且始终获得付款。

这就像在黑暗中拍摄,但我认为问题出在自定义代码上。

add_filter( 'woocommerce_package_rates', 'conditional_free_shipping', 100, 2 );
function conditional_free_shipping( $rates, $package ) {
    // Set the min Order amount for free shipping
    $min_order_amount = 39;    
    $cart_subtotal = (float) WC()->cart->get_subtotal(); // Subtotal excl. taxes

    $free = array();
    $free_key = '';

    // Loop through shipping rates
    foreach ( $rates as $rate_id => $rate ) {
        if ( 'free_shipping' === $rate->method_id ) {

            // Your conditions goes bellow
            if ( $cart_subtotal >= $min_order_amount ) {
                $free[ $rate_id ] = $rate;
            }

            $free_key = $rate_id;
            break;
        }
    }

    // No free shipping (Other shipping methods only)
    if( empty( $free ) ) {
        unset($rates[$free_key]);
        return $rates;
    }
    // Only free shipping
    else
        return $free;
}

0 个答案:

没有答案