根据特定角色隐藏 woocommerce 结帐中的自定义字段

问题描述 投票:0回答:1

我正在尝试禁用我在整个卖家的结帐页面中创建的自定义字段。正在寻找一种方法来禁用 WooCommerce 中商店经理的优惠券字段。我已将此代码插入到我的 function.php 中,我该怎么做?

add_filter( 'woocommerce_checkout_fields' , 'field_cfpiva' );

function field_cfpiva( $fields ) {
     $fields['billing']['billing_cf'] = array(
        'label'     => __('Codice Fiscale / P.IVA', 'woocommerce'),
        'placeholder'   => _x('Codice Fiscale / P.IVA', 'placeholder', 'woocommerce'),
        'required'  => true,
        'class'     => array('form-row-wide'),
        'clear'     => true
     );

     return $fields;
}

 add_filter( 'woocommerce_admin_billing_fields' , 'admin_field_cfpiva' );

function admin_field_cfpiva( $fields ) {
     $fields['cf'] = array(
        'label' => __('Codice Fiscale / P.IVA', 'woocommerce'),
        'show'  => true
    );
    return $fields;
}   

function add_cf_in_invoice( $fields, $order ) {
    $new_fields = array();

    if( get_post_meta( $order->id, '_billing_cf', true ) ) {
        $new_fields['_billing_cf'] = array( 
            'label' => 'Codice Fiscale: ',
            'value' => get_post_meta( $order->id, '_billing_cf', true )
        );
    }    
    return array_merge( $fields, $new_fields );
}

add_filter( 'wcdn_order_info_fields', 'add_cf_in_invoice', 10, 2 );

/**
* Add the field to order emails
**/
add_filter('woocommerce_email_order_meta_keys', 
'my_woocommerce_email_order_meta_keys');

function my_woocommerce_email_order_meta_keys( $keys ) {
$keys['_billing_cf'] = 'Codice Fiscale';
return $keys;
}
php woocommerce checkout custom-fields
1个回答
0
投票

修改您的函数“admin_field_cfpiva”。您需要在添加字段之前添加条件。

尝试以下代码 -

function admin_field_cfpiva( $fields ) {
    $user = wp_get_current_user();
    if ( !in_array( 'shop_manager', (array) $user->roles ) ) {   

         $fields['cf'] = array(
              'label' => __('Codice Fiscale / P.IVA', 'woocommerce'),
              'show'  => true
         );
    }
    return $fields;
}
© www.soinside.com 2019 - 2024. All rights reserved.