【问题标题】:Set all shipping methods cost to zero for a Free shipping coupon in Woocommerce在 Woocommerce 中将免费送货优惠券的所有送货方式成本设置为零
【发布时间】:2018-07-17 10:01:27
【问题描述】:

我的购物车中有 3 种送货方式,一旦您的客户输入免费送货优惠券,它们就会变成零价格。

我知道如何在functions.php 中添加过滤器来检测优惠券,但是有人知道sn-p 可以将购物车中可见的运输方式(单选按钮)设置为零吗?

我的送货方式是 UPS、FedEx 等公司...

我激活了免费送货选项,以便可以使用优惠券进行管理。

客户选择的送货方式列表是根据我的产品运输类别和订单总重量计算的。

运费等级不计算,而是按产品设置,我使用TABLE RATE PLUGIN 来计算重量。

非常感谢

【问题讨论】:

  • 谢谢,但我想要的恰恰相反......如果我的优惠券启用免费送货方式,我不能让客户选择送货方式..
  • 当送货方式显示在购物车中时,邀请客户使用单选按钮选择一个,如果应用优惠券,我需要将其所有价格设置为 0

标签: php wordpress woocommerce shipping coupon


【解决方案1】:

必须使用“有效的免费送货优惠券”选项启用第一种免费送货方式……

然后您需要设置所需的优惠券代码并启用“允许免费送货”选项。

当应用有效的优惠券代码(启用“允许免费送货”选项)时,以下代码会将所有送货方式的费用设置为

更新:隐藏“免费送货”方式并在运输标签标题后附加(free)

add_filter( 'woocommerce_package_rates', 'coupon_free_shipping_customization', 20, 2 );
function coupon_free_shipping_customization( $rates, $package ) {
    $has_free_shipping = false;

    $applied_coupons = WC()->cart->get_applied_coupons();
    foreach( $applied_coupons as $coupon_code ){
        $coupon = new WC_Coupon($coupon_code);
        if($coupon->get_free_shipping()){
            $has_free_shipping = true;
            break;
        }
    }

    foreach( $rates as $rate_key => $rate ){
        if( $has_free_shipping ){
            // For "free shipping" method (enabled), remove it
            if( $rate->method_id == 'free_shipping'){
                unset($rates[$rate_key]);
            }
            // For other shipping methods
            else {
                // Append rate label titles (free)
                $rates[$rate_key]->label .= ' ' . __('(free)', 'woocommerce');

                // Set rate cost
                $rates[$rate_key]->cost = 0;

                // Set taxes rate cost (if enabled)
                $taxes = array();
                foreach ($rates[$rate_key]->taxes as $key => $tax){
                    if( $rates[$rate_key]->taxes[$key] > 0 )
                        $taxes[$key] = 0;
                }
                $rates[$rate_key]->taxes = $taxes;
            }
        }
    }
    return $rates;
}

代码进入您的活动子主题(或活动主题)的 function.php 文件中。

经过测试并且有效。它应该也适合你。

有时,您可能需要刷新运输方式
1) 先清空购物车。
2) 前往配送区域设置,然后禁用/保存并重新启用/保存相关配送方式。

【讨论】:

  • @Pipoo 代码测试和更新...一旦保存此代码,请刷新最后解释的运输方式...
【解决方案2】:

如果你想达到同样的效果,但只要有可用的免费送货方式(不仅应用了优惠券,而且购物车总价高于特定价格),你可以使用这个:

add_filter( 'woocommerce_package_rates', 'wc_apply_free_shipping_to_all_methods', 10, 2 );
function wc_apply_free_shipping_to_all_methods( $rates, $package ) {
  if( isset( $rates['free_shipping:11'] ) ) { 
    unset( $rates['free_shipping:11'] );
    foreach( $rates as $rate_key => $rate ) { 
                // Append rate label titles (free)
                $rates[$rate_key]->label .= ' ' . __('(free)', 'woocommerce');

                // Set rate cost
                $rates[$rate_key]->cost = 0;

                // Set taxes rate cost (if enabled)
                $taxes = array();
                foreach ($rates[$rate_key]->taxes as $key => $tax){
                    if( $rates[$rate_key]->taxes[$key] > 0 )
                        $taxes[$key] = 0;
                }
                $rates[$rate_key]->taxes = $taxes;
    }   
  }
  return $rates;
}

注意,在free_shipping 之后有一个:11。在 WooCommerce 2.6 之前,送货方式“免费送货”的详细信息存储在数组元素 $rates['free_shipping'] 下。但是,现在它存储为$rates['free_shipping:shipping_zone_instance_id'],其中shipping_zone_instance_id 是运输方式的运输区域实例ID。您可以通过检查管理面板中的“免费送货”或在新选项卡中打开它并查看 url http://prntscr.com/jd2zjy 来检查送货方式的实例 ID。

【讨论】:

  • 这不是感谢 sn-p 的一种方式……您的回答不是在这里回答最初的问题,应该删除。它严格基于我的代码,没有优惠券部分,这是在这个问题中提出的。这真的不公平。你应该问一个新问题……
  • @LoicTheAztec,我认为有人会欣赏更通用的解决方案。这是不是很明显这是您的代码但经过调整?你想让我在我的回答中标记你吗?我真的没有任何不尊重的意思
  • 您在此处的已删除答案中提出了问题:“感谢您提供这么酷的 sn-p!您能否告诉我如何编辑此代码以在超过特定购物车总价的情况下免运费?” 所以这是在回答你的问题……但它不是在回答这个线程中提出的问题……if( isset( $rates['free_shipping:11'] ) ) { unset( $rates['free_shipping:11'] ); 不是通用的,它与你的具体问题有关……
猜你喜欢
  • 2014-03-03
  • 1970-01-01
  • 1970-01-01
  • 2021-10-28
  • 2021-01-05
  • 2020-12-03
  • 1970-01-01
  • 2021-02-24
相关资源
最近更新 更多