복붙노트

[WORDPRESS] Woocommerce의 IP 주소 (인 GeoLocation)를 기반으로 장바구니 버튼으로 변경 추가

WORDPRESS

Woocommerce의 IP 주소 (인 GeoLocation)를 기반으로 장바구니 버튼으로 변경 추가

해결법


  1. 1.Woocommerce에는 기본적으로 Geolocation 지원 도구 (및 WC_GEOLOCATION 관련 클래스)가 있습니다.

    Woocommerce에는 기본적으로 Geolocation 지원 도구 (및 WC_GEOLOCATION 관련 클래스)가 있습니다.

    당신이 가고있는 관련 국가를 정의하는 사용자 정의 조건 함수의 위치 정보가 데모 버튼을 사용할 것을 사용 아래의 첫 번째. 당신이 배열의 모든 관련 국가 코드를 설정합니다 그 함수에서 (난 그냥 목적을 테스트하기 위해 '미국'과 'SG'정의).

    코드:

    // Updated: check for the defined country codes based over user IP geolocation country code
    function country_geo_ip_check(){
        // ==> HERE define the countries codes in the array
        $non_allowed_countries = array('US', 'SG');
    
        // Get an instance of the WC_Geolocation object class
        $geolocation_instance = new WC_Geolocation();
        // Get user IP
        $user_ip_address = $geolocation_instance->get_ip_address();
        // Get geolocated user IP country code.
        $user_geolocation = $geolocation_instance->geolocate_ip( $user_ip_address );
    
        return in_array( $user_geolocation['country'], $non_allowed_countries ) ? false : true;
    }
    
    // Shop and other archives pages (replacing add to cart by a linked button to the product)
    add_filter( 'woocommerce_loop_add_to_cart_link', 'replace_loop_add_to_cart_button', 10, 2 );
    function replace_loop_add_to_cart_button( $button, $product  ) {
        if ( country_geo_ip_check() ) return $button;  // Exit for non defined countries
    
        if ( $product->is_type( 'variable' ) ) return $button; // Excluding variable products
    
        $button_text = __( "View product", "woocommerce" );
        $button_link = $product->get_permalink();
        return '<a class="button" href="' . $button_link . '">' . $button_text . '</a>';;
    }
    
    // Single producct pages
    add_action( 'woocommerce_single_product_summary', 'replace_single_add_to_cart_button', 1 );
    function replace_single_add_to_cart_button() {
        global $product;
    
        if ( country_geo_ip_check() ) return;  // Exit for non defined countries
    
        // For variable product types (keeping attribute select fields)
        if( $product->is_type( 'variable' ) ) {
            remove_action( 'woocommerce_single_variation', 'woocommerce_single_variation_add_to_cart_button', 20 );
            add_action( 'woocommerce_single_product_summary', 'custom_demo_button', 20 );
        }
        // For all other product types
        else {
            remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
            add_action( 'woocommerce_single_product_summary', 'custom_demo_button', 30 );
        }
    }
    
    // Your custom button replacement
    function custom_demo_button() {
        global $post, $product;
    
        $style = 'style="padding-right: 0.75em;padding-left: 0.75em;margin-left: 8px; background-color: #0ebc30;"';
        $button_text = __("View Demo", "woocommerce");
    
        // Get demo Url
        if( function_exists('get_field') )
            $url_demo = get_field( "url_demo" );
    
        if( ! isset($url_demo) ){ // If the Url is not defined
            $button_text = __("Missing URL", "woocommerce");
            $style = 'style="color: red; background-color: white; border: solid 1px red"';
            $url_demo = '#';
        }
    
        // Output
        echo '<a href="'.$url_demo.'" target="_blank" class="button demo_button"'.$style.'>'.$button_text.'</a>';
    }
    

    코드는 function.php의 활성 자식 테마의 파일 (또는 활성 테마)에 간다. 테스트 및 작동합니다.

  2. from https://stackoverflow.com/questions/49793939/change-add-to-cart-button-based-on-ip-adress-geolocation-in-woocommerce by cc-by-sa and MIT license