The code snippets needs to be updated for shipping zones. You can see how to work with rates in shipping zones here: https://docs.woocommerce.com/document/hide-other-shipping-methods-when-free-shipping-is-available/#snippets-for-wc-26
That code will need to be modified of course, both to loop through products, and if it finds one it’s looking for, then hide all but local pickup.
If you aren’t sure how to code this, the Conditional Shipping and Payments extension can also help: http://www.woothemes.com/products/woocommerce-conditional-shipping-and-payments/
Hi Caleb,
Ok, i try customize the code snippet.
Thanks for the links.
Regards,
Christophe
Hi Christophe,
Were you able to customize the code snippet? I also need to force local pickup for a few items. Thank you!
Ian
Hi Ian,
Wow! Thats a few months ago. I’m searching in my archive where i saved my snippets (Google Keep), and find this code:
// Force pickup as a shipping option if one or more products in the ebshop is marked as pickup only.
// To do this, add a shipping class with the slug 'pickup-only' then set products with that class as required.
// Add this script to your theme's functions.php or similar.
function hideShippingWhenPickupRequired($rates, $package)
{
foreach ($package['contents'] as $item) {
$product = $item['data'];
$shippingClass = $product->get_shipping_class();
if ('pickup-only' === $shippingClass) {
// The cart requires pickup
return array(
'local_pickup' => $rates['local_pickup'],
);
}
}
return $rates;
}
add_filter('woocommerce_package_rates', 'hideShippingWhenPickupRequired', 10, 2);
I hope this helps you.
Regards,
Christophe
-
This reply was modified 9 years, 4 months ago by
ctuxboy.
Hi. Thank you very much. But it didn’t work and giving an error
Fatal error: Call to a member function get_label() on null in *********/public_html/wp-content/plugins/woocommerce/includes/wc-cart-functions.php on line 334
I think it is because of the Woocommerce update with Shipping zones?
Anyway, thank you very much!
Oops! I didn’t try this snippet on a newer WC version, there my client wants no more the ‘local pickup’ class, so i removed it (stupid that i didn’t saved!). A time ago i remove this snippet, but i found a lot of very good and updated WC snippets from this website and helps me find out most WC problems:
https://businessbloomer.com/category/woocommerce-tips/
But ni one of the last webshops i develop, adding this snippet:
/**
* @snippet Hide one shipping option in one zone when Free Shipping is available
* @compatible WooCommerce 2.6.1
*/
add_filter( 'woocommerce_package_rates', 'wk_unset_shipping_when_free_is_available_in_zone', 10, 2 );
function wk_unset_shipping_when_free_is_available_in_zone( $rates, $package ) {
// Only unset rates if free_shipping is available
if ( isset( $rates['free_shipping:4'] ) ) {
unset( $rates['flat_rate:2'] ); // shipping method with ID
unset( $rates['service_point_shipping_method:8'] ); // shipping method with ID (BE)
}
return $rates;
}
This is not a solution for your problem, but this snippet is updated for the latetst WC version. What i want try to explain, if you look in this snippet, you see ‘id’ numbers, and thats new and can solve your problem (i think!). The ‘id’ numbers can you see in the HTML via the developer console from your browser.
Hope this brings you on an idea solving the problem?
(Oh my god, sorry for my poor english)
Christophe