Created
July 23, 2014 05:40
-
-
Save kane-c/5e4d53601564ed667fb9 to your computer and use it in GitHub Desktop.
WordPress WooCommerce: Force local pickup as the only shipping option when products in the cart require it.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Force pickup as a shipping option if one or more products in the catalog 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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here's my solution: for carts with one or more products belonging to the shipping class "Local Pickup Only" (
$shipping_class_name = 'local-pickup-only'
), only the local pickup location(s) (from the Local Pickup in Blocks) will appear.Tested with WordPress 6.5.2 and WooCommerce 8.7.0.
Important: "Clear customer sessions" after implementation (WooCommerce > Status > Tools > Clear customer sessions).
For those using the "legacy" local pickup, the code below accomplishes the same goal.