• Resolved craigmckee

    (@craigmckee)


    I have this code which works and hides paid shipping option when free shipping is available. How can I change it so that local pick remains too?

    /**
     * Hide shipping rates when free shipping is available.
     *
     * @param array $rates Array of rates found for the package.
     * @return array
     */
    function dokan_vendor_shipping_hide_when_free_is_available( $rates ) {
        $free = array();
        foreach ( $rates as $rate_id => $rate ) {
            if ( 'free_shipping' === $rate->method_id || 'free_shipping' === $rate->id || strpos( $rate->id, 'free_shipping' ) !== false ) {
                $free[ $rate_id ] = $rate;
                break;
            }
        }
        return ! empty( $free ) ? $free : $rates;
    }
    add_filter( 'woocommerce_package_rates', 'dokan_vendor_shipping_hide_when_free_is_available', 100 );
Viewing 9 replies - 1 through 9 (of 9 total)
  • @craigmckee

    This is a fairly complex development topic. I’m going to leave it open for a bit to see if anyone is able to chime in to help you out.
    
    I can also recommend <a href="https://developer.woocommerce.com/" rel="noopener" target="_blank">the WooCommerce Developer Resources Portal</a> for resources on developing for WooCommerce.
    
    You can also visit the <a href="https://www.facebook.com/groups/advanced.woocommerce/" rel="noopener" target="_blank">WooCommerce Facebook group</a> or the <code>#developers</code> channel of the <a href="https://woocommerce.com/community-slack/" rel="noopener" target="_blank">WooCommerce Community Slack</a>. We're lucky to have a great community of open-source developers for WooCommerce, and many of our developers hang out there, as well.

    Hi @craigmckee

    This tutorial shows how to unset a specific shipping method when free shipping is available.

    I hope it helps!

    Thread Starter craigmckee

    (@craigmckee)

    The code I used at the top does hide the paid shipping option, my issue is it also hides the “local pickup” option which I wanted to keep. Is this possible?

    Hi @craigmckee!

    Apparently, what your code does is a loop between all the shipping options, to unset them since the free shipping option is available.

    I’d go with the Maykato link, which explains how to unset a specific shipping option and apply something similar to all the others except for the local pickup.

    If you don’t know how to do that, I’d then recommend Oyadeyi’s reply. You can ask and talk with other developers at the WooCommerce Developer Resources Portal for resources on developing for WooCommerce.

    Also at the WooCommerce Facebook group or the #developers channel of the WooCommerce Community Slack.

    All the best,

    Thread Starter craigmckee

    (@craigmckee)

    Thank you @chiape !

    I appreciate your explanation, I’ll see if I can figure out how to get an if in so that it doesn’t do anything to local pickup ??

    • This reply was modified 2 years, 10 months ago by craigmckee.
    Thread Starter craigmckee

    (@craigmckee)

    @chiape

    I did come across this code but this doesn’t even hide the paid shipping so doesn’t work at all.

    /**
     * Hide shipping rates when free shipping is available, but keep "Local pickup" 
     * Updated to support WooCommerce 2.6 Shipping Zones
     */
    
    function hide_shipping_when_free_is_available( $rates, $package ) {
    	$new_rates = array();
    	foreach ( $rates as $rate_id => $rate ) {
    		// Only modify rates if free_shipping is present.
    		if ( 'free_shipping' === $rate->method_id ) {
    			$new_rates[ $rate_id ] = $rate;
    			break;
    		}
    	}
    
    	if ( ! empty( $new_rates ) ) {
    		//Save local pickup if it's present.
    		foreach ( $rates as $rate_id => $rate ) {
    			if ('local_pickup' === $rate->method_id ) {
    				$new_rates[ $rate_id ] = $rate;
    				break;
    			}
    		}
    		return $new_rates;
    	}
    
    	return $rates;
    }
    
    add_filter( 'woocommerce_package_rates', 'hide_shipping_when_free_is_available', 10, 2 );

    Hello there,

    As far as I understand, you would like to keep local pick up when there is free shipping available, is this correct?

    If so, please check this documentation:
    https://woocommerce.com/document/hide-other-shipping-methods-when-free-shipping-is-available/#section-5

    I hope this leads you in the right direction.

    Thread Starter craigmckee

    (@craigmckee)

    @ihereira thank you for that. I had used that link and tried that code but it doesn’t work at all. I don’t know why thought – it doesn’t hide anything at all. ??

    Mirko P.

    (@rainfallnixfig)

    Hi @craigmckee,

    In that case, since we are unable to provide support for customizations per our Support Policy, we’d recommend hiring a developer or one of the customization experts listed at https://woocommerce.com/customizations/.

    Thanks.

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Hide Paid Shipping but keep Local Pickup’ is closed to new replies.