Woocommerce - 产品价格取决于国家

5 wordpress location woocommerce

我有2个关于Woocommerce for Wordpress的问题.

我正在开发一个向丹麦市场销售扬声器的网站.

问题一:

我可以检测访问者的IP并检测该人来自哪个国家/地区吗?我想这可以通过一些ClientLocation api来完成.

如果某个人不是来自丹麦,我可以禁用所有购物相关页面和按钮吗?Fx:隐藏添加到购物车,购物车和结账.我仍然希望这些人能够看到价格,他们应该没有选择购买它们.

问题2:

让我们说第一个问题是成功的.然后我想为丹麦其他国家展示不同的价格.因此,如果您从一个国家/地区访问该网站,价格为XXX,而另一个国家/地区的价格为XXXX.
让我们说:

In USA the price is = $500 
And in UK the price = £400
Run Code Online (Sandbox Code Playgroud)

(这与货币无关.不同国家的市场价格不同.)

我看过这个插件:http: //wordpress.org/plugins/woocomerce-price-by-country/它允许我为每个产品写不同的价格,但当我用http://geopeeker.com测试它时/我根本没有工作过.

你能给我一些pointets或一些链接到你知道的一些插件吗?

UPDATE

我设法解决问题1.我将访问者国家存储在一个带有IP位置XML API的cookie中然后我可以创建一个if语句,说如果该国家不等于丹麦,那么添加到购物车,购物车等.应该删除.

所以是的,如果有任何可以让我知道如何解决问题2,我会非常感激.

我能够检测到国家/地区,但无法为指定国家/地区指定每种产品的价格.

2更新:

只是为了让任何感兴趣的读者知道,我最终购买了这个插件.这是完美的工作!

Fas*_*nut 5

对于问题的第二部分:如果您只使用简单的产品类型(没有变化),那么您可以将自定义价格字段添加到产品数据页面并使用woocommerce_get_price_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;
}
Run Code Online (Sandbox Code Playgroud)

您可以在产品页面上创建和保存自定义字段,如下所示:

//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 ) );   

}
Run Code Online (Sandbox Code Playgroud)

-----------------适用于有变化的产品----------------------------

警告:可变产品要复杂得多,而且我对这个答案的信心并不像上面的简单产品部分那么自信,但这是我目前的理解.我有我不得不砍围绕使用这种方法(我将在年底讲解)当一些小型车的显示问题,但总数在两个微型车和普通车计算正确.

首先,我们要在现有产品的"变体"标签上为每个变体添加新字段:

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>';
}
Run Code Online (Sandbox Code Playgroud)

每当用户在编辑产品页面上添加新变体时,我们还需要动态添加它们:

add_action( 'woocommerce_product_after_variable_attributes_js', 'so24963039_variable_fields_js' ); //JS to add fields for dynamically added new variations
function so24963039_variable_fields_js(){ //add fields to new variations that get added 
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>';
}
Run Code Online (Sandbox Code Playgroud)

然后我们将更改保存到变体元数据中的自定义字段:

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] ) );
        }
    }
}
}
Run Code Online (Sandbox Code Playgroud)

现在我们有自定义变体元数据,我们可以在自定义价格模块中访问它,如下所示:

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;
}
Run Code Online (Sandbox Code Playgroud)

现在,我相信除了迷你推车显示器外,该部件应该可以正常工作.出于某种原因,我似乎无法弄清楚如何访问变体元数据以强制它在迷你购物车中正确显示 - 就像我发现迷你购物车显示器正在生成但我遇到了麻烦获取正确的上下文路径来访问自定义变量,所以我最终必须在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>
Run Code Online (Sandbox Code Playgroud)