在WordPress中添加过滤器

时间:2019-07-22 10:23:19

标签: wordpress woocommerce hook add-filter

我正在尝试这样的代码:

我有一家商店,并且如果产品价格低于4.000欧元,也不想将产品出售给俄罗斯。

因此,我尝试执行一个返回true或false的函数,如果产品价格低于此价格,然后添加一个woocommerce过滤器以删除下拉列表中的Russia。

首先,我尝试在没有任何条件的情况下运行woocommerce过滤器,并且它可以正常工作,但是当我尝试在条件之前添加add过滤器时,它不起作用。

这是我的代码:

检查产品价格的功能

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;
}

然后是过滤器的功能:

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; 
}

我检查了一下,第一个返回了我222,但没有应用过滤器,我不知道为什么。

0 个答案:

没有答案