在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,');
}
?>
你自己回答了你的问题。 –
我的问题是这样的事情,插件,功能或片段存在,我可以在哪里得到它? – TafMak
Taf(OP),你有没有继续发展呢?我正在寻找类似的功能,并且想知道你的解决方案在哪里。 – inspirednz