• Resolved joosemi1993

    (@joosemi1993)


    I’m trying to a code that works like this:

    I have a shop and I don’t want to sell product to Russia if the product price is less than 4.000€.

    So I tried to do a function that return true or false if the product price is less than this amount and then if it’s add a woocommerce filter to remove Russia in the dropdown.

    Firstly I tryed to run the woocommerce filter without any condition and it worked but when I tryed to add the add filter inside the condition before, doesn’t work.

    This is my code:
    Function that check the product price

    <?php 
    function is_Russia() {
        global $product;
        global $woocommerce;
        $geoip = geoip_detect2_get_info_from_current_ip();
        $user_country = $geoip->country->isoCode;
        if( ! is_admin() ) {
            $remove_rus = 'no es un admin';
            if (WC()->cart->cart_contents_count > 0) { 
                $remove_rus = 'no es un admin y el carrito esta lleno';
                if(is_checkout()) {
                    $remove_rus = 'no es un admin y el carrito esta lleno y esta en checkout page';
                    //$cart_price = floatval( preg_replace( '#[^\d.]#', '', $woocommerce->cart->get_cart_total() ) );
                    $cart_price =  WC()->cart->total;
                    $curreny_symbol = get_woocommerce_currency();
                    if ($curreny_symbol == 'EUR') {
                        $eur_amount = 4000;
                        if ($cart_price > $eur_amount) {
                            $remove_rus = true;
                        } else {
                            $remove_rus = false;
                        }
                    } elseif ($curreny_symbol == 'CHF') {
                        $chf_amount = 4405;
                        if ($cart_price > $chf_amount) {
                            $remove_rus = true;
                        } else {
                            $remove_rus = false;
                        }
                    } elseif ($curreny_symbol == 'DKK') {
                        $dkk_amount = 29836;
                        if ($cart_price > $dkk_amount) {
                            $remove_rus = true;
                        } else {
                            $remove_rus = false;
                        }
                    } elseif ($curreny_symbol == 'GBP') {
                        $gbp_amount = 3587;
                        if ($cart_price > $gbp_amount) {
                            $remove_rus = true;
                        } else {
                            $remove_rus = false;
                        }
                    } elseif ($curreny_symbol == 'USD') {
                        $usd_amount = 4487;
                        if ($cart_price > $usd_amount) {
                            $remove_rus = true;
                        } else {
                            $remove_rus = false;
                        }
                    }
                }
            }
        }
        if ($remove_rus > 0) {
            add_filter( 'woocommerce_countries', 'custom_us_states_post', 10, 1 );
            $remove_rus = 2222;
        }
        return $remove_rus;
    }

    And then the function of the filter:

    <?php function custom_us_states_post( $states ) {
        $non_allowed_us_states = array( 'RU'); 
        foreach( $non_allowed_us_states as $state_code ) {
            if( isset($states['RU'] ) ) unset( $states['RU'] );
        }
        return $states; 
    }

    I checked and the first one return me 222 but without apply the filter, I don’t know why.

    • This topic was modified 5 years, 7 months ago by joosemi1993.

    The page I need help with: [log in to see the link]

Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Add filter For billing and shipping countries’ is closed to new replies.