2014-01-25 110 views
2

在WooCommerce中有没有一种方式/插件来限制某个产品在特定区域下载。请记住,这不是假设为网站范围,而是基于产品到产品的基础。WooCommerce限制按国家或地区下载产品 - [已解决]

我想象的是,在结帐时,如果某个产品具有启用​​下载限制的功能,则会拉出当前用户的位置(国家/地区),并将其与该产品允许的国家/地区进行比较。如果匹配,则检查收益如果不匹配,则返回一条消息,通知用户他们请求的产品无法在其国家下载。问题是,是否存在这样的插件,特征,功能或片段,如果存在的话?

更新:看到没有答案我已经开始,并开始创建自己的东西。我没有以前的PHP经验,所以请帮我简化代码。你可以尝试一下。它是否正确?

UPDATE(解决方案): 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,'); 

          } 



        ?> 
+0

你自己回答了你的问题。 –

+0

我的问题是这样的事情,插件,功能或片段存在,我可以在哪里得到它? – TafMak

+0

Taf(OP),你有没有继续发展呢?我正在寻找类似的功能,并且想知道你的解决方案在哪里。 – inspirednz

回答

1

我不知道,如果你看到/尝试这样做,但根据http://docs.woothemes.com/document/configuring-woocommerce-settings/你可以做你所要求的东西。

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

允许各国

在这里你可以选择是否要出售/船到过的国家,或 有选择的几个 - 有用的,如果只交易自己的国家或地区内 实例。您允许的国家以外的客户将无法登录 。

特定国家

定义您愿意出售/运送到的国家/地区。您必须将 “允许的国家/地区”选项设置为“特定国家/地区”。

+0

我知道这一点,但 1)这是一个网站的设置,它会影响所有产品。 2)WooCommerce没有地理编码,因此它依赖用户提供的信息来确定位置。此设置依赖于用户的帐单地址。 上面的插件应该实现的是: 1)使用地理编码来确定用户位置。 2)允许网站管理员为每个产品设置允许的国家,而不是整个网站。 – TafMak

+0

3)当用户想要购买的产品受限于其国家(用户无法购买产品)时,禁用添加到购物车按钮或任何其他模板功能,这对于位置许可产品(如电影下载)非常理想。 4)它与数字工作下载也不依赖送货地址。 – TafMak