dongwu8653 2017-08-11 11:02
浏览 26
已采纳

根据用户国家/地区和产品ID自定义ADD TO CART按钮

In my WooCommerce store, I have some products which are only available in USA, I want to hide the "Add to Cart" button on a Single Product Page, when the geolocation finds any country other than USA.

If it is USA, show "add to cart', if it is not USA, hide "add to cart" button.

(P.S. I used a plugin for that, but it is not working for some javascript conflict with other plugins.)

  • 写回答

1条回答 默认 最新

  • duanniying2342 2017-08-11 12:20
    关注

    When conditions match (specific product IDs and other countries), this code will replace the add to cart buttons in shop archives by a link to the product and in single product pages by a custom text.

    You just need to set in:

    • 1st function the needed country code … (for you 'US' and already done)
    • 2nd and 3rd functions your product IDs in the array

    Here is the code:

    // Conditional function country code detection
    // @argument $code (string): country code restriction
    // @return boolean
    if( ! function_exists('is_from_country_code') ){
    
        // ==> HERE below set your country code (Replace 'US' by yours)
        function is_from_country_code( $code = 'US' ){
            $location = new WC_Geolocation;
            $user_ip = $location->get_ip_address();
            $user_country_code = $location->geolocate_ip( $user_ip, false, false )['country'];
            return $user_country_code == $code ? true : false;
        }
    }
    
    // Replace "Add to cart" single product button and quantity by a custom text
    add_action( 'woocommerce_single_product_summary', 'Custom_single_add_to_cart', 1 );
    function Custom_single_add_to_cart(){
        global $product;
    
        // Set here your product IDS
        $product_ids = array( 56, 53, 50 );
    
        if( is_from_country_code() ||
             ( ! is_from_country_code() && ! in_array( $product->get_id(), $product_ids) ) )
            return; // Continue for foreign countries and the specific products IDs
    
        remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
        add_action('woocommerce_single_product_summary', function(){
            echo '<p class="custom-text">'.__('Not available for your country', 'woocommerce').'</p>';
        }, 30);
    }
    
    // Shop and archives pages: we replace the button add to cart by a link to the product
    add_filter( 'woocommerce_loop_add_to_cart_link', 'custom_text_replace_button', 10, 2 );
    function custom_text_replace_button( $button, $product  ) {
    
        // Set here your product IDS
        $product_ids = array( 56, 53, 50 );
    
        if( is_from_country_code() ||
             ( ! is_from_country_code() && ! in_array( $product->get_id(), $product_ids) ) )
            return $button; // Continue for foreign countries and the specific products IDs
    
        $button_text = __("View product", "woocommerce");
        return '<a class="button" href="'.$product->get_permalink().'">'.$button_text.'</a>';
    }
    

    Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

    This code is tested on woocommerce version 3+ and works

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图2.0 版本点聚合中Marker的位置无法实时更新,如何解决呢?
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题