WooCommerce如何限制产品下载的国家或地区?

3
在WordPress WooCommerce中,是否有一种方法/插件可以限制某个地区下载特定产品。请记住,这不应该是全站范围的限制,而是基于产品的限制。我想象的是,在结账时,如果启用了下载限制,则一个函数会获取当前用户的位置(国家),并将其与允许该产品的国家数组进行比较。如果匹配,则继续检查,如果不匹配,则返回一条消息,告知用户他们所请求的产品在他们的国家无法下载。问题是,是否存在这样的插件、功能、函数或片段,如果有,它在哪里?
更新:由于没有答案,我已经开始自己创建一些东西。我之前没有PHP经验,请帮助我使这段代码简洁。你可以试试它。这个代码正确吗?
更新(解决方案):Woocommerce现在有一个内置的功能,检查用户位置并将其存储供商店所有者在自定义函数中使用,可以随意使用它 :)
以下代码放入您的主题的functions.php文件中。它将在“常规选项卡”下的产品页面的添加/编辑页面上添加一个“区域设置”面板。它有两个选项,“限制类型:”,可以设置为“允许”或“拒绝”,以及“区域:”选项,您可以在其中指定受影响的国家。如果产品的区域设置未设置,则允许每个人都可以访问它。
/**
 *  Mazwi WooCommerce Region Control BETA 
 * ------------------------------------
 *
 *  
 * 
 * Execute code if the user's country (set for each product) is allowed
 *
 * Author: Taf Makura
 * Thanks to Remi Corson's Tutorial 
 */


// Display Fields
add_action( 'woocommerce_product_options_general_product_data', 'woo_add_custom_general_fields' );

// Save Fields
add_action( 'woocommerce_process_product_meta', 'woo_add_custom_general_fields_save' );

// Display Fields
function woo_add_custom_general_fields() {

  global $woocommerce, $post;

  echo '<div class="options_group">';

  ?>

    <?php
        // Select
            woocommerce_wp_select( 
            array( 
                'id'      => '_restriction-type', 
                'label'   => __( 'Restriction type', 'woocommerce' ), 
                'options' => array(
                'allow'   => __( 'Allow', 'woocommerce' ),
                'deny'   => __( 'Deny', 'woocommerce' ),
        )
    )
);

        // Create Textarea
            woocommerce_wp_textarea_input( 
            array( 
                'id'          => '_regions', 
                'label'       => __( 'Regions', 'woocommerce' ), 
                'placeholder' => '', 
                'desc_tip'    => 'true',
                'description' => __( 'Please enter two letter country codes. Each country code should be followed by a coma Example: ZW, AU, ZA, US ', 'woocommerce' ) 
        )
    );

  echo '</div>';
}


function woo_add_custom_general_fields_save( $post_id ){

        // Select
            $woocommerce_select = $_POST['_restriction-type'];
            if( !empty( $woocommerce_select ) )
                update_post_meta( $post_id, '_restriction-type', esc_attr( $woocommerce_select ) );


        // Textarea
            $woocommerce_textarea = $_POST['_regions'];
            if( !empty( $woocommerce_textarea ) )
                update_post_meta( $post_id, '_regions', esc_html( $woocommerce_textarea ) );

 }

以下代码应放入模板 .php 文件中,条件执行应在此发生。我可以想象,如果您在此处放置添加到购物车循环(添加到购物车按钮),它将允许您控制哪些产品可以在某些国家购买。基于每个产品的基础上。
<?php global $woocommerce; 

        // Get restriction type (deny or allow) for current product
        $restriction_type = get_post_meta( $post->ID, '_restriction-type', true );

        // Get region(s) the above restriction type is applied to 
        $regions = get_post_meta( $post->ID, '_regions', true );

        // Turns this string into an array. 
        $regions_array = (explode(',', str_replace('/\s+/','', $regions)));

        // Get users current IP location from WooCommerce 
        $base_country = (..... YOU NEED TO GET THE USER LOCATION ISO COUNTRY CODE ......)

        // If user's current IP location is either allowed, is not denied or is not set in the region settings = success
        if( $restriction_type == 'allow' && in_array($base_country , $regions_array) || $restriction_type == 'deny' && !in_array($base_country , $regions_array) || $restriction_type == '' || $regions == '' ) {

                if ($restriction_type == '' || $regions == '') {

                    //  Code to execute on success if a product is not set (NOTE: It will not be restricted)

                    echo('This product\'s region control has not been set, you can set it in WP Admin');
                }

                // Code to execute on success if a products region settings are set to allow access

                echo('YOU ARE IN'); 

        } else {

                // Code to execute when region is restricted

                echo(' you are restricted,');
        }

?>

你自己回答了你的问题。 - user2286243
我的问题是这样的东西,插件,函数或片段是否存在,我在哪里可以得到它? - TafMakura
Taf(OP),你有没有继续开发这个项目?我正在寻找类似的功能,想知道你的解决方案进展到哪里了。 - inspirednz
@inspiredlife,我刚刚编辑了第二个代码片段,使其按照我的插件工作方式工作,你可以继续完成剩下的部分。重要的是,上面的代码中$base_country应该是用户的2字母ISO国家代码,你需要自己通过用户的IP地址获取它。现在WooCommerce已经内置了一个处理此问题的函数。 - TafMakura
@TafMak:感谢您回复我。我会试着运行您提供的代码。谢谢... Jonathan - inspirednz
显示剩余5条评论
1个回答

1

我不确定你是否看到/尝试过这个,但根据http://docs.woothemes.com/document/configuring-woocommerce-settings/,你可以做你所要求的。

要配置您的商店,请转到WooCommerce>设置。然后浏览以下内容以获取有关WooCommerce选项的更多信息。

允许的国家/地区

在此处,您可以选择是销售/发货到所有国家/地区还是仅限于一些国家/地区 - 如果仅在自己的国家内交易,则非常有用。不在允许国家/地区范围内的客户将无法结帐。

特定国家/地区

定义您愿意销售/发货的国家/地区。您必须将“允许的国家/地区”选项设置为“特定国家/地区”。


我知道这一点,但是:1)这是一个站点范围的设置,它将影响所有产品。 2)WooCommerce没有地理编码,因此它依赖于用户提供的信息来确定位置。此设置依赖于用户的账单地址。上述插件应该实现的目标是:1)使用地理编码来确定用户位置。 2)允许站点管理员为每个产品设置允许的国家/地区,而不是整个站点。 - TafMakura
  1. 当用户所需购买的产品在其所在国家受限时(即用户无法购买该产品),禁用添加到购物车按钮或任何其他模板功能,这对于基于位置的许可项目非常理想,例如电影下载。
  2. 它也适用于数字下载,并且不依赖于送货地址。
- TafMakura

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接