Marcus Christiansen
Marcus Christiansen

Reputation: 3197

Using woocommerce_calculate_totals hook to set "Local pickup" shipping cost to 0

We have modified the checkout process in Woocommerce to allow the customer to select between delivery or store pickup.

I'm using the woocommerce_calculate_totals to check which method was selected by the customer and if the user selected store pick up I want to set the shipping fee to 0

I'm using the following code.

add_action('woocommerce_calculate_totals', function($order) {
    /* 
     * We need to check if the user selected local delivery, which would be added to the request string 
     * If no store pick-up is selected, then we don't need to display shipping calculations
     */ 
    global $woocommerce;

    if(!empty($_POST['post_data'])) {
        if(strpos($_POST['post_data'], 'local-delivery')) {

        } else {
            $woocommerce->cart->shipping_total = 0.00;
            $woocommerce->cart->shipping_tax_total = 0.00; 
        } 
    }
}, 40, 1);

If a user selects local delivery, it calculates shipping costs. However, if a user switches back to pick up, Woocommerce does not remove the shipping cost.

Is there anything I'm missing?

Upvotes: 1

Views: 1352

Answers (1)

LoicTheAztec
LoicTheAztec

Reputation: 253784

You don't need any code for that… It can be done through WooCommerce settings. So you will have 2 shipping methods in your shipping settings for a shipping Zone:

  • A "Flat rate" shipping method for "Local delivery" (with a cost)
  • A "Local pickup" shipping method for "Store pick up" (without cost)

For the "Store pick up" ("Local pickup" shipping method):

enter image description here

You will set a cost of 0:

enter image description here

Then you will get in cart and checkout something like:

enter image description here

Upvotes: 3

Related Questions