M
M
Maxim Borisenko2021-03-22 19:34:26
WordPress
Maxim Borisenko, 2021-03-22 19:34:26

How to add Woocommerce shipping zones?

There is an online store, it works on woocommerce, as planned, there is a "Delivery" item in the basket, which displays two delivery methods "Pickup", "Courier delivery".

By clicking on "self-delivery", a 5% discount is given on the total amount. How to implement this functionality? I’m already confused about something, either create my own delivery zones instead of countries, then they will have different delivery methods (which is not desirable), or create the “Russia” zone, and then how to make it so that when choosing “Courier delivery” it would drop out select with delivery zones and when choosing a zone (district of the city), the amount of delivery was automatically calculated. Does anyone have an answer? At least a hint of it .. Thank you!
By clicking on "Courier delivery", a select with delivery zones (custom) is displayed, when you select any zone, the cost of delivery is calculated (the amounts we know in advance).

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Borisenko, 2021-04-05
@makbori

Self-pickup discount implemented like this

function woo_discount_total(WC_Cart $cart) {

  global $woocommerce;

  $delivery_name = "";
  $delivery_selected = [];
  $available_methods = $woocommerce->shipping->get_packages();

  if(isset($woocommerce->session)) {

    $delivery_selected = $woocommerce->session->get("chosen_shipping_methods");

  }

  foreach($available_methods as $method) {
    
    foreach($delivery_selected as $delivery) {
      
      if(isset($method["rates"][$delivery])) {

        $delivery_name = $method["rates"][$delivery]->label;

        break;

      }
    
    }

  }

  if($delivery_name == "Самовывоз") {

    $discount = $cart->subtotal * 0.05; // 0.05 - это 5%

    $cart->add_fee("Фиксированная скидка в 5% за самовывоз ", -$discount);

  }

}

add_action("woocommerce_cart_calculate_fees" , "woo_discount_total");

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question