Wordpress Woocommerce-产品价格取决于国家/地区
关于WoodPress的Woocommerce,我有两个问题 我正在一个向丹麦市场销售扬声器的网站上工作 问题一: 我是否可以检测访客的IP以及此人来自哪个国家?我想这可以通过一些ClientLocation api来实现 如果一个人不是丹麦人,我可以禁用所有购物相关页面和按钮吗。外汇:隐藏添加到购物车,购物车和结帐。 我仍然希望这些人能够看到价格,他们不应该有购买的选择 问题2: 假设第一个问题是成功的。然后,我想展示丹麦以外的其他国家的不同价格。因此,如果您是从一个国家访问站点,则价格为XXX,而从另一个国家访问站点,则价格为XXXX。Wordpress Woocommerce-产品价格取决于国家/地区,wordpress,location,woocommerce,Wordpress,Location,Woocommerce,关于WoodPress的Woocommerce,我有两个问题 我正在一个向丹麦市场销售扬声器的网站上工作 问题一: 我是否可以检测访客的IP以及此人来自哪个国家?我想这可以通过一些ClientLocation api来实现 如果一个人不是丹麦人,我可以禁用所有购物相关页面和按钮吗。外汇:隐藏添加到购物车,购物车和结帐。 我仍然希望这些人能够看到价格,他们不应该有购买的选择 问题2: 假设第一个问题是成功的。然后,我想展示丹麦以外的其他国家的不同价格。因此,如果您是从一个国家访问站点,则价格为XX
比如说:
In USA the price is = $500
And in UK the price = £400
(这与货币无关,只是不同国家的市场价格不同。)
我看过这个插件:
它允许我为每种产品写不同的价格,但当我用它进行测试时,我一点也没用
你能给我一些你知道的插件的指针或链接吗
更新
我设法解决了问题1。我将访客国家存储在cookie中,然后我可以创建一个if语句,表示如果该国家不等于丹麦,则应删除添加到购物车、购物车等
所以是的,如果anyknow能给我一个如何解决问题2的想法,我会非常感激
我能够检测到国家,但无法指定给定国家的每种产品的价格
第二次更新:
为了让所有感兴趣的读者都知道,我最终购买了一款运行完美的产品 我看到了您的更新,您确实获得了访客的国家/地区,您可以使用它创建if语句来删除购物车。(顺便说一句,这太酷了) 这不是回答了你的第二个问题吗,关于改变每位游客的价格?你所要做的就是确保这两个价格都存储在某个地方,然后让它响应丹麦或英国的价格 价格是特定的-自定义字段 您提到这不是货币转换-因此您需要存储这两个值。将自定义字段添加到使用新价格编辑的产品条目中,并将其命名为“denmarkprice”或其他名称 我对WooCommerce不太熟悉,不知道什么自定义字段插件可以工作,但是如果您不想自己创建自定义字段,并且想在if-else语句中显示该变量时,可以使用_meta()调用该变量
关于问题的第二部分:如果您只使用简单的产品类型(没有变化),那么您可以将自定义价格字段添加到产品数据页,并使用woocommerce\u get\u price\u html过滤价格
add_filter('woocommerce_get_price_html','so24863612_custom_price');
function so24863612_custom_price(){
global $post;
$_postID = $post->ID;
$product = get_product( $_postID );
$UK_price = get_post_meta($_postID, '_UK_price', true); //loads custom meta data
$return_price = $product->get_regular_price(); //default to regular price
if (!empty($UK_price)) {
$return_price = $UK_price;
}
return $return_price;
}
您可以在产品页面上创建和保存自定义字段,如下所示:
//Display custom fields on product data page in admin
add_action( 'woocommerce_product_options_general_product_data', 'so24963039_display_custom_general_tab_fields' );
function so24963039_display_custom_general_tab_fields() {
global $woocommerce, $post;
$UK_price = get_post_meta( $post->ID, '_UK_price', true );
woocommerce_wp_text_input(
array(
'id' => '_UK_price',
'label' => __( 'UK Price (£)', 'woocommerce' ),
'value' => $UK_price,
'desc_tip' => 'false'
)
);
}
//Save custom fields to access via get_post_meta
add_action( 'woocommerce_process_product_meta', 'so24963039_save_custom_general_tab_fields' );
function so24963039_save_custom_general_tab_fields ($post_id) {
$woocommerce_UK_price = $_POST['_UK_price'];
if( !empty( $woocommerce_UK_price ) )
update_post_meta( $post_id, '_UK_price', esc_attr( $woocommerce_UK_price ) );
}
-----------------适用于有变化的产品----------------------------
警告:可变产品要复杂得多,我对这个答案的信心不如我对上面简单产品部分的信心,但这是我目前的理解。在使用这种方法时,我不得不解决一些迷你购物车显示问题(我将在最后解释),但迷你购物车和普通购物车中的合计都是正确计算的
首先,我们希望在现有产品的变体选项卡上为每个变体添加新字段:
add_action( 'woocommerce_product_after_variable_attributes', 'so24963039_variable_fields', 10, 2 ); //Display Fields
function so24963039_variable_fields( $loop, $variation_data ) {
echo '<tr><td>';
woocommerce_wp_text_input(
array(
'id' => '_variant_UK_price['.$loop.']',
'label' => __( 'UK Price (£)', 'woocommerce' ),
'desc_tip' => 'false',
'value' => $variation_data['_variant_UK_price'][0]
)
);
echo '</td></tr>';
}
现在,我相信该部分应该有一切工作,除了迷你车显示。出于某种原因,我似乎无法理解如何访问变体元数据,以强制它在迷你车中正确显示-就像我发现迷你车显示是在哪里生成的,但我在获取访问自定义变量的正确上下文路径时遇到了问题,所以我不得不在template-tags.php并将自定义值数组传递给自定义价格函数中的可选参数。就我们应该如何做事而言,这感觉非常“错误”,但它完成了工作。我非常愿意听到这部分问题的“正确”解决方案
在template-tags.php中:
<div class="small-7 large-7 columns"><?php
$product_title = $_product->get_title();
echo '<a class="cart_list_product_title" href="'.get_permalink($cart_item['product_id']).'">' . apply_filters('woocommerce_cart_widget_product_title', $product_title, $_product) . '</a>';
echo '<div class="cart_list_product_price">';
//original line: echo woocommerce_price($_product->get_price());
/*Custom Price Override Block*/
$_productID = $_product->id;
$product_type = $_product->product_type;
if($product_type == 'variation') {
$custom_field_data = $_product->product_custom_fields;
$regular_price = $custom_field_data['_regular_price'];
$custom_UK_price = $custom_field_data['_variant_UK_price'];
$custom_variant_prices = [$regular_price[0], $custom_UK_price[0]];
echo so24863612_get_custom_price($_productID, $custom_variant_prices );
} else {
echo so24863612_get_custom_price($_productID );
}
/*End Custom Price Override Block*/
echo ' /</div>';
echo '<div class="cart_list_product_quantity">'.__('Quantity', 'woocommerce').': '.$cart_item['quantity'].'</div>';
?></div>
请检查答案。谢谢你的回答。很抱歉,我没有及时回复。不幸的是,我有不同的产品,而不仅仅是简单的产品。我担心你会这么说。我的客户也需要同样的东西,我为此挣扎了一周左右。我让它工作,但它是有点黑客当谈到迷你车显示。(我不得不编辑一个模板文件,而不是用钩子修复它,因为我还不知道如何从我的插件中访问变量元数据)如果你碰巧找到了更好的方法,请告诉我!非常感谢。这让我走得很远!但我仍然没有越过终点线。但我已经接受了你的回答。谢谢你的帮助。如果我找到了一个优化的方法,我会告诉你的。仅供参考,Roy Ho还提供了一个额外的插件,它似乎为你提供了一些额外的选项,你可以显示基于国家IP的特定产品
add_action( 'woocommerce_process_product_meta_variable', 'so24963039_save_variable_fields', 10, 1 ); //Save variation fields
function so24963039_save_variable_fields( $post_id ) {
if (isset( $_POST['variable_sku'] ) ) {
$variable_sku = $_POST['variable_sku'];
$variable_post_id = $_POST['variable_post_id'];
// Variant Tier 1 Price
$_variant_UK_price = $_POST['_variant_UK_price'];
for ( $i = 0; $i < sizeof( $variable_sku ); $i++) {
$variation_id = (int) $variable_post_id[$i];
if ( isset( $_variant_UK_price[$i] ) ) {
update_post_meta( $variation_id, '_variant_UK_price', stripslashes($_variant_UK_price[$i] ) );
}
}
}
}
add_filter('woocommerce_get_price_html','so24863612_custom_price');
function so24863612_custom_price(){
global $post;
$_postID = $post->ID;
$product = get_product( $_postID );
$product_type = $product->product_type;
$UK_price = get_post_meta($_postID, '_UK_price', true); //covers simple products
if($product_type == 'variation'){ //override with variant prices
$UK_price = get_post_meta($_postID, '_variant_$UK_price', true);
}
$return_price = $product->get_regular_price(); //default to regular price
if (!empty($UK_price)) {
$return_price = $UK_price;
}
return $return_price;
}
<div class="small-7 large-7 columns"><?php
$product_title = $_product->get_title();
echo '<a class="cart_list_product_title" href="'.get_permalink($cart_item['product_id']).'">' . apply_filters('woocommerce_cart_widget_product_title', $product_title, $_product) . '</a>';
echo '<div class="cart_list_product_price">';
//original line: echo woocommerce_price($_product->get_price());
/*Custom Price Override Block*/
$_productID = $_product->id;
$product_type = $_product->product_type;
if($product_type == 'variation') {
$custom_field_data = $_product->product_custom_fields;
$regular_price = $custom_field_data['_regular_price'];
$custom_UK_price = $custom_field_data['_variant_UK_price'];
$custom_variant_prices = [$regular_price[0], $custom_UK_price[0]];
echo so24863612_get_custom_price($_productID, $custom_variant_prices );
} else {
echo so24863612_get_custom_price($_productID );
}
/*End Custom Price Override Block*/
echo ' /</div>';
echo '<div class="cart_list_product_quantity">'.__('Quantity', 'woocommerce').': '.$cart_item['quantity'].'</div>';
?></div>