我想在您点击并转到商店页面时添加一个弹出窗口,用户可以在其中选择城市。 选择城市后,我想显示用户选择的城市特定价格的产品。 每个城市都有不同的价格。
最佳答案
您想为每个城市的每个产品指定确切的价格,还是可以编写简单的规则,您可以将其应用于基于城市的原始价格(例如低 10% 四舍五入到最接近的偶数)?
如果你想为每个城市的每个产品指定价格,就是为所有产品的所有城市制作元字段。 The guide that Dez linked to was nice so I'll reuse that here .
首先为指定城市的用户设置一个cookie。获取城市值(value)的方式有很多种,不知道你更喜欢哪种方式。
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;
}
但使用此解决方案时要注意缓存。那可能会引起一些麻烦。
关于php - woocommerce:根据城市选择显示不同价格的产品,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25190324/