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

时间:2018-04-24 09:09:25

标签: woocommerce checkout custom-fields

我正在尝试禁用我在结帐页面中为整个卖家创建的自定义字段。寻找一种方法来禁用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;
}

1 个答案:

答案 0 :(得分: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;
}