私は現在、ユーザーの場所に基づいて複数の価格/通貨を使用して遊んでいます。注文プロセス全体を実行していて、もうすぐです。
この関数を使用しget_price()
てフックしwoocommerce_get_price
(class-wc-product.php
822行目にあります)、製品から設定したカスタムフィールドの金額(gb_price、us_priceなど)を見つけます。
ショップ、単一の商品ビュー、カート、チェックアウトではすべて正常に機能しますが、注文すると、すべてデフォルトの基本コストと通貨にフォールバックします。これは、functions.phpを介してフックした場合にのみ失敗することに気づきました。関数自体をクラスファイルで直接修正すると、すべてが完全に機能します。
私は本当にWCのコアをハックしたくないので、誰かが見て、なぜ失敗するのか教えてもらえますか?これが私のコードです...
class-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()内のクラス自体に移動するだけで、完全に機能します。