应用优惠券时隐藏特定的送货方式

我为双倍x2的运费创建了优惠券代码“ Tiendas”,并禁用了免费的默认商店运费(订单> 50€)

此外,优惠券可实现免费送货,但订单价值增加至> 250欧元。

我的商店有3种送货方式:

  • flat_rate:1
  • flat_rate:7
  • Free_shipping

启用优惠券后,flat_rate:7和免费送货必须隐藏。并且flat_rate:1应该是可见的,其运输成本为x2(4.80€x 2 = 9.60€)

仅当我键入flat_rate而不是flat_rate:1时才有效,但是隐藏了所有送货方式而不是其中一种。

基于Applied coupons disable Free shipping conditionally in Woocommerce答案线程,这是我的代码尝试:

 add_filter( 'woocommerce_package_rates','coupons_removes_free_shipping',10,2 );
function coupons_removes_free_shipping( $rates,$package ){
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return $rates;

    $min_subtotal      = 250; // Minimal subtotal allowing free shipping

    // Get needed cart subtotals
    $subtotal_excl_tax = WC()->cart->get_subtotal();
    $subtotal_incl_tax = $subtotal_excl_tax + WC()->cart->get_subtotal_tax();
    $discount_excl_tax = WC()->cart->get_discount_total();
    $discount_incl_tax = $discount_total + WC()->cart->get_discount_tax();

    // Calculating the discounted subtotal including taxes
    $discounted_subtotal_incl_taxes = $subtotal_incl_tax - $discount_incl_tax;

    $applied_coupons   = WC()->cart->get_applied_coupons();
    if( in_array( 'tiendas',$applied_coupons ) && sizeof($applied_coupons) > 0 && $discounted_subtotal_incl_taxes < $min_subtotal ){
        foreach ( $rates as $rate_key => $rate ){

            // Set 2x cost"
            if( $rate->method_id === 'flat_rate:1' ){
                // Set 2x of the cost
                $rates[$rate_key]->cost = $rates[$rate_key]->cost * 2;}

            // Disable "flat_rate:7"
            if( $rate->method_id === 'flat_rate:7'  ){
                unset($rates[$rate_key]);


            // Disable "Free shipping"
            if( 'free_shipping' === $rate->method_id  ){
                unset($rates[$rate_key]);


            }
        }
    }
    }
    return $rates;
    }
kangzilong 回答:应用优惠券时隐藏特定的送货方式

实际上,您在那里,所要做的就是比较$rate_key而不是$rate->mothod_id

您的代码应如下所示:

if( in_array( 'tiendas',$applied_coupons ) && sizeof($applied_coupons) > 0 && $discounted_subtotal_incl_taxes < $min_subtotal ){
        foreach ( $rates as $rate_key => $rate ){

            // Set 2x cost"
            if( $rate_key === 'flat_rate:1' ){
                // Set 2x of the cost
                $rates[$rate_key]->cost = $rates[$rate_key]->cost * 2;
            }

            // Disable "flat_rate:7"
            if( $rate_key === 'flat_rate:7'  ){
                unset($rates[$rate_key]);
            }


            // Disable "Free shipping"
            if( 'free_shipping' === $rate_key  ){
                unset($rates[$rate_key]);
            }
        }
    }

或者,更简单一些:

if( in_array( 'tiendas',$applied_coupons ) && sizeof($applied_coupons) > 0 && $discounted_subtotal_incl_taxes < $min_subtotal ){
  unset($rates['flat_rate:7']);
  unset($rates['free_shipping']);

  foreach ( $rates as $rate_key => $rate ){
            if( $rate_key === 'flat_rate:1' ) $rates[$rate_key]->cost = $rates[$rate_key]->cost * 2;                 // Set 2x of the cost
  }
}
本文链接:https://www.f2er.com/2736091.html

大家都在问