• Hi everyone, I’m having a problem with woocommerce, I need to set a postalcode for global $woocommerce variable, even when the user is not logged in, to display products based on the city avaiability. So the code below should check if the postalcode is already set to $woocommerce, if not, set it. But when I browse the site, the postcode is never there, the code always jump in the IF statement, I cant persist the postcode. The code should to jump in IF statement just whem load the site, than it should to jump in Else stament. does anyone know why?

    if (!is_admin()) {
        add_action('init', 'ns_prod_getlocation_ip');
    }
    function ns_prod_getlocation_ip()
    {
        global $woocommerce;
        $zip = $woocommerce->customer->get_postcode();
        $city = $woocommerce->customer->get_city();
    
       var_dump($zip); // empty
    
       if (empty($zip) || empty($city))
       {
          $woocommerce->customer->set_location('','','10100100','test');
       }
       else
       { 
          var_dump('test')
       }
    
       var_dump($zip); // not empty
    }
  • The topic ‘Problem with set postcode to $woocommerce->customer->set_location’ is closed to new replies.