我目前一直在根据用户位置使用多种价格/货币。我正在经历整个订购过程,我几乎就在那里。
我正在使用该get_price()函数与woocommerce_get_price(位于class-wc-product.php第 822 行)挂钩,然后从产品中找到我设置的自定义字段金额(gb_price、us_price 等)。
在商店、单一产品视图、购物车、结帐中一切正常,但在下订单时,一切都会退回到默认的基本成本和货币。我注意到这只在通过functions.php连接时失败。如果我直接在类文件中修改函数本身,一切正常。
我真的不想破解 WC 的核心,所以有人可以看看并告诉我为什么它会失败吗?这是我的代码...
类-wc-product.php
function get_price() {  
    return apply_filters( 'woocommerce_get_price', $this->price, $this );
}
函数.php
add_filter('woocommerce_get_price', 'return_custom_price', $product, 2); 
function return_custom_price($price, $product) {    
    global $post, $woocommerce;
    // Grab the product id
    $post_id = $product->id; 
    // Get user's ip location and correspond it to the custom field key
    $user_country = $_SESSION['user_location'];
    $get_user_currency = strtolower($user_country.'_price');
    // If the IP detection is enabled look for the correct price
    if($get_user_currency!=''){
        $new_price = get_post_meta($post_id, $get_user_currency, true);
        if($new_price==''){
            $new_price = $price;
        }
    }
    return $new_price;
}   
所以除了订单确认外,这在任何地方都有效。如果我只是将函数从functions.php 移动到get_price() 中的类本身,它就可以完美运行。