Wordpress / WooCommerce注册

时间:2014-09-22 18:45:44

标签: wordpress woocommerce

我使用的插件(User Profiles Made Easy)允许用户在我的网站上注册时选择自己的角色。它做得很好,但它没有做的是允许我根据他们选择的角色有条件地显示/隐藏其他字段。

我想到了Gravity Form及其用户注册插件,但它不允许用户在注册时选择自己的角色。

WooCommerce搭配是这样的:当买家退房时,我可以在那时(而不是在注册期间)收集额外的数据,但我希望只显示与他们选择的角色相关的字段。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

借用cutomizing WooCommerce checkout上的教程,我想我们可以通过current_user_can()将某些字段角色/功能条件logica包装起来。

// Add a new checkout field
function kia_filter_checkout_fields($fields){
    if( current_user_can('some_capability' ) ){

        $fields['extra_fields'] = array(
                'some_field' => array(
                    'type' => 'text',
                    'required'      => true,
                    'label' => __( 'Some field' )
                    )
                );
    }

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

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

    if( current_user_can('some_capability' ) ){

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

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

        <?php foreach ( $checkout->checkout_fields['extra_fields'] as $key => $field ) : ?>

                <?php woocommerce_form_field( $key, $field, $checkout->get_value( $key ) ); ?>

            <?php endforeach; ?>

    <?php } ?>
    </div>

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

// save the extra field when checkout is processed
function kia_save_extra_checkout_fields( $order_id, $posted ){
    if( current_user_can( 'some_capability' && isset( $posted['some_field'] ) ) {
        update_post_meta( $order_id, '_some_field', sanitize_text_field( $posted['some_field'] ) );
    }
}
add_action( 'woocommerce_checkout_update_order_meta', 'kia_save_extra_checkout_fields', 10, 2 );

// display the extra data on order recieved page and my-account order review
function kia_display_order_data( $order_id ){  
    if( current_user_can('some_capability' ) ){
?>
        <h2><?php _e( 'Additional Info' ); ?></h2>
        <table class="shop_table shop_table_responsive additional_info">
            <tbody>
                <tr>
                    <th><?php _e( 'Some Field:' ); ?></th>
                    <td data-title="Telephone"><?php echo get_post_meta( $order_id, '_some_field', true ); ?></td>
                </tr>
            </tbody>
        </table>
<?php }
}
add_action( 'woocommerce_thankyou', 'kia_display_order_data', 20 );
add_action( 'woocommerce_view_order', 'kia_display_order_data', 20 );


// display the extra data in the order admin panel
function kia_display_order_data_in_admin( $order ){  
    if( current_user_can('some_capability' ) ){
?>
        <div class="order_data_column">
            <h4><?php _e( 'Extra Details', 'woocommerce' ); ?></h4>
            <?php 
                echo '<p><strong>' . __( 'Some field' ) . ':</strong>' . get_post_meta( $order->id, '_some_field', true ) . '</p>'; ?>
        </div>
<?php }
}
add_action( 'woocommerce_admin_order_data_after_order_details', 'kia_display_order_data_in_admin' );