根据WooCommerce中的购物车小计将所有运输方式成本设置为零 [英] Set all shipping methods costs to zero based on cart items subtotal in WooCommerce

查看:33
本文介绍了根据WooCommerce中的购物车小计将所有运输方式成本设置为零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的页面上有多种送货方式,当订单金额超过2500捷克克朗时,我希望将其费用降为零.我只发现代码仅在客户达到免费送货最低价时才显示免费送货选项,但这不是我想要的..我想显示所有方法(但价格为零),以便客户仍然可以选择哪种送货方式他希望与他的订单一起交付的公司.谢谢!

i have multiple shipping methods on my page and want to make them cost zero when the order is above 2500 Czech crowns. I only found the code to only show the Free shipping option when the customer hits the minimum for free shipping but that is not what I want.. I want to show all the methods (but with zero price) so customer still can choose which shipping company he wants his order to be delivered with. Thank you!

推荐答案

基于

Based on Set cart shipping total amount issue after Woocommerce update answer code, here is the way to make it work based on specific cart items subtotal amount:

add_filter('woocommerce_package_rates', 'null_shipping_costs_conditionally', 10, 2 );
function null_shipping_costs_conditionally( $rates, $package ){
    $threshold_amount = 2500;
    
    if( $package['contents_cost'] >= $threshold_amount ) {
        // Loop through shipping methods rates
        foreach ( $rates as $rate_key => $rate ){
            // Targeting all shipping methods except "Free shipping"
            if ( 'free_shipping' !== $rate->method_id ) {
                $has_taxes = false;
                $taxes = [];
    
                $rates[$rate_key]->cost = 0; // Set cost to 0 (zero)
                // Taxes rate cost (if enabled)
                foreach ($rates[$rate_key]->taxes as $key => $tax){
                    if( $tax > 0 ){
                        $has_taxes = true;
                        $taxes[$key] = 0; // Set tax cost to 0 (zero)
                    }
                }
                if( $has_taxes )
                    $rates[$rate_key]->taxes = $taxes;
            }
        }
    }
    return $rates;
}

代码进入活动子主题(或活动主题)的functions.php文件中.经过测试,可以正常工作.

Code goes in functions.php file of the active child theme (or active theme). Tested and works.

注意:保存此代码后,请不要忘记清空购物车以刷新运送的缓存数据.

Note: After saving this code, don't forget to empty your cart to refresh shipping cached data.

这篇关于根据WooCommerce中的购物车小计将所有运输方式成本设置为零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