Php 基于用户角色隐藏WooCommerce上的自定义签出字段

Php 基于用户角色隐藏WooCommerce上的自定义签出字段,php,wordpress,woocommerce,checkout,user-roles,Php,Wordpress,Woocommerce,Checkout,User Roles,我已经创建了一个CSR用户角色和几个自定义签出字段,以显示在WooCommerce的签出页面上,我想对任何其他用户隐藏这些签出字段,但具有CSR角色的用户除外 我已经创建了字段和角色,但是我的字段出了问题,因为它们仍然显示给所有用户。我按照教程隐藏字段。如果代码的格式设置已关闭,则表示歉意。当我从Atom中提取它时,编辑器没有接受我的大部分格式 添加自定义字段 add_action( 'woocommerce_after_order_notes', 'my_custom_checkout_fie

我已经创建了一个CSR用户角色和几个自定义签出字段,以显示在WooCommerce的签出页面上,我想对任何其他用户隐藏这些签出字段,但具有CSR角色的用户除外

我已经创建了字段和角色,但是我的字段出了问题,因为它们仍然显示给所有用户。我按照教程隐藏字段。如果代码的格式设置已关闭,则表示歉意。当我从Atom中提取它时,编辑器没有接受我的大部分格式

添加自定义字段

add_action( 'woocommerce_after_order_notes', 'my_custom_checkout_field' );

function my_custom_checkout_field( $checkout ) {
echo '<div class="my_custom_checkout_field"><h2>' . __('CSR Information')    
.'</h2>';

woocommerce_form_field( 'date_of_purchase', array(
'type' => 'text',
'label'      => __('Date of Purchase', 'woocommerce'),
'placeholder'   => _x('MM/DD/YYYY', 'placeholder', 'woocommerce'),
'required'   => false,
'class'      => array('form-row-wide'),
'clear'     => true,
   ), $checkout->get_value( 'date_of_purchase' ));

woocommerce_form_field( 'place_of_purchase', array(

'type' => 'select',
'label'      => __('Place of Purchase', 'woocommerce'),
'placeholder'   => _x('Select Option', 'placeholder', 'woocommerce'),
'required'   => false,
'class'      => array('form-row-wide'),
'clear'     => true,
'options' => array('option-1' => 'Option 1', 'option_2' => 'Option 2',    
'option_3' => 'Option 3'),
   ), $checkout->get_value( 'place_of_purchase' ));


woocommerce_form_field( 'color_item', array(
'type' => 'select',
'label'      => __('Product Color', 'woocommerce'),
'placeholder'   => _x('Select Option', 'placeholder', 'woocommerce'),
'required'   => false,
'class'      => array('form-row-wide'),
'clear'     => true,
'options' => array('option-1' => 'Option 1', 'option_2' => 'Option 2',
'option_3' => 'Option 3'),
   ), $checkout->get_value( 'color_item' ));


woocommerce_form_field( 'product_model', array(

'type' => 'select',
'label'      => __('Model', 'woocommerce'),
'placeholder'   => _x('Select Option', 'placeholder', 'woocommerce'),
'required'  => false,
'class'      => array('form-row-wide'),
'clear'     => true,
'options' => array('option-1' => 'Option 1', 'option_2' => 'Option 2',
'option_3' => 'Option 3'),
   ), $checkout->get_value( 'product_model' ));  

echo '<strong>' . __('Check All That Apply:') .'</strong>';


woocommerce_form_field( 'lightbulb_out', array(
'type'          => 'checkbox',
'class'         => array('checkbox_field'),
'label'         => __('Lightbulb is Out'),
'required'  => false,
   ), $checkout->get_value( 'lightbulb_out' ));

woocommerce_form_field( 'not_turn_on', array(
'type'          => 'checkbox',
'class'         => array('checkbox_field'),
'label'         => __('Will Not Turn On'),
'required'  => false,
   ), $checkout->get_value( 'not_turn_on' ));

woocommerce_form_field( 'fan_not_running', array(
'type'          => 'checkbox',
'class'         => array('checkbox_field'),
'label'         => __('Fan Stopped Running'),
'required'  => false,
   ), $checkout->get_value( 'fan_not_running' ));

woocommerce_form_field( 'strange_noise', array(
'type'          => 'checkbox',
'class'         => array('checkbox_field'),
'label'         => __('Strange Noise'),
'required'  => false,
   ), $checkout->get_value( 'strange_noise' ));

woocommerce_form_field( 'not_catching', array(
'type'          => 'checkbox',
'class'         => array('checkbox_field'),
'label'         => __('Not Catching Insects'),
'required'  => false,
   ), $checkout->get_value( 'not_catching' ));

woocommerce_form_field( 'csr_other', array(
'type'          => 'checkbox',
'class'         => array('checkbox_field'),
'label'         => __('Other'),
'required'  => false,
   ), $checkout->get_value( 'csr_other' ));

woocommerce_form_field( 'case_description', array(

'type' => 'textarea',
'label'      => __('Description of Case', 'woocommerce'),
'placeholder'   => _x('Please provide details', 'placeholder', 'woocommerce'),
'required'  => false,
'class'      => array('form-row-wide'),
'clear'     => true,
   ), $checkout->get_value( 'case_description' ));

echo '</div>';
}
隐藏除CSR之外的所有CSR值

function custom_override_checkout_fields( $fields ) {
if ( ! current_user_can( 'csr' ) && isset( $fields['date_of_purchase'] ) ) {
    unset( $fields[['date_of_purchase']] );
}

if ( ! current_user_can( 'csr' ) && isset( $fields['place_of_purchase'] ) ) {
    unset( $fields[['place_of_purchase']] );
}

if ( ! current_user_can( 'csr' ) && isset( $fields['color_item'] ) ) {
    unset( $fields[['color_item']] );
}

if ( ! current_user_can( 'csr' ) && isset( $fields['product_model'] ) ) {
    unset( $fields[['product_model']] );
}

if ( ! current_user_can( 'csr' ) && isset( $fields['lightbulb_out'] ) ) {
    unset( $fields[['lightbulb_out']] );
}

if ( ! current_user_can( 'csr' ) && isset( $fields['not_turn_on'] ) ) {
    unset( $fields[['not_turn_on']] );
}

if ( ! current_user_can( 'csr' ) && isset( $fields['fan_not_running'] ) ) {
    unset( $fields[['fan_not_running']] );
}

if ( ! current_user_can( 'csr' ) && isset( $fields['strange_noise'] ) ) {
    unset( $fields[['strange_noise']] );
}

if ( ! current_user_can( 'csr' ) && isset( $fields['not_catching'] ) ) {
    unset( $fields[['not_catching']] );
}

if ( ! current_user_can( 'csr' ) && isset( $fields['csr_other'] ) ) {
    unset( $fields[['csr_other']] );
}

if ( ! current_user_can( 'csr' ) && isset( $fields['case_description'] ) ) {
    unset( $fields[['case_description']] );
}

 return $fields;
}
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields'
);

