• Resolved PaulJBis

    (@pauljbis)


    I want to allow users from other countries to browse my site, but not allow them to buy: if they try, there should be a message asking them to email us for a custom shipping quote.

    I have configured two shipping zones: my country and every other one, and I have left the latter without any shipping methods. This way, if an user enters a foreign shipping address, there won’t be any shipping methods available.

    Now I want to disable every payment gateway I have in that case. I know I can use the woocommerce_available_payment_gateways filter, but how do I detect the “no shipping methods” condition using code?

    Thanks.

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Support Leonardo Albuquerque a11n

    (@leonardolopesalbuquerque)

    Hello.

    You could check shipping country with the code bellow and add a conditional in case it’s not your country

    
    WC()->customer->get_shipping_country()
    

    Other way would be to check the cart items and get the shipping from it using the function bellow

    
    WC()->shipping->calculate_shipping( $package )
    

    If you need more help with custom coding, you can get development related help at the following places:

    WooCommerce Developer Resources Portal: https://developer.woocommerce.com/
    WooCommerce Slack Community: https://woocommerce.com/community-slack/
    WooCommerce Community on Facebook: https://www.facebook.com/groups/advanced.woocommerce/
    You can also reach out to the official WooCommerce development partners via this link: https://woocommerce.com/customizations/

    Thread Starter PaulJBis

    (@pauljbis)

    Unfortunately, we also need to exclude from shipping a couple of specific provinces from my country, so the first method isn’t 100% accurate. (I forgot to mention this in my first post).

    I am looking at the methods available in “WC_Order” (https://wp-kama.com/plugin/woocommerce/function/WC_Order), and I wonder: which one should I use to get the specific province in my country? get_shipping_postcode()? get_shipping_state()?

    As for calculate_shipping(), correct me if I’m wrong, but from the docs and the code samples I’ve read, my impression is that it’s a function to add shipping rates if you are programming your own shipping method (from https://woocommerce.com/document/shipping-method-api/). What does it return if there are no shipping methods available?(*) How do I check its return value?

    (*) As mentioned above, my idea is to create a shipping zone for all the places we don’t ship for, and leave it without any shipping methods.

    Hi @pauljbis

    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 the WooCommerce Developer Resources Portal for resources on developing for WooCommerce.

    You can also visit the WooCommerce Facebook group or the #developers channel of the WooCommerce Community Slack.

    Thread Starter PaulJBis

    (@pauljbis)

    Okay, I was an idiot and didn’t realize that there are already filters for this case:

    woocommerce_cart_no_shipping_available_html
    woocommerce_no_shipping_available_html

    Now all I need is to know how to remove the existing payment gateways from the coe executed in that filter.

    Thread Starter PaulJBis

    (@pauljbis)

    Okay, I finally found an answer elsewhere. To make a long story short: you need to create a global variable to allow the code in both filters, woocommerce_no_shipping_available_html and woocommerce_available_payment_gateways, to communicate with each other.

    Since creating global variables is considered bad practice, I created a class using the singleton pattern, which holds a variable saying if there are shipping methods or not.

    if( ! class_exists('Vuelamedia_EstadoGlobal')) {
    	class Vuelamedia_EstadoGlobal {
    		private $hayMetodoDeEnvio;
    		private static $instance;
    		
    		private function __construct() {
    			$this->hayMetodoDeEnvio=true;
    			}
    	
    	    public static function getInstance() {
            	if(is_null(self::$instance)) {
                    self::$instance = new self();
             	   }
                return self::$instance;
            	}
    
    		public function guardaValor($valor=TRUE) {
    			$this->hayMetodoDeEnvio = $valor;
    			}
    			
    		public function leeSiHayMetodoDeEnvio() {
    			return $this->hayMetodoDeEnvio;
    			}
    		}
    	}
    
    add_filter("woocommerce_no_shipping_available_html", "quitar_formas_de_pago", 20);
    add_filter("woocommerce_cart_no_shipping_available_html", "quitar_formas_de_pago", 20);
    
    function quitar_formas_de_pago($texto) {
    
    	Vuelamedia_EstadoGlobal::getInstance()->guardaValor(false);
    	return $texto;
    	}
    
    add_filter("woocommerce_available_payment_gateways", "filtrar_formas_de_pago");
    function filtrar_formas_de_pago($available_gateways) {
       if( is_admin() ) { return $available_gateways; }
       
       $metodo = Vuelamedia_EstadoGlobal::getInstance()->leeSiHayMetodoDeEnvio();
    
    	if (!$metodo) {
    		return array();
    		}
    	else { return $available_gateways; }
    	}
Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Detecting when there are no shipping methods available’ is closed to new replies.