Woocommerce install with Local Pickup Plus plugin. Product A must be picked up and is set to "Must be picked up". Product B is sold on it's own and is also a synced force sell with Product A. It is set to "Can be picked up" and also has flat rate shipping available. If Product B is purchased alone, customers can select pick up or flat rate shipping. When Product B is purchased with Product A, both should be pickup only. However, when Product A and B are in the cart, two shipping methods are shown - local pickup for product A and the flat rate for Product B.
Maybe I have a setting wrong somewhere that I can't find, but when local pickup is selected, no other shipping method should be available. I have tried many variations of code and some work somewhat and others not at all.
This results in WSOD:
add_filter( 'woocommerce_package_rates', 'unset_wc_shipping_methods_when_cat', 10 ,2 );
function unset_wc_shipping_methods_when_cat ( $rates, $package ) {
$product_category = 'birth-pool-hire';
// loop through the cart
foreach ( $cart->get_cart() as $cart_item ) {
if ( has_term( $product_category, 'product_cat', $cart_item['product_id'] ) )
$local_pickup = $rates['local_pickup_plus'];
$rates = array();
$rates['local_pickup_plus'] = $local_pickup_plus;
}
return $rates;
}
This correctly removes the Flat Rate but you can no longer select a pickup location (Both shipping methods appear as "Local pickup: $0.00" and the top shipping method has the select pickup location box missing. https://gist.github.com/rynaldos/9de09f00765c83868fb7fbd731c3aa1b
I have also tried the code here, altering local_pickup to local_pickup_plus but that results in just the pickup location box disappearing - the Shipping 2 flat rate still appears. Remove shipping Flat Rate method for particular Category in WooCommerce 2.6 and 3+
I have tried fiddling with other variations, but all have had the same result - not working at all or making the local pickup location selection box disappear.
All I need is that if local pickup (plus) is selected, no other shipping methods should be present/available. And customers should still be able to select their preferred pickup location. Ripping my hair out on this one and sick to boot so help much appreciated!
UPDATE: The following to sets of code work correctly to somewhat disable the flatrate shipping but still allow local pickup including selecting pickup location.
/**
* Hide shipping rates when local pickup is available.
* Updated to support WooCommerce 2.6 Shipping Zones.
*
* @param array $rates Array of rates found for the package.
* @return array
*/
function wc_hide_shipping_when_pickup_is_available( $rates ) {
$pickup = array();
foreach ( $rates as $rate_id => $rate ) {
if ( 'local_pickup_plus' === $rate->method_id ) {
$pickup[ $rate_id ] = $rate;
break;
}
}
return ! empty( $pickup ) ? $pickup : $rates;
}
add_filter( 'woocommerce_package_rates', 'wc_hide_shipping_when_pickup_is_available', 100 );
Altered from here: https://docs.woocommerce.com/document/hide-other-shipping-methods-when-free-shipping-is-available/
function hide_shipping_method_based_on_shipping_class( $rates, $package )
{
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// HERE define your shipping class to find
$class = 602;
// HERE define the shipping method to hide
$method_key_id = 'flat_rate:1';
// Checking in cart items
foreach( WC()->cart->get_cart() as $cart_item ){
// If we find the shipping class
if( $cart_item['data']->get_shipping_class_id() == $class ){
unset($rates[$method_key_id]); // Remove the targeted method
break; // Stop the loop
}
}
return $rates;
}
add_filter( 'woocommerce_package_rates', 'hide_shipping_method_based_on_shipping_class', 10, 2 );
Altered from: Hide shipping method for specific shipping classes in woocommerce
I say somewhat however because Shipping 2 still remains. Instead, I either get There are no shipping methods available. Please ensure that your address has been entered correctly, or contact us if you need any help.
if an address is entered or Enter your full address to see shipping costs.
if an address is not yet entered.
Shipping 2 should not be appearing at all if pickup is forced for all items in the cart.