Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在钩住的函数中调用自定义用户字段值_Php_Wordpress_Woocommerce_User Data_Hook Woocommerce - Fatal编程技术网

Php 在钩住的函数中调用自定义用户字段值

Php 在钩住的函数中调用自定义用户字段值,php,wordpress,woocommerce,user-data,hook-woocommerce,Php,Wordpress,Woocommerce,User Data,Hook 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 ){

我已经在用户配置文件中创建了一个自定义字段,现在我需要将该字段的值作为折扣调用到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);
}
add_action('woocommerce_cart_计算费用','add_custom_费用');
$DESCUNTOTOTAL=获取作者元('DESCUNTO',$user\U id);
功能添加自定义费用(WC\U购物车$Cart){
如果($DESCUNTOTOTAL<1){
返回;
}
//计算要减少的金额
$cart->add_fee(‘折扣:’,-$descumentotottal);
}
但无法获得“descuento”的值

我怎么做呢


谢谢。

您需要使用WordPress函数
获取当前用户id()
来获取当前用户id和用户元数据

因此,
woocommerce\u cart\u calculate\u 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 );
    }
}
add_操作('woocommerce_cart_计算费用','add_custom_费用');
函数添加\自定义\费用(){
if(定义了('DOING'uajax'))
返回;
如果(!用户是否已登录())
返回;
$user\u id=get\u current\u user\u id();
$DESCUNTOTOTAL=get_user_meta($user_id,'DESCUNTO',true);
如果($DESCUNTOTOTAL<1){
返回;
}否则{
$DESCUNTOTOTAL*=-1;
//启用转换“折扣:”
$discount=u uu('discount:','woocommerce');
//计算要减少的金额(不含税)
WC()->cart->add_fee($折扣、$descumentotottal、false);
}
}
代码会出现在活动子主题(或主题)的任何php文件中,或者出现在任何插件php文件中

参考或相关: