Choose Agent Field only set when user role is sales agent at Woocommerce Checkout

167 Views Asked by At

I am using B2b sales Agent Plugin for woocommmerce. The issue is, As guest user to checkout, they dont want to choose the agent. They want to buy directly without sales agent.

But as the sales agent roles, They need to chose the own sales agent user during checkout. Since, the sales agent should enable this field in order to get track the order from spesific sales agen.

The question is

  1. How to unset the 'Choose agent' Field during the checkout is guest user.
  2. How to automatically assigned to the specific sales agent, when the sales agent is currently log in, then buying the product on behalf of customer without to choose the sales agent field.

I try this code but critical error, i try to see the b2b sales agent documentation from B2b Sales Agen Documentation, but cannot solved

Here is the code snippet:

//edit checkout field b2b

 add_filter( 'woocommerce_admin_billing_fields' , 'custom_override_b2b' );

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

         unset($fields['wcb2bsa_sales_agent']['wcb2bsa_sales_agent']);
        
         );
    }
    return $fields;
}

Thank you for helping me.

The Choose Sales agent at checkout

1

There are 1 best solutions below

4
Titus On

Please try this code, i remove wrong syntax ( additional ");" ) and add user check:

add_filter( 'woocommerce_admin_billing_fields' , 'custom_override_b2b' );

function custom_override_b2b( $fields ) {
    $user = wp_get_current_user();
    if(!$user){
        return $fields;
    }
    if ( !in_array( 'sales_agent', (array) $user->roles ) ) {
        unset($fields['wcb2bsa_sales_agent']);
    }
    return $fields;
}