woocommerce:根据城市选择显示不同价格的产品

时间:2014-08-07 19:10:12

标签: php location woocommerce

我想在您点击并转到商店页面时添加一个弹出窗口,用户选择该城市。 选择城市后,我希望显示具有该用户所选城市特定价格的产品。 每个城市都有不同的价格。

2 个答案:

答案 0 :(得分:2)

您是否要为每个城市中的每件产品指定确切的价格,或者是否可以编写简单的规则,您可以根据城市应用于原始价格(例如,低于10%舍入到最接近的偶数)?

如果要为每个城市的每件产品指定价格,请为所有产品的所有城市制作元字段。 The guide that Dez linked to was nice so I'll reuse that here

首先为指定城市的用户设置cookie。有很多方法可以获得城市价值,我不知道你更喜欢哪种方式。

setcookie("city", $city, time() + 315360000);

然后使用此过滤器覆盖显示给用户的价格:

add_filter('woocommerce_get_sale_price', 'my_custom_price', 99, 2);
add_filter('woocommerce_get_price', 'my_custom_price', 99, 2);

function my_custom_price( $orginal_price, $product )
{
    //Get the cooke value
    $city = $_COOKIE["city"];

    //your logic for calculating the new price based on city here
    switch ($city) {
        case 'new_york':

            $new_price = round($orginal_price * 0.95); //Calculate the price (here 5% discount)
            break;

        default:
            $new_price = $orginal_price
            break;
    }

     //OR just: 
     $new_price = get_post_meta( $product->ID, 'wc_price_'.$city, true ); //Retrieve the price from meta value 

     //If no matching price is found, return original price
     if( ! empty( $new_price ) ) {
         return $orginal_price;
     } 

    //Return the new price (this is the price that will be used everywhere in the store)
    return $new_price;
}

但请注意使用此解决方案时的缓存。这可能会造成一些麻烦。

答案 1 :(得分:0)

这个具体的例子有很多代码,所以我会指出你能做什么。

选项1

  1. 当用户选择城市时,设置具有该城市名称或city_id
  2. 的Cookie
  3. 为与城市名称或city_id(Here's a guide
  4. 匹配的产品创建自定义字段
  5. 如果用户的Cookie中没有城市设置,则隐藏用户的价格
  6. 自定义templates/single-product/price.php模板以显示映射到其城市的价格
  7. 当他们将产品添加到购物车时,会覆盖产品的价格(Here's a guide
  8. 选项2

    1. 使用WooCommerce Dynamic Pricing
    2. 在将产品添加到购物车并将城市作为注册字段之前,要求某人注册
    3. 根据他们为City回答的内容,将他们置于为动态定价定义的角色
    4. 警告:我不确定动态定价选项是否适合您的需求,因为我认为它会根据用户角色在购物车中显示折扣,而不仅仅是调整价格该项目。