首页 文章

在Woocommerce中将所有送货方式成本设置为零,以获得免费送货优惠券

提问于
浏览
2

我的购物车中有3种送货方式,只要您的客户输入免费送货优惠券就应该变为零价格 .

我知道如何在functions.php中添加一个过滤器来检测优惠券,但有人知道一个片段,可以将购物车中的运送方式(单选按钮)设置为ZERO以获取此订单吗?

我的交付方式是UPS,FedEx等公司......

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

根据我的产品运输类别和订单总重量计算客户交付方法的选择列表 .

装运等级不是按计算出来的,而是按产品设定的,我使用TABLE RATE PLUGIN来计算重量 .

非常感谢

2 回答

  • 0

    首先免费送货方式必须启用选项“有效免费送货优惠券”...

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

    当应用有效的优惠券代码(启用选项"Allow free shipping")时,以下代码会将所有送货方式成本设置为 zero .

    更新:隐藏“免运费”方法并附加“(免费)”标签 Headers

    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)转到装运区域设置,然后禁用/保存并重新启用/保存相关的装运方法 .

  • 4

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

    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之前,发货方法"Free Shipping"的详细信息存储在数组元素 $rates['free_shipping'] 下 . 但是,现在它存储为 $rates['free_shipping:shipping_zone_instance_id'] ,其中 shipping_zone_instance_id 是送货方式的送货区实例ID . 您可以通过检查管理面板中的"Free Shipping",或者在新选项卡中打开它并查看网址http://prntscr.com/jd2zjy来检查送货方式的实例ID .

相关问题