Reputation: 3197
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
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:
For the "Store pick up" ("Local pickup" shipping method):
You will set a cost of 0:
Then you will get in cart and checkout something like:
Upvotes: 3