在WordPress WooCommerce中,是否有一种方法/插件可以限制某个地区下载特定产品。请记住,这不应该是全站范围的限制,而是基于产品的限制。我想象的是,在结账时,如果启用了下载限制,则一个函数会获取当前用户的位置(国家),并将其与允许该产品的国家数组进行比较。如果匹配,则继续检查,如果不匹配,则返回一条消息,告知用户他们所请求的产品在他们的国家无法下载。问题是,是否存在这样的插件、功能、函数或片段,如果有,它在哪里?
更新:由于没有答案,我已经开始自己创建一些东西。我之前没有PHP经验,请帮助我使这段代码简洁。你可以试试它。这个代码正确吗?
更新(解决方案):Woocommerce现在有一个内置的功能,检查用户位置并将其存储供商店所有者在自定义函数中使用,可以随意使用它 :)
以下代码放入您的主题的functions.php文件中。它将在“常规选项卡”下的产品页面的添加/编辑页面上添加一个“区域设置”面板。它有两个选项,“限制类型:”,可以设置为“允许”或“拒绝”,以及“区域:”选项,您可以在其中指定受影响的国家。如果产品的区域设置未设置,则允许每个人都可以访问它。
以下代码应放入模板 .php 文件中,条件执行应在此发生。我可以想象,如果您在此处放置添加到购物车循环(添加到购物车按钮),它将允许您控制哪些产品可以在某些国家购买。基于每个产品的基础上。
更新:由于没有答案,我已经开始自己创建一些东西。我之前没有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,');
}
?>