• Resolved preetindersodhi

    (@preetindersodhi)


    Hello,

    I want to disable Paypal Checkout when there is no available shipping method. It is for a restaurant’s website. Local pickup option is available for only selected postal codes. But user is able to pay even if the user is from other postal code & no shipping method is available.

    How to fix this issue?

Viewing 8 replies - 1 through 8 (of 8 total)
  • Plugin Author Payment Plugins

    (@mrclayton)

    Hi @preetindersodhi

    How to fix this issue?

    As with most custom business cases, you can use the provided filters from WooCommerce to achieve the desired functionality.

    If you want to hide PayPal as a payment option if there are no shipping methods then you can add some simple PHP that filters it based on the shipping method availability.

    Example:

    add_filter('woocommerce_available_payment_gateways', function($gateways){
    	if(is_checkout()){
    		$packages = WC()->shipping()->get_packages();
    		$has_methods = array_reduce($packages, function($has_methods, $package){
    			if(!$has_methods){
    				return !empty($package['rates']);
    			}
    			return $has_methods;
    		}, false);
    		if(!$has_methods){
    			unset($gateways['ppcp']);
    		}
    	}
    	return $gateways;
    });

    Kind Regards

    Thread Starter preetindersodhi

    (@preetindersodhi)

    Thanks a lot, it works.

    Another solution: copy templates/checkout/payment.php in the theme folder & modify the code as per following. Thanks to chatgpt.

    
    function has_available_shipping_methods_for_address() {
        if (!function_exists('WC') || !WC()->cart) {
            return false;
        }
    
        // Update the customer's shipping address from the checkout form to the current session.
        WC()->customer->set_props(array(
            'shipping_country'   => WC()->checkout->get_value('shipping_country'),
            'shipping_state'     => WC()->checkout->get_value('shipping_state'),
            'shipping_postcode'  => WC()->checkout->get_value('shipping_postcode'),
            'shipping_city'      => WC()->checkout->get_value('shipping_city'),
            'shipping_address_1' => WC()->checkout->get_value('shipping_address_1'),
            'shipping_address_2' => WC()->checkout->get_value('shipping_address_2')
        ));
        WC()->customer->save();
    
        // Recalculate the shipping after updating the address.
        WC()->cart->calculate_shipping();
    
        $packages = WC()->shipping->get_packages();
    
        foreach ($packages as $i => $package) {
            if (!isset($package['rates']) || empty($package['rates'])) {
                return false;  // No shipping methods available for the address
            }
        }
    
        return true;  // Shipping methods are available for the address
    }
    
    
    
    <style>#payment.no-shipping {display:none;}</style>
    
    <div id="payment" class="woocommerce-checkout-payment<?php if (has_available_shipping_methods_for_address()) {echo ' shipping-available ';} else {echo ' no-shipping ';} ?>">
    
    
    Plugin Author Payment Plugins

    (@mrclayton)

    Another solution:

    To any future users reading this thread, I don’t recommend the chatgpt suggestion. If you don’t need to modify the WooCommerce templates, then don’t.

    The chatgpt suggestion also adds unnecessary complexity.

    Thread Starter preetindersodhi

    (@preetindersodhi)

    functions.php solution is quicker & better.

    Shouldn’t PayPal checkout be disabled by default if no shipping method is available? One shouldn’t have to add this extra code.

    Plugin Author Payment Plugins

    (@mrclayton)

    Shouldn’t PayPal checkout be disabled by default if no shipping method is available?

    No. There are use cases where shipping isn’t available but a merchant still wants to process the payment. We try not to make sweeping generalizations about how a merchant chooses to take a payment.

    Thread Starter preetindersodhi

    (@preetindersodhi)

    An option to enable/disable this would be good.

    Show PayPal button even if no shipping method is available: Yes/No.

    Plugin Author Payment Plugins

    (@mrclayton)

    But user is able to pay even if the user is from other postal code & no shipping method is available.

    WooCommerce already validates that there is a selected shipping method before the plugin is called to process the payment. If there are no shipping methods then WooCommerce will display a notice on the checkout page. It’s not possible to process a payment in WooCommerce if a shipping method isn’t selected. Here is the WooCommerce code that validates that a shipping method is selected.

    The PayPal plugin will also display a notice, depending on your settings, in the PayPal popup that says Store doesn't ship to this location. Please use a different address.

    Kind Regards

    Thread Starter preetindersodhi

    (@preetindersodhi)

    The system works fine, if cash on delivery method is available, it shows a notice & order does not get placed if a shipping method is not available.

    “No shipping method has been selected. Please double check your address, or contact us if you need any help.”

    But when I select PayPal, and click the Paypal Button, it takes me to Paypal login page.

    I have the option “Validate Checkout Fields” turned on, but apparently it doesn’t mind if there is no shipping method available.

    https://snipboard.io/Lgxu5h.jpg

    The functions.php code you provided solves this issue, but it would be better to include this code in the plugin itself and give an option for it.

    If the first name field is empty, it gives an error but doesn’t care for shipping methods.

    A notice is displayed in the PayPal popup “XXX doesn’t ship to this location. Please use a different address.” but the Pay button is still there.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Disable checkout if no shipping method is available’ is closed to new replies.