提问者:小点点

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


我已经创建了一个优惠券代码“Tiendas”,用于双倍x2发货价格,并禁用免费默认商店发货(订单)

此外,优惠券允许免费送货,但会将订单价值增加到

我的店铺有3种配送方式:

  • 统一费率:1
  • 统一费率:7
  • 免费送货

启用优惠券时,统一费率:7和免费送货必须隐藏。和统一费率:1应可见,其运输成本为x2(4.80欧元x2=9.60欧元)

仅当我键入flat\u rate,而不是flat\u rate:1,但隐藏所有装运方法而不是一种时才有效。

基于“应用优惠券在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;
    }

共1个答案

匿名用户

实际上,你在那里,你所要做的就是比较$rate\u键,而不是$rate-

您的代码应该如下所示:

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
  }
}