【发布时间】:2014-08-07 19:10:12
【问题描述】:
当您单击并转到用户选择城市的商店页面时,我想添加一个弹出窗口。 选择城市后,我想以用户选择的城市的特定价格显示产品。 每个城市都有不同的价格。
【问题讨论】:
-
如果没有为您自定义编码的东西,这是值得怀疑的,但我可以建议的最接近的方法是将每个城市作为一个类别,并为每个城市复制每个产品并以这种方式设置价格。
标签: php location woocommerce
当您单击并转到用户选择城市的商店页面时,我想添加一个弹出窗口。 选择城市后,我想以用户选择的城市的特定价格显示产品。 每个城市都有不同的价格。
【问题讨论】:
标签: php location woocommerce
您想为每个城市的每件产品指定准确的价格,还是可以编写简单的规则以适用于基于城市的原始价格(例如,四舍五入到最接近的偶数降低 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;
}
但使用此解决方案时请注意缓存。这可能会引起一些麻烦。
【讨论】:
这个具体的例子有很多代码要写,所以我会指出你可以做什么。
templates/single-product/price.php 模板以显示映射到他们所在城市的价格警告:我不确定动态定价选项是否适合您的需求,因为我认为它会根据用户角色在购物车中显示折扣,而不仅仅是调整价格项目。
【讨论】: