How to link two accounts in Woocommerce?

Asked

Viewed 212 times

0

How to do this in Woocommerce?

Register a client linked to the representative’s account, then everything the customer buys the representative can see. And with that the company will pay a commission to the representative.

So the representative will know everything the customer is buying for him to receive his commission.

Is there any way to have that control?

1 answer

1

It will be necessary to implement several features to mount this system. And the beginning is exactly "link user accounts".

Customers and representatives will play a role (role) this can be managed manually via code or using a specialized plugin. I’ll show you how to do this on the User Profile page. The customer can choose his or her representative, and the representative can see which customers are associated with him or her. The logic of this can be different and the interface can be implemented in frontend along with Woocommerce forms, but the functions to query/record user data will be the same (see Codex for definitions):

  • get_users()
  • get_user_meta()
  • update_user_meta()
  • current_user_can()
  • current_user_has_role() - this is a custom function created by me from references

In this example, I use the standard papers, being Subscriber equivalent to Client, and Author equivalent to Representative. Here I am using procedural programming, but it’s better OOP (in these linked examples, you will see how to make the header of a plugin, which is how to implement the code below).

add_action( 'admin_init', 'wpse_70265_init');

/**
 * Novos campos no perfil de clientes e representantes
 */
function wpse_70265_init() 
{
    if( current_user_has_role( 'author' ) ) 
    {
        add_action( 'show_user_profile', 'mostrar_cliente_sopt_39058', 10 );    
    }
    if( current_user_has_role( 'administrator' ) ||  current_user_has_role( 'subscriber' ) )
    {
        add_action( 'show_user_profile', 'mostrar_representante_sopt_39058', 10 );
        add_action( 'edit_user_profile', 'mostrar_representante_sopt_39058', 10 );
        add_action( 'personal_options_update', 'guardar_representante_sopt_39058' );
        add_action( 'edit_user_profile_update', 'guardar_representante_sopt_39058' );
    } 
}

/**
 * Mostra lista de clientes no perfil do representante
 */
function mostrar_cliente_sopt_39058( $user ) 
{
    ?> 
    <h2>Clientes</h2>
    <table class="form-table">
        <tr>
            <th><label for="agree">Nome</label></th>
            <td>
                <ul>
                    <?php
                    $users = get_users( array( 'role' => 'subscriber', 'meta_key' => 'representante', 'meta_value' => $user->ID ) );
                    foreach( $users as $user )
                    {
                        printf(
                            '<li>%s</li>',
                            $user->data->user_nicename
                        );
                    }
                    ?>
                </ul>
            </td>           
        </tr>
    </table>
    <?php 
}

/**
 * Mostra lista de representantes no perfil dos clientes
 */
function mostrar_representante_sopt_39058( $user ) 
{
    ?> 
    <h2>Representante</h2>
    <table class="form-table">
        <tr>
            <th><label for="agree">Selecionar</label></th>
            <td>
                <ul>
                    <?php
                    $users = get_users( array( 'role' => 'author' ) );
                    $saved =  get_user_meta( $user->ID, 'representante', true );
                    foreach( $users as $user )
                    {
                        printf(
                            '<li><label><input type="radio" name="representante" value="%1$s" id="representante[%1$s]" %3$s />'.
                            ' %2$s ' .
                            '</label></li>',
                            esc_attr( $user->ID ),
                            esc_html( $user->data->user_nicename ),
                            checked( $saved, $user->ID, false )
                        );
                    }
                    ?>
                </ul>
            </td>           
        </tr>
    </table>
    <?php 
}

/**
 * Grava o representante escolhido pelo administrador ou cliente
 */
function guardar_representante_sopt_39058( $user_id ) 
{
    if ( !current_user_can( 'edit_user', $user_id ) )
        return false;

    update_user_meta( $user_id, 'representante', $_POST['representante'] );
}

/**
 * Function name grabbed from: http://core.trac.wordpress.org/ticket/22624
 * 2 lines of code from TutPlus: http://goo.gl/X4lmf
 */
if( !function_exists( 'current_user_has_role' ) )
{
    function current_user_has_role( $role )
    {
        $current_user = new WP_User( wp_get_current_user()->ID );
        $user_roles = $current_user->roles;
        $is_or_not = in_array( $role, $user_roles );
        return $is_or_not;
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.