隐藏运费统一费率“当Woocommerce免费送货时

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

我是新手,所以如果我发布我的帖子,请指导我...也不是程序员......我尝试了另外两个线程中给出的代码,但它并没有完全解决问题。我从文档中了解到我不是要求对这些线程进行澄清,所以我希望我能开一个​​新的...

网站:www.leannacinquanta.com/shop,Woocommerce插件最新版本。当我将以下两个代码段插入到我的高级Wp主题“X主题”中的自定义CSS区域时,它没有导致我的购物车发生任何更改。我非常感谢你的帮助:

/*WOOCOMMERCE hide"calculate shipping"*/
add_filter('woocommerce_product_needs_shipping', function(){return false;});

/*WOOCOMMERCE remove Flat Rate from view when order is over $50 which qualifies for free shipping */
add_filter('woocommerce_package_rates', 'hide_flat_rate_based_on_cart_total', 10, 3);
function hide_flat_rate_based_on_cart_total( $available_shipping_methods, $package ){
    $price_limit = 50;
    if( WC()->cart->get_total() > $price_limit ){
        foreach($available_shipping_methods as $method_key  =>  $method){
            if ( strpos($method->method_id, 'flat_rate' ) !== false) {
                unset($available_shipping_methods[$method_key]);
            }
        }
    }
    return $available_shipping_methods;
}
php wordpress woocommerce checkout shipping
1个回答
0
投票

您的代码中存在两个问题:

  • 您的第一个功能删除了所有地方的运输(只需要在购物车页面中完成)
  • 第二个功能应该使用购物车小计(不可能在购物车总数上进行,而且无法访问)。

首先,您需要按照所需的限额设置免费送货:

enter image description here

完成后,此代码改为:

// Hide shipping in cart page.
add_filter( 'woocommerce_product_needs_shipping', 'disable_shipping_in_cart_page', 10, 2 );
function disable_shipping_in_cart_page( $needs_shipping, $product ){
    if ( is_cart() )
        $needs_shipping = false;

    return $needs_shipping;
}

// Conditionally remove Flat Rate which qualifies for free shipping
add_filter( 'woocommerce_package_rates', 'hide_flat_rate_when_free_is_available', 10, 3 );
function hide_flat_rate_when_free_is_available( $rates, $package ) {
    $free = array();
    $free_available = false;

    foreach ( $rates as $rate_key => $rate ) {
        if ( 'free_shipping' === $rate->method_id ){
            $free_available = true;
            break;
        }
    }

    foreach ( $rates as $rate_key => $rate ) {
        if ( $free_available && 'flat_rate' === $rate->method_id )
            unset($rates[$rate_key]);
    }

    return $rates;
}

代码位于活动子主题(或活动主题)的function.php文件中。

您应该刷新运输缓存: 1)首先,此代码已保存在function.php文件中。 2)在装运设置中,输入装运区并禁用装运方法并“保存”。然后重新启用“运输方式”并“保存”。你完成了。

而是使用第一个功能,您还可以从购物车页面中删除运费计算器: Woocommerce> Settings> Shipping> shipping options (tab)

代码经过测试和运行。

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