Enabling and disabling shipping method options based on whether the user is logged into their account is easy. Having this code snippet implemented can encourage the user to register an account with your store if it means they get an exclusive shipping option. This guide shows you how you can enable free shipping only for users which are logged in to their account. That means users have have not registered an account and logged in will not see the free shipping option at the checkout. What better incentive to get your users to register with your online store? The only thing you’ll need to tweak in this code is the shipping method radio button value which is explained further in the guide.
/**
* Snippet Name: WooCommerce disable shipping method option if user is not logged in. In this example, enable free shipping only if the user is logged in.
* Snippet Author: ecommercehints.com
*/
add_filter( 'woocommerce_package_rates', 'ecommercehints_disable_shipping_method_based_on_login_status', 10, 2 );
function ecommercehints_disable_shipping_method_based_on_login_status($rates, $package) {
if (!is_user_logged_in()) {
unset( $rates['free_shipping:14'] ); // The shipping method radio button value
}
return $rates;
}
How Do I Get The Shipping Method Radio Button Value?
Use your browser’s developer tools (usually F12 on the keyboard) then use the inspect element tool and click on the radio button you’d like to enable/disable. Expand the list until you see the value you’d like. It typically includes a colon and a number as shown in the image.
