我想在您点击并转到商店页面时添加一个弹出窗口,用户选择该城市。 选择城市后,我希望显示具有该用户所选城市特定价格的产品。 每个城市都有不同的价格。
答案 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)
这个具体的例子有很多代码,所以我会指出你能做什么。
templates/single-product/price.php
模板以显示映射到其城市的价格警告:我不确定动态定价选项是否适合您的需求,因为我认为它会根据用户角色在购物车中显示折扣,而不仅仅是调整价格该项目。