我已经扩展了我在教程中发布的关于如何使用的代码

首先,您需要注册新的签出字段。在这里,我添加了一个
当前用户\u can()
,以测试当前用户是否具有查看这些额外字段的适当能力。您可能可以使用
current\u user\u can('csr')
或者更好地将
manage\u csr
功能添加到
csr
角色中。我正在使用
manage\u options
功能,因为它更容易测试

// Add new checkout fields
function kia_filter_checkout_fields( $fields ){

    if( current_user_can( 'manage_options' ) ) {

        $fields['extra_fields'] = array(
            'date_of_purchase' => array(
                'type' => 'text',
                'label'      => __( 'Date of Purchase', 'your-plugin' ),
                'placeholder'   => _x ('MM/DD/YYYY', 'placeholder', 'your-plugin' ),
                'required'   => false,
                'class'      => array( 'form-row-wide' ),
                'clear'     => true
            ),
            'place_of_purchase' => array(
                'type' => 'select',
                'label'      => __( 'Place of Purchase', 'your-plugin' ),
                'placeholder'   => _x( 'Select Option', 'placeholder', 'your-plugin' ),
                'required'   => false, 
                'class'      => array('form-row-wide' ),
                'clear'     => true,
                'options' => array('option-1' => __( 'Option 1', 'your-plugin' ), 'option_2' => __( 'Option 2', 'your-plugin' ), 'option_3' => __( 'Option 3', 'your-plugin' ) ) 
            ),
            'color_item' => array(
                'type' => 'select',
                'label'      => __( 'Product Color', 'your-plugin' ),
                'placeholder'   => _x( 'Select Option', 'placeholder', 'your-plugin' ),
                'required'   => false,
                'class'      => array( 'form-row-wide' ),
                'clear'     => true,
                'options' => array( 'option-1' => __( 'Option 1', 'your-plugin' ), 'option_2' => __( 'Option 2', 'your-plugin' ), 'option_3' => __( 'Option 3', 'your-plugin' ) ) 
            ),
            'product_model' => array(
                'type' => 'select',
                'label'      => __( 'Model', 'your-plugin' ),
                'placeholder'   => _x('Select Option', 'placeholder', 'your-plugin' ),
                'required'  => false,
                'class'      => array('form-row-wide'),
                'clear'     => true,
                'options' => array( 'option-1' => __( 'Option 1', 'your-plugin' ), 'option_2' => __( 'Option 2', 'your-plugin' ), 'option_3' => __( 'Option 3', 'your-plugin' ) )
            ),
            'product_condition' => array(
                'type' => 'multicheck',
                'label'      => __( 'Product Condition:', 'your-plugin' ),
                'description'      => __( 'Check All That Apply:', 'your-plugin' ),
                'required'  => false,
                'clear'     => true,
                'options' => array( 'lightbulb_out' => __( 'Lightbulb is Out', 'your-plugin' ), 
                                    'not_turn_on' => __( 'Will Not Turn On', 'your-plugin' ), 
                                    'fan_not_running' => __( 'Fan Stopped Running', 'your-plugin' ),
                                    'strange_noise' => __( 'Strange Noise', 'your-plugin' ), 
                                    'not_catching' => __( 'Not Catching Insectsn', 'your-plugin' ), 
                                    'csr_other' => __( 'Other', 'your-plugin' ),
                        ),

            ),
            'case_description' => array(
                'type' => 'textarea',
                'label'      => __( 'Description of Case', 'your-plugin' ),
                'placeholder'   => _x( 'Please provide details', 'placeholder', 'your-plugin' ),
                'required'  => false,
                'class'      => array( 'form-row-wide' ),
                'clear'     => true,
            ),

        );
    }

    return $fields;
}
add_filter( 'woocommerce_checkout_fields', 'kia_filter_checkout_fields' );
你会注意到我对你的“风扇停止”等复选框做了一些不同的操作。你不必这么做,但我很好奇,而且拖拖拉拉。WooCommerce不支持多个复选框,但它支持定义自己的自定义字段类型。因此,通过以下步骤,我们创建了一种新类型的表单字段:

function kia_multicheck_form_field( $field, $key, $args, $value ){

    $field_html = '<fieldset>';

    if( isset( $args['label'] ) ){
        $field_html .= '<legend>' . $args['label'] . '</legend>';
    }


    if ( ! empty( $args['options'] ) ) {
        foreach ( $args['options'] as $option_key => $option_text ) {
            $field_html .= '<input type="checkbox" class="input-multicheck ' . esc_attr( implode( ' ', $args['input_class'] ) ) . '" value="' . esc_attr( $option_key ) . '" name="' . esc_attr( $key ) . '[]" id="' . esc_attr( $args['id'] ) . '_' . esc_attr( $option_key ) . '"' . checked( $value, $option_key, false ) . ' />';
            $field_html .= '<label for="' . esc_attr( $args['id'] ) . '_' . esc_attr( $option_key ) . '" class="multicheck ' . implode( ' ', $args['label_class'] ) . '">' . $option_text . '</label>';
        }
    }

    if ( $args['description'] ) {
        $field_html .= '<span class="description">' . esc_html( $args['description'] ) . '</span>';
    }

    $field_html .= '</fieldset>';

    $container_class = esc_attr( implode( ' ', $args['class'] ) );
    $container_id = esc_attr( $args['id'] ) . '_field';

    $after = ! empty( $args['clear'] ) ? '<div class="clear"></div>' : '';

    $field_container = '<p class="form-row %1$s" id="%2$s" data-sort="' . esc_attr( $sort ) . '">%3$s</p>';

    $field = sprintf( $field_container, $container_class, $container_id, $field_html ) . $after;

    return $field;
}
add_filter( 'woocommerce_form_field_multicheck', 'kia_multicheck_form_field', 10, 4 );
函数kia\u multicheck\u form\u字段($field、$key、$args、$value){
$field_html='';
如果(isset($args['label'])){
$field_html.=''.$args['label'].';
}
如果(!empty($args['options'])){
foreach($args['options']作为$option\u key=>$option\u text){
$field_html.='';
$field_html.='.$option_text.';
}
}
如果($args['description'])){
$field_html.=''.esc_html($args['description'])。';
}
$field_html.='';
$container\u class=esc\u attr(内爆('''$args['class']);
$container_id=esc_attr($args['id'])。'u field';
$after=!empty($args['clear'])?“”:“”;
$field_container='

%3$s

”; $field=sprintf($field\u container,$container\u class,$container\u id,$field\u html)。$after; 返回$field; } 添加过滤器('woocommerce'u form'u field'u multicheck','kia'u multicheck'u form'u field',10,4);
接下来,我们在签出页面上显示新字段。。。。但只有当它们存在时,因为返回到第一个代码块,如果用户没有正确的权限,它们将不在签出字段数组中

// display the extra field on the checkout form
function kia_extra_checkout_fields(){ 

    $checkout = WC()->checkout(); 

    if( isset( $checkout->checkout_fields['extra_fields'] ) ) { ?>

        <div class="extra-fields">
        <h3><?php _e( 'CSR Information' ); ?></h3>

        <?php

        // because of this foreach, everything added to the array in the previous function will display automagically
        foreach ( $checkout->checkout_fields['extra_fields'] as $key => $field ) : 
            woocommerce_form_field( $key, $field, $checkout->get_value( $key ) );
        endforeach; ?>
        </div>

<?php }
}
add_action( 'woocommerce_checkout_after_customer_details' ,'kia_extra_checkout_fields' );
//在签出表单上显示额外字段
函数kia_extra_checkout_fields(){
$checkout=WC()->checkout();
如果(设置($checkout->checkout\u字段['extra\u字段]){?>

看起来你一开始并没有将你的字段添加到
woocommerce\u checkout\u字段中,因此它们无法通过这种方式删除。然后你会在订单注释后直接在
woocommerce\u hook上回显它们。非常感谢你的帮助!不幸的是,我正在研究的主题是阻止它执行p没错。不过它确实适用于2016年的主题!谢谢你对照2016年的主题进行检查。这真的应该是一个插件,而不是你主题的一部分。哪个部分被阻止正确执行?你主题的签出页面是否在客户详细信息后有一个
woocommerce\u签出\u
hook?
// display the extra field on the checkout form
function kia_extra_checkout_fields(){ 

    $checkout = WC()->checkout(); 

    if( isset( $checkout->checkout_fields['extra_fields'] ) ) { ?>

        <div class="extra-fields">
        <h3><?php _e( 'CSR Information' ); ?></h3>

        <?php

        // because of this foreach, everything added to the array in the previous function will display automagically
        foreach ( $checkout->checkout_fields['extra_fields'] as $key => $field ) : 
            woocommerce_form_field( $key, $field, $checkout->get_value( $key ) );
        endforeach; ?>
        </div>

<?php }
}
add_action( 'woocommerce_checkout_after_customer_details' ,'kia_extra_checkout_fields' );