在 WooCommerce 3 中提供免费送货时隐藏特定的统一费率

Hide specifics Flat Rates when Free Shipping is available in WooCommerce 3(在 WooCommerce 3 中提供免费送货时隐藏特定的统一费率)
本文介绍了在 WooCommerce 3 中提供免费送货时隐藏特定的统一费率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

在 WooCommerce 3 中,我有以下运输选项(设置):

In WooCommerce 3, I have these shipping options (settings):

  1. 免运费:free_shipping:1 - 最低订单金额设为 $50.
  2. 正常运输 flat_rate:3 - 金额 $5.
  3. 快递flat_rate:5 - 金额$10.
  1. Free Shipping: free_shipping:1 - Minimum order amount is set at $50.
  2. Normal Shipping flat_rate:3 - Amount $5.
  3. Express Shipping flat_rate:5 - Amount $10.

我希望 快递 选项始终可用(如图所示).

I would like Express Shipping option to be always available (shown).

但是当免费送货可用时(意味着客户在购物车中的金额超过 50 美元),我只想隐藏正常送货.

But when Free shipping is available (meaning that the customer has more than $50 in the cart) I would like to hide Normal Shipping only.

因此,当免费送货可用(和隐藏)时,可用的运费将为正常送货 和快递.

So when Free shipping is NOT available (and hidden), the available shipping rates will be Normal Shipping and Express Shipping.

这可能吗?我怎样才能在 WooCommerce 3 上获得这个?

Is that possible? How can I get this on WooCommerce 3?

推荐答案

基于 WooCommerce 官方代码段,做了一些小改动,当免费送货可用时,您将只能隐藏您的第一个统一费率:

Based on the official WooCommerce snippet code, making some light changes, you will be able to hide only your first flat rate when free shippings is available:

add_filter( 'woocommerce_package_rates', 'conditionally_hide_shipping_methods', 100, 2 );
function conditionally_hide_shipping_methods( $rates, $package ) {
    // HERE yours 2nd flat rate "Express Shipping" (that you never hide) in the array:
    $flat_rates_express = array( 'flat_rate:5', 'flat_rate:12', 'flat_rate:14' );

    $free = $flat2 = array();
    foreach ( $rates as $rate_key => $rate ) {
        // Updated Here To 
        if ( in_array( $rate->id, $flat_rates_express ) )
            $flat2[ $rate_key ] = $rate;
        if ( 'free_shipping' === $rate->method_id )
            $free[ $rate_key ] = $rate;
    }
    return ! empty( $free ) ? array_merge( $free, $flat2 ) : $rates;
}

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

在 WooCommerce 3 上测试并有效.

Tested on WooCommerce 3 and works.

刷新运输缓存:
1) 首先清空您的购物车.
2) 此代码已保存在您的 function.php 文件中.
3) 进入运输区域设置并禁用一个统一费率"(例如) 和保存".然后重新启用统一费率"和保存".大功告成,可以进行测试了.

Refresh the shipping caches:
1) First empty your cart.
2) This code is already saved on your function.php file.
3) Go in a shipping zone settings and disable one "flat rate" (for example) and "save". Then re-enable that "flat rate" and "save". You are done and you can test it.

这篇关于在 WooCommerce 3 中提供免费送货时隐藏特定的统一费率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)