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

标签 php location woocommerce

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

最佳答案

您想为每个城市的每个产品指定确切的价格,还是可以编写简单的规则,您可以将其应用于基于城市的原始价格(例如低 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/

相关文章:

php - WooCommerce 3.0 : can't find hook which corresponds to an admin created backend order

php - 需要使用 pdo 检索 sql 数据库中的单个单元格

php - Pressflow innodb数据库挂起

java - 当我将标记当前位置拖到另一个位置时,如何更新位置?

string - 比较结构化和非结构化的邮政地址

php - WooCommerce 礼品卡插件实际上在哪里创建数据库记录?

php - Yii widget 在组件中调用

php - 在 JSON 编码中使用深度的目的是什么?

python - 在 Geopandas 中转换 shapefile 的坐标

wordpress - 如何禁用 woocommerce 的购物车功能?