Okay
  Public Ticket #3779249
B2B Price Range (Frontend)
Closed

Comments

  •  1
    Alexandros started the conversation

    Hi there!

    there is something i would like your help with,

    our product structure is kind of like the following example:

    (Base Product Variable) - Arabian Nights Bites

    Variation: Arabian Nights Bites - 1 unit > Retail price: 0,50 € , B2B Price: null

    Variation: Arabian Nights Bites - 500 grams > Retail price: null , B2B Price: 11,47€ (at the admin panel we enter the price including vat)

    Variation: Arabian Nights Bites - 1 Kg > Retail price: null , B2B Price: 22,94€ (at the admin panel we enter the price including vat)

    At the frontend the b2b user sees this: B2B King - 1.jpg

    Arabian Nights Bites

    Retail Price:€ 0.50 price with VAT 

    Wholesale Price:€ 0.40 –€ 18.50 price excluding VAT

    We would like the b2b user to see the following 

    Arabian Nights Bites

    Retail Price:€ 0.50 price with VAT 

    Wholesale Price:€ 9.25–€ 18.50 price excluding VAT


    In details, at the wholesale range price of the frontend of the b2b user, currently it shows the retail price if b2b price is null.

    we would like to return only the b2b price returning from the lowest b2b price of the variations to the highest value range.

    Looking forward for your answer,

    Alex

    Attached files:  B2B King - 1.jpg

  •  2,281
    WebWizards replied

    Hello Alex,

    Glad to assist,

     

    B2BKing does not normally set that range directly. The way it works is that B2BKing just sets a price for each variation, and WooCommerce calculates the range based on each variation's price.

    In practice, what happens is that here:

    • Variation: Arabian Nights Bites - 1 unit > Retail price: 0,50 € , B2B Price: null

    The price for the variation is simply 0.50, and WooCommerce sets the range based on it.

     

    To set the range based on B2B prices only, I have tried to write a code snippet:

    add_filter('woocommerce_get_price_html', function($price, $product){
    
    	if ($product->is_type('variable')){
    		if (function_exists('b2bking') && b2bking()->is_b2b_user()){
    
    			$group_id = b2bking()->get_user_group();
    
    			$min_price = 0;
    			$max_price = 0;
    			// get all variations, and check B2B prices only.
    			$children = $product->get_children();
    			foreach ($children as $variation_id){
    				// get retail price
    				$variation = wc_get_product($variation_id);
    
    				$reg_price_group = b2bking()->get_woocs_price(b2bking()->tofloat(get_post_meta($variation_id,'b2bking_regular_product_price_group_'.$group_id, true)));
    				$sale_price_group = b2bking()->get_woocs_price(b2bking()->tofloat(get_post_meta($variation_id,'b2bking_sale_product_price_group_'.$group_id, true)));
    				$group_price = $reg_price_group;
    				if (!empty($sale_price_group)){
    					$group_price = $sale_price_group;
    				}
    
    				if (!empty($reg_price_group) || !empty($sale_price_group)){
    					if ($max_price === 0){
    						$min_price = $max_price = $group_price;
    					} else {
    						if ($group_price < $min_price){
    							$min_price = $group_price;
    						}
    						if ($group_price > $max_price){
    							$max_price = $group_price;
    						}
    					}
    				}
    			}
    
    			if ($min_price !== 0){
    
    				$min_price = b2bking()->b2bking_wc_get_price_to_display( $product, array( 'price' => $min_price ) );
    				$max_price = b2bking()->b2bking_wc_get_price_to_display( $product, array( 'price' => $max_price ) );
    				
    				$price = wc_format_price_range ($min_price, $max_price);
    			}
    		}
    		
    		
    	}
    
    	return $price;
    
    }, 9999999, 2);

     

    You can add this code to your functions.php file or any code snippets plugin. 

    This should work to set the range based on B2B prices only, but please note this cannot work in all situations. For example it will not work with dynamic rule discounts or fixed price rules. 

    It can only work if the prices are directly set on each variation.

    Unfortunately it would be impossible to make it work under all situations as it would involve a huge amount of code to basically rewrite the entire WooCommerce range display functionality.

    But if all your prices are simply set on the product directly, I think this should work well. I have tested it on my local test site and it seemed to work in my tests.

     

    Kind regards,

    Stefan