Woocommerce-产品价格取决于国家

Woocommerce-产品价格取决于国家

问题描述:

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

I have 2 questions regarding Woocommerce for Wordpress.

我正在一个向丹麦市场出售扬声器的网站上工作.

I'm working on a site that sells speakers to the Danish market.

问题一:

我可以检测访客的IP并检测此人来自哪个国家/地区?我想这可以通过一些ClientLocation API来完成.

Can I detect the IP of a visitor and detect which country the person is from? I guess this can be done with some ClientLocation api.

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

Can I then disable all shopping relatet pages and buttons if a person is not from Denmark. Fx: hiding the add to cart, cart and checkout. I still want the persons to be able to see the prices, they should just not have the option to buy them.

问题2:

让我们说,第一个问题很成功.然后,我想显示与丹麦相比其他竞争产品的价格不同.因此,如果您从一个国家/地区访问该网站,价格为XXX,而从另一个国家/地区访问该价格为XXXX.
假设:

Lets say that question one was sucessfull made. Then I would like to show different prices for other contries than Denmark. So if you are visiting the site from one country, the price is XXX and from another country the price is XXXX.
Let's say:

In USA the price is = $500 
And in UK the price = £400

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

(This has nothing to do with currency. The market price is just different in different countries.)

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

I've looked at this plugin: http://wordpress.org/plugins/woocomerce-price-by-country/ It allowed me to write different prices for each product, but when I testet it with http://geopeeker.com/ I hadn't worked at all.

您能给我一些指针或一些您知道的插件的链接吗?

Can you give me some pointets or some links to some plugins that you know of?

更新

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

I managed to solve question 1. I store the visitors country in a cookie with IP location XML API And then I could just create an if statement, saying that if the country was not equal to Denmark, then the add to cart, cart etc. should be removed.

是的,如果知道的话,我将非常感激它,让我对如何解决问题2有所了解.

So yeah, I would really appreciate it if anyknow could give me an idea on how I can solve question 2.

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

I'm able to detect country, but not able to specify a price of each product to the given country.

第二次更新:

为了让所有感兴趣的读者知道,我最终购买了此插件.效果很好!

Just to let any interested readers know, I ended up buying this plugin. which is working perfectly!

对于问题的第二部分:如果您仅使用简单的产品类型(无变化),则可以将自定义价格字段添加到产品数据页面,然后使用woocommerce_get_price_html过滤价格.

For the 2nd part of your question: If you are only using simple product types (without variations) then you can add custom price fields to the product data page and filter the price using 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;
}

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

You can create and save custom fields on the product page like this:

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

}

-----------------对于具有变化的产品-------------------------- -

-----------------For products with Variations----------------------------

警告:可变产品要复杂得多,我对上述答案的信心不及上面简单产品部分的信心,但这是我目前对这两种方式的理解.使用此方法时,我遇到了一些微型购物车展示的问题,这些问题我必须解决(我将在最后解释),但是微型购物车和常规购物车中的总数都是正确计算的.

WARNING: Variable products are much more complicated and I'm not nearly as confident in this answer as I am with the simple products part above, but here's my current understanding either way. I had some mini-cart display issues that I had to hack around when using this method (which I will explain at the end), but the totals are calculated correctly in both the mini-cart and the regular cart.

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

First we want to add new fields to each variant on the variation tab of existing products:

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>';
}

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

We also need to add them dynamically whenever the user adds new variants on the edit product page:

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>';
}

然后,将更改保存到版本元数据中的自定义字段:

Then we save changes to the custom fields in the variation meta data:

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] ) );
        }
    }
}
}

现在我们有了自定义变化元数据,我们可以像这样在自定义价格模块中访问它:

Now that we have our custom variation meta data, we can access it in the custom price module like so:

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;
}

现在,我相信除了微型购物车显示器以外,其他所有东西都应该可以正常工作.由于某种原因,似乎我只是想不通如何访问变体元数据以强制其在微型购物车中正确显示-就像我发现正在生成微型购物车显示的位置,但我遇到了麻烦获取正确的上下文路径以访问自定义变量,因此我最终不得不在template-tags.php中执行此操作,并将自定义值的数组传递给自定义价格函数中的可选参数.就我们应该如何做事情而言,这感觉非常错误",但是却可以完成工作.我很乐意听到问题的这一部分的正确"解决方案.

Now, I believe that part should have everything working except for the mini-cart display. For some reason it seems like I just couldn't figure out how to get access to the variation meta data to force it to display properly in the mini cart - like I found where the mini-cart display was being generated but I was having trouble getting the right context path to access the custom variable so I ended up having to do that in the template-tags.php and pass an array of custom values to an optional parameter in my custom price function. This feels very 'wrong' in terms of how we should do things, but it gets the job done. I'm very open to hearing the 'correct' solution to this part of the problem.

在template-tags.php中:

In 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>