2014-07-21 81 views
5

我有2个关于WordPress的Woocommerce的问题。Woocommerce - 产品价格取决于国家

我正在从事向丹麦市场销售扬声器的网站。

问题一:

我能察觉访问者的IP和检测的人是来自哪个国家?我想这可以用一些ClientLocation API完成。

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

问题2:

让我们说,问题之一是sucessfull制造。那么我想为丹麦其他国家显示不同的价格。因此,如果您从一个国家/地区访问该网站,价格是XXX,而来自另一个国家/地区的价格是XXXX。
比方说:

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

(这有什么好做货币市场价格是在不同的国家只是不同。)

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

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

UPDATE

我设法解决问题1我存储来访的国家与IP location XML API一个cookie然后,我可能只是建立一个if语句,他说,如果该国不等于丹麦,然后添加到购物车,购物车等应该被删除。

所以,是的,我真的很感激,如果anyknow可以给我我怎么能解决问题2.

我能够检测的国家,但不能指定每个产品的价格的想法给定的国家。

第2'更新:

只是为了让任何有兴趣的读者知道,我最后买this plugin.这是工作完美!

+1

请检查答案 –

回答

4

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

您可以创建和产品页面类似这样的保存自定义字段:

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

} 

-----------------对于产品Variations- ---------------------------

警告:变量产品要复杂得多,我对此答案的信心不足因为我对上面的简单产品有所了解,但这是我目前的理解。我有一些微型购物车显示问题,我不得不在使用这种方法时(我将在后面解释),但总计在迷你购物车和普通购物车中均可正确计算。

首先,我们希望新的字段添加到每个变种现有产品的变化选项卡上:

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

我们还需要动态地添加它们,每当用户编辑产品页面上增加了新的变种:

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

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

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

现在,我认为部分应该拥有的一切,除了微型车显示工作。出于某种原因,我似乎无法弄清楚如何访问变体元数据,以迫使它在迷你购物车中正确显示 - 就像我发现迷你购物车显示屏正在生成的位置,但我遇到了麻烦获得正确的上下文路径来访问自定义变量,所以我最终必须在template-tags.php中执行此操作,并将自定义数组数组传递给我的自定义价格函数中的可选参数。就我们应该怎样做而言,这感觉非常“错误”,但它完成了工作。我非常乐于听到这个问题的“正确”解决方案。

在模板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> 
+0

喜。谢谢您的回答。对于迟到的回复,我感到抱歉。不幸的是,我有产品变化,而不仅仅是简单的产品。 – Adnaves

+0

我担心你会这么说。我的客户需要同样的东西,我在这里挣扎了一个星期左右。我知道它的工作原理,但对于迷你购物车显示器来说,这有点不合理。 (我不得不编辑模板文件,而不是用钩子修复它,因为我无法弄清楚如何从我的插件中访问变体元数据) 让我知道如果你碰巧找出更好的方法来做到这一点! – FascistDonut

+1

谢谢。这让我感到很痛苦!但我还没有越过终点线。但我已经接受你的答案。感谢所有的帮助。如果我找到最佳方式,我会告诉你。 – Adnaves

1

我看到你的更新,你设法得到访问者的国家,你可以用它来创建if语句来删除购物车。 (顺便说一句,酷酷的)

这不是回答你的问题2,关于更改每个访客的价格吗?所有你需要做的是确保两个价格都存储在某个地方,然后让它回应丹麦或英国的价格。

价格是特定的 - 自定义字段

你提到这不是货币转换 - 所以你需要存储这两个值。将自定义字段添加到以新价格编辑的产品条目中,并将其命名为“denmarkprice”或其他东西

我不是100%熟悉WooCommerce说自定义字段插件可能工作,但可以使用http://www.advancedcustomfields.com/如果您不想仅自己创建自定义字段,并且在想要将其显示在if else语句中时用the_meta()调用该变量。

http://codex.wordpress.org/Custom_Fields