Reputation: 73
I have a use case for a website and I can't figure out how to configure WooCommerce Shipping to do what I want.
Regarding shipping in that store:
I set up two Shipping zones:
I have 2 main problems here:
How can this be solved?
Upvotes: 3
Views: 1047
Reputation: 253901
As you have enabled that free local shipping rate, you will need to set it the code below. It can be a "Free Shipping" method or a "Local Pickup" method (set with 0 cost and no tax).
You will have also to set in the function the array of products IDs that need this shipping method enabled (hiding all others).
add_filter( 'woocommerce_package_rates', 'conditionally_hide_shipping_methods', 10, 2 );
function conditionally_hide_shipping_methods( $rates, $package ) {
// Set HERE the shipping method to use:
// It can be 'free_shipping' or 'local_pickup' (set with 0 cost and no tax)
$shipping_method_id = 'local_pickup';
// Set HERE your "LOCAL free" product IDs
$local_free_ids = array( 40, 37 );
$found = false;
foreach( WC()->cart->get_cart() as $cart_item ) {
if( in_array($cart_item[ 'product_id' ], $local_free_ids) ) { // <== ID OF MY SHIPPING_CLASS
$found = true;
break; // Stop the loop
}
}
// If on of this product Ids are in cart we hide all other shipping methods
if ( $found ){
$local_free = array();
foreach ( $rates as $rate_id => $rate ) {
if ( $shipping_method_id === $rate->method_id ) {
$local_free[ $rate_id ] = $rate;
break;
}
}
return ! empty( $local_free ) ? $local_free : $rates;
} else {
return $rates;
}
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
Sometimes is necessary to refresh the shipping caches:
1) First your cart is empty.
2) The code is already saved on your function.php file.
3) Go in a shipping zone and disable one "flat rate" (for example) and "save". Then re-enable that "flat rate" and "save". You are done.
Based on: WooCommerce - Hide other shipping methods when FREE SHIPPING is available
To notify customers for Local products only, this should be asked in a new question...
Upvotes: 1
Reputation: 65264
These is somewhat debatable but you don't need to code to get this done. As I think you're already there.
I set up two Shipping zones:
Local (only a list of Zip codes) -> Free Rate, Outside Area (everywhere) -> put a bunch of classes such as +10 , +20 , +25 ... and gave them rates
In your Local
and Everywhere
should have same shipping classes but should have different values.
Let us say that in your settings be like this
Zone regions: add list of zip codes or region.
Shipping methods: add a Flat Rate method
Zone regions: add list of zip codes or region or leave it blank.
Shipping methods: add a Flat Rate method
I have 2 main problems here:
- I dont know how to force some products to be local ONLY.
- when doing the setup above, the products that are on the outside area for which I gave a shipping class are not showing in local if someone is trying to checkout from a local Zip code.
Above two images are demonstrating a product that has a "Class A" shipping class.
Upvotes: 1