I've currently been playing around with using multiple prices/currencies based on user location. I'm going through the whole ordering process and I'm almost there.
I'm using the get_price()
function to hook with woocommerce_get_price
(located in class-wc-product.php
, line 822) and then find custom field amounts that i have set (gb_price, us_price etc) from the product.
All works fine in the shop, single product view, cart, checkout but when placing order it all falls back to the default base cost and currency. I've noticed that this only fails when hooking in via functions.php. If I amend the function itself directly in the class file, everything works perfectly.
I really don't want to hack away at the core of WC so can someone have a look and tell me why it fails? Here's my code...
class-wc-product.php
function get_price() {
return apply_filters( 'woocommerce_get_price', $this->price, $this );
}
functions.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;
}
So this works everywhere except the order confirmation. If I simply move the function from functions.php to the class itself within get_price(), it works perfectly.