Okay
  Public Ticket #3987538
Check if user is B2C or B2B
Closed

Comments

  •  8
    Miguel Zenha started the conversation

    Hi, Stefan!

    We are now changing the store to a hybrid B2B & B2C shop and we are facing some issues.

    I want to build a shortcode to check if the user is a B2C or B2B

    If the user role is null or it's not logged in, then we assume that it is a B2C guest. 

     After that, we want to show the B2C or B2C price ($product->get_regular_price() / $product->get_sale_price()).

    How can we do this code snippet in PHP? 

     Best regards and thanks.

  •  2,435
    WebWizards replied

    Hello Miguel,

    Glad to assist,

    Generally you can use this PHP function to check if a user is B2B or B2C:

    if (b2bking()->is_b2b_user()){
    // user is b2b
    } else {
    // user is b2c
    }

     

    However, if your goal is to simply separate B2B and B2C prices, well, B2BKing does that automatically. 

    These functions:

    $regular_price = $product->get_regular_price();
    $sale_price = $product->get_sale_price();

    should return the correct price for the current user (either the b2b or b2c price depending on whether the user is b2b or b2c).

     

    It would be helpful if you can explain in more detail what you are trying to achieve. I would like to give it some thought and maybe I can provide you with a simpler or more elegant solution, or at least save you some time,

     

    Kind regards,

    Stefan

  •  8
    Miguel Zenha replied

    Hi Stefan!

    I managed to get it working.
    The goal was to show the price without taxes to b2c users, and the price with tax to b2c users.

    I can leave the code here in case someone needs to do something similar:

    In my case, the tax is 23%.

    Example: a product that costs 200€ with tax, shows up like this:
    B2C users: €200
    B2B users: 162.60 + TAX 

    I used the following code:

    $product_id = get_the_ID();
    $user_id = get_current_user_id();
    $html = '';
    $cliente_b2c = true;
    if (is_user_logged_in()) {
        if ( function_exists( 'b2bking' ) && b2bking()->is_b2b_user( $user_id ) ) {
            $cliente_b2c = false;
        }
        else {
            // cliente b2c
        }
    }
    else {
        // cliente b2c
    }
    if ($product_id) {    
        
        $product = wc_get_product($product_id);
        
        $preco_regular = $product->get_regular_price();
        $preco_sale = $product->get_sale_price();
        
        
        if( $cliente_b2c ) {
            $html .= '<div class="bloco-preco-b2b-single">';
            
            $html .= '<div class="bloco-label-preco">Preço</div>';
            $html .= '<div class="bloco-preco-sem-iva-single-product">';
            
            if ( $product->is_on_sale() ) {
                $html .= '<span class="preco-antigo">€' . $preco_regular . '</span>';    
                $html .= '<span class="preco-actual">€' . $preco_sale . '</span>';
            }
            else {
                $html .= '<span class="preco-actual">€' . $preco_regular. '</span>';
            }
            
            
            
            $html .= '</div>';
            
                    
            $html .= '</div>';
        }
        else {
            
            
            $taxa_iva = 23 / 100; // 0.23
            
            // Calcula o preço sem IVA
            $preco_sem_iva = $preco_regular / (1 + $taxa_iva);
            $preco_sem_iva_formatado = number_format($preco_sem_iva, 2, ',', '.');
            
            $html .= '<div class="bloco-preco-b2b-single">';
            
            $html .= '<div class="bloco-label-preco">Preço</div>';
            $html .= '<div class="bloco-preco-sem-iva-single-product">';
            
            
            if ( $product->is_on_sale() ) {
                $preco_sem_iva = $preco_sale / (1 + $taxa_iva);
                $preco_sem_iva_formatado = number_format($preco_sale, 2, ',', '.');
            }
        
            
            $html .= '<div class="bloco-preco-sem-iva-single-product">€' . $preco_sem_iva_formatado . '<span class="iva-label"> + IVA</span></div>';
            
            $html .= '</div>';
            $html .= '</div>';
            
            $html .= '<style>.bloco-features-produto {display: none !important;} .bloco-btn-contactar-b2b {display: flex !important;}</style>';
        }
        
    }
    echo $html;


    Best regards and thanks