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

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

报告相同问题?

悬赏问题

  • ¥60 pb数据库修改或者求完整pb库存系统,需为pb自带数据库
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路