2016-11-28 7 views
1

ユーザープロファイルにカスタムフィールドを作成しました。そのフィールドの値を割引としてWooCommerceカートに呼び出す必要があります。
これはカートでカスタム料金を追加するための機能です:WooCommerceフック関数のカスタムユーザーフィールド値を呼び出す

add_action('woocommerce_cart_calculate_fees' , 'add_custom_fees'); 
$descuentototal = get_the_author_meta('descuento', $user_id); 

function add_custom_fees(WC_Cart $cart){ 
    if($descuentototal < 1){ 
     return; 
    } 
    // Calculate the amount to reduce 
    $cart->add_fee('Discount: ', -$descuentototal); 
} 

しかし、「descuento」の値を取得するために管理することはできません。

どうすればいいですか?

ありがとうございました。

答えて

1

あなたは、ユーザーのメタデータを取得するためにget_current_user_id()は、現在のユーザーIDとget_user_meta()を取得するために、WordPressの関数を使用する必要があります。

だから woocommerce_cart_calculate_feesフックのための正しいコードは次のようになります。

add_action('woocommerce_cart_calculate_fees', 'add_custom_fees'); 
function add_custom_fees(){ 

    if (is_admin() && ! defined('DOING_AJAX')) 
     return; 

    if (!is_user_logged_in()) 
     return; 

    $user_id = get_current_user_id(); 
    $descuentototal = get_user_meta($user_id, 'descuento', true); 

    if ($descuentototal < 1) { 
     return; 
    } else { 
     $descuentototal *= -1; 

     // Enable translating 'Discount: ' 
     $discount = __('Discount: ', 'woocommerce'); 

     // Calculate the amount to reduce (without taxes) 
     WC()->cart->add_fee($discount, $descuentototal, false); 
    } 
} 

コードは、任意のプラグインのPHPファイルでも、あなたのアクティブな子テーマ(またはテーマ)の任意のPHPファイルになりますか。

リファレンスまたは関連:

関連する問題