Скрыть указанные c способы доставки при использовании купона - PullRequest
2 голосов
/ 22 января 2020

Я создал код купона "Tiendas" для двойной стоимости доставки x2 и отключил бесплатную доставку в магазин по умолчанию (заказы> 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, но вместо одного скрываем все методы доставки.

На основе " Отключение примененных купонов Условно бесплатная доставка в 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 Ответ

1 голос
/ 22 января 2020

На самом деле вы там, все, что вам нужно сделать, это сравнить $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
  }
}
...