Okay
  Public Ticket #3705528
B2B and B2C price on archives
Closed

Comments

  •  2
    Petr Kalous started the conversation

    Hi, 

    I am trying to achieve a custom plugin for displaying prices for both B2C and B2B on archive and product pages.

    1) How to get REGULAR B2C price for B2B user. I found something for product page but nothing for archives.

    2) Is it possible to add product price for B2C with Vat and for B2B without VAT?

    Here is my part of the code.
    echo '<div class="wcvbp-variation-column">' . add_filter('b2bking_both_prices_retail_adjust_tax', '__return_false') . '</div>';
    // Cena velkoobshodní BEZ DPH echo '<div class="wcvbp-variation-column">' . ( $sale_price ? '<span class="wcvbp-sale-price">' . wc_price( $sale_price ) . '</span>' : wc_price( $regular_price ) ) . '</div>';
    // Cena velkoobshodní S DPH
    echo '<div class="wcvbp-variation-column">' . ( $sale_price ? '<span class="wcvbp-sale-price">' . wc_price( $sale_price * 1.12 ) . '</span>' : wc_price( $regular_price * 1.12 ) ) . '</div>';


    Attached files:  Snímek obrazovky 2024-08-14 205520.png

  •  2,281
    WebWizards replied

    Hello Petr,

    Glad to assist,


    (1) I believe the best way to get the B2C price here, would be to get it straight from the database, using get_post_meta functions.

    In general, I think you can use this code to get the raw price values for the B2C regular and sale price:

    global $post;
    if (isset($post->ID)){
        $product_id = $post->ID;
        $b2c_regular_price = get_post_meta($product_id, '_regular_price', true);
        $b2c_sale_price = get_post_meta($product_id, '_sale_price', true);
    }
    

    It should work in archives as well. I think we can use one of the hooks from here https://www.businessbloomer.com/woocommerce-visual-hook-guide-archiveshopcat-page/ for the display.

    For example, if we add this code snippet to functions.php:

    add_action('woocommerce_after_shop_loop_item_title', function(){
        global $post;
        if (isset($post->ID)){
            $product_id = $post->ID;
            $b2c_regular_price = get_post_meta($product_id, '_regular_price', true);
            $b2c_sale_price = get_post_meta($product_id, '_sale_price', true);
            echo 'B2C Regular Price: '.wc_price($b2c_regular_price);
        }
    }, 10, 1);
    

    It results in:

    7675420222.png


    (2) Is it possible to add product price for B2C with Vat and for B2B without VAT?

    (A) I want to first mention that this is generally possible without custom code, by follow these steps:


    -> Configure a tax exemption in B2BKing -> Dynamic Rules, for B2B users only.

    -> Enable these settings in B2BKing - Settings -> Other: 

    1668723842.png

    ( the first setting is explained in more detail here: https://woocommerce-b2b-plugin.com/docs/how-to-display-rrp-recommended-retail-price-to-b2b-users/ )


    The result would be the following, shown on both the archive and single product page:


    3365049835.png




    (B) Or do you specifically only need to do it with code / in a custom plugin?

    If so, generally you can get the B2B price ex VAT using:

    if ($product->is_on_sale()){
        $product_price = $product->get_sale_price();
    } else {
        $sale_price = $product->get_regular_price();
        $product_price = $sale_price;
    }
    $product_price = round(floatval(b2bking()->b2bking_wc_get_price_to_display( $product, array( 'price' => $product_price))),4);
    

    And the B2C price can be obtained as described at (1).


    It can depend on your WooCommerce -> Settings -> Tax page configuration.


    Looking forward to your reply,

    Kind regards,

    Stefan

  •  2
    Petr Kalous replied

    Hi Stefan,

    thanks for youre advice. The first one works great.


    The second one is something else. 

    I want to assing porduct prices for B2C with VAT and B2B without VAT in backend, directly in to woocommerce. Is here any posiblity?


    A have one more question. 

    Client has some B2C customers with additional discout 5% for all customs. Is there any way how to create B2C group for it? - I have solution in my head, but it's more dificult then B2C group.


    Thank you for answers.

    Have a nice day

    Petr K.



  •  2,281
    WebWizards replied

    I want to assing porduct prices for B2C with VAT and B2B without VAT in backend, directly in to woocommerce. Is here any posiblity?

    I understand you're referring to how prices are entered in the backend.

    To set this up, please add the following PHP code snippet to your site:

    add_filter('option_woocommerce_prices_include_tax', function($val){
        $user_id = get_current_user_id();
        $is_b2b = get_user_meta($user_id, 'b2bking_b2buser', true);
        if ($is_b2b === 'yes'){
            $val = 'no'; // prices entered ex tax for B2B
        } else {
            $val = 'yes';
        }
        return $val;
    }, 10, 1)
    

    Then you will enter prices differently for B2C and B2C:

    7863261225.png

    For example in this image, 119 is VAT inclusive, and 90 is VAT exclusive.


    Client has some B2C customers with additional discout 5% for all customs. Is there any way how to create B2C group for it? - I have solution in my head, but it's more dificult then B2C group.

    I don't see a great solution I'm afraid. I think in that case they have to be set as B2B users in their own group.

    The concept of the plugin is that if a user has special pricing / discounts, it cannot really be a B2C user.