【问题标题】:Hide shipping Flat rate" when free shipping is available in Woocommerce当 Woocommerce 中提供免费送货时,隐藏运费统一费率”
【发布时间】:2018-08-09 01:43:46
【问题描述】:

我是新手,所以如果我发帖的方式有问题,请指导我……我也不是程序员……我尝试了其他两个线程中给出的代码,但它并没有完全奏效。我从文档中了解到,我不是要对这些线程进行澄清,所以我希望我可以打开一个新线程...

网站:www.leannacinquanta.com/shop,Woocommerce 插件最新版本。 当我将以下两个代码 sn-ps 插入到我的高级 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;
}

【问题讨论】:

  • 嘿,你需要在主题functions.php中添加你的代码

标签: php wordpress woocommerce checkout shipping


【解决方案1】:

您的代码中有 2 个问题:

  • 您的第一个函数会删除随处发货(只需要在购物车页面中完成的地方)
  • 第二个功能应该需要使用购物车小计(无法在购物车总计中使用它,并且无法访问)。

首先,您需要将免费送货设置为您想要的限额:

完成后,改用这段代码:

// 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)

代码已经过测试并且可以工作。

【讨论】:

    猜你喜欢
    • 2018-03-05
    • 2015-08-14
    • 2019-05-31
    • 1970-01-01
    • 1970-01-01
    • 2020-11-05
    • 2020-06-22
    • 2016-11-11
    相关资源
    最近更新 更多