2

私は現在、ユーザーの場所に基づいて複数の価格/通貨を使用して遊んでいます。注文プロセス全体を実行していて、もうすぐです。

この関数を使用しget_price()てフックしwoocommerce_get_priceclass-wc-product.php822行目にあります)、製品から設定したカスタムフィールドの金額(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()内のクラス自体に移動するだけで、完全に機能します。

4

1 に答える 1

1

これが私が現在使用しているコードです。決して完璧ではありませんが、人々が正しい軌道に乗るのに役立つはずです。(WC2.0でまだテストされていませんが、2の場合、そのままでは機能しないとほぼ確信しています。)使用されるセッションはIP駆動型テスト用であり、機能に不可欠ではないことに注意してください。以前からプラグインとして使用するように調整しました。WPの製品では、価格にカスタムフィールド、つまり「us_price」、「gb_price」などを使用します。これは、IP検出がフックする方法です。

// 1. Change the amount
function return_custom_price($price, $product) {
    global $post, $woocommerce;
    $post_id = $post->ID;
    // Prevent conflicts with order pages and products, peace of mind.
    if($post_id == '9' || $post_id == '10' || $post_id == '17' || $post_id == '53' || $post_id == ''){
        // cart, checkout, , order received, order now
        $post_id = $product->id;
    }
    $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;
        }
    }
    if( is_admin() && $_GET['post_type']=='product' ){
        return $price;
    } else {
        return $new_price;
    }
}
add_filter('woocommerce_get_price', 'return_custom_price', $product, 2);

// 2. Update the order meta with currency value and the method used to capture it
function update_meta_data_with_new_currency( $order_id ) {
    if($_SESSION['user_order_quantity']>=2){
        update_post_meta( $order_id, 'group_ticket_amount', $_SESSION['user_order_quantity'] );
        update_post_meta( $order_id, 'master_of', '' );

    }
    update_post_meta( $order_id, 'currency_used', $_SESSION['user_currency'] );
    update_post_meta( $order_id, 'currency_method', $_SESSION['currency_method'] );
}
add_action( 'woocommerce_checkout_update_order_meta', 'update_meta_data_with_new_currency' );
于 2013-05-15T08:58:54.970 に答える