0

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.

https://i.imgur.com/DYXMvW9.png

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.

Rose Thorn
  • 179
  • 3
  • 16
  • I am now wondering if this is actually a bug with Woocommerce because even if I set Product B to “Must be picked up” Shipping 2 still appears but now has “There are no shipping methods available. Please ensure that your address has been entered correctly, or contact us if you need any help.” beside it. – Rose Thorn Sep 08 '18 at 00:57
  • There definitely appears to be something buggy going on. I deleted Product B completely and recreated it from scratch as I noticed two other products that I have Product 1 and Product 2 did not seem to have the same issues. Product 2 is almost the same as Product B but is sold with Product 1. Originally I had duplicated Product B from Product 2. However, even recreating Product B from scratch is still having most of the same problems. Product 2 on its own correctly shows the choice of shipping or pickup. Product B on its own is only allowing flat rate despite being set to "Can be picked up". – Rose Thorn Sep 08 '18 at 02:25
  • Even setting Product B to "Must be picked up" is not allowing pickup. In the cart, selecting pickup reverted back to flat rate after ajax loading. So I cleared Transients again (which I have done between each change and every change), cleared cache and customer sessions. I then readded Product B, was no longer able to select pickup or shipping in the cart and in the checkout, only flat rate was available with no option to change. – Rose Thorn Sep 08 '18 at 02:31

0 Answers0