Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
5.7 kB
4
Indexable
Never

add_filter( 'woocommerce_get_price_html', 'b2bking_show_both_prices', 999995, 2);
function b2bking_show_both_prices($price, $product){

	$groupid = 308;

	$product_id = $product->get_id();

	$retail_text = get_option('b2bking_retail_price_text_setting', esc_html__('Retail price','b2bking')).': ';
	$wholesale_text = get_option('b2bking_wholesale_price_text_setting', esc_html__('Wholesale price','b2bking')).': ';

	$retail_price = $wholesale_price = '';

	if (b2bking()->is_b2b_user()){
		$wholesale_price = $price;
	} else {
		$retail_price = $price;
	}
	
	// variable product
	if( $product->is_type('variable') ){
		$children = $product->get_children();
		$min_price = 0;
		$max_price = 0;
		foreach ($children as $variation_id){
			// get retail price
			$variation = wc_get_product($variation_id);

			if (b2bking()->is_b2b_user()){
				$variation_price = b2bking()->get_woocs_price(b2bking()->tofloat(get_post_meta($variation_id,'_regular_price', true)));
				$variation_price_temp = b2bking()->get_woocs_price(b2bking()->tofloat(get_post_meta($variation_id,'_sale_price', true)));
			} else {
				$variation_price = b2bking()->get_woocs_price(b2bking()->tofloat(get_post_meta($variation_id,'b2bking_regular_product_price_group_'.$groupid, true)));
				$variation_price_temp = b2bking()->get_woocs_price(b2bking()->tofloat(get_post_meta($variation_id,'b2bking_sale_product_price_group_'.$groupid, true)));
			}
			if (!empty($variation_price_temp)){
				// there is indeed a sale price
				$variation_price = $variation_price_temp;
			}

			if ($max_price === 0){
				$min_price = $max_price = $variation_price;
			} else {
				if ($variation_price < $min_price){
					$min_price = $variation_price;
				}
				if ($variation_price > $max_price){
					$max_price = $variation_price;
				}
			}
		}

		// apply tax
		if (apply_filters('b2bking_both_prices_retail_adjust_tax', false)){
			$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 ) );
		}

		// if min and max prices are different, show range, else show the price
		if ($min_price !== $max_price){
			$new_price = wc_format_price_range( $min_price, $max_price );
		} else {
			$new_price = wc_price($min_price);
			if ($min_price === 0){
				$new_price = ''; // do not show
			}
		}
		if (b2bking()->is_b2b_user()){
			$retail_price = $new_price;
		} else {
			$wholesale_price = $new_price;
		}

	} else {
		
		// Simple product
		// get retail price

		if (b2bking()->is_b2b_user()){
			$retail_price = b2bking()->get_woocs_price(b2bking()->tofloat(get_post_meta($product_id,'_regular_price', true)));
			$retail_sale_price = b2bking()->get_woocs_price(b2bking()->tofloat(get_post_meta($product_id,'_sale_price', true)));

			if (!empty($retail_price) && floatval($retail_price) !== 0){
				if (!empty($retail_sale_price) && apply_filters('b2bking_use_retail_sale_price', true)){
					// if there is a sale price
					if (apply_filters('b2bking_both_prices_retail_adjust_tax', false)){

						$retail_price = wc_format_sale_price(b2bking()->b2bking_wc_get_price_to_display( $product, array( 'price' => $retail_price ) ), b2bking()->b2bking_wc_get_price_to_display( $product, array( 'price' => $retail_sale_price ) ));

					} else {
						$retail_price = wc_format_sale_price($retail_price, $retail_sale_price );

					}
				} else {
					if (apply_filters('b2bking_both_prices_retail_adjust_tax', false)){// recent change, previously true
						$retail_price = wc_price(b2bking()->b2bking_wc_get_price_to_display( $product, array( 'price' => $retail_price )));

					} else {
						$retail_price = wc_price($retail_price);
					}
				}
			}
		} else {
			$wholesale_price = b2bking()->get_woocs_price(b2bking()->tofloat(get_post_meta($product_id,'b2bking_regular_product_price_group_'.$groupid, true)));
			$wholesale_sale_price = b2bking()->get_woocs_price(b2bking()->tofloat(get_post_meta($product_id,'b2bking_sale_product_price_group_'.$groupid, true)));

			if (!empty($wholesale_price) && floatval($wholesale_price) !== 0){
				if (!empty($wholesale_sale_price) && apply_filters('b2bking_use_retail_sale_price', true)){
					// if there is a sale price
					if (apply_filters('b2bking_both_prices_retail_adjust_tax', false)){

						$wholesale_price = wc_format_sale_price(b2bking()->b2bking_wc_get_price_to_display( $product, array( 'price' => $wholesale_price ) ), b2bking()->b2bking_wc_get_price_to_display( $product, array( 'price' => $wholesale_sale_price ) ));

					} else {
						$wholesale_price = wc_format_sale_price($wholesale_price, $wholesale_sale_price );

					}
				} else {
					if (apply_filters('b2bking_both_prices_retail_adjust_tax', false)){// recent change, previously true
						$wholesale_price = wc_price(b2bking()->b2bking_wc_get_price_to_display( $product, array( 'price' => $wholesale_price )));

					} else {
						$wholesale_price = wc_price($wholesale_price);
					}
				}
			}
		}
	
	}


	$abort = false;
	if ($retail_price === wc_price(0) || $wholesale_price === wc_price(0) || empty($retail_price) || empty($wholesale_price)){
		$abort = true;
	}

	if (!$abort){
		$price = '<span class="b2bking_both_prices_text b2bking_retail_price_text">'.$retail_text .'</span><span class="b2bking_both_prices_price b2bking_retail_price_price">'. $retail_price . '<br></span><span class="b2bking_both_prices_text b2bking_b2b_price_text">'.$wholesale_text.'</span><span class="b2bking_both_prices_price b2bking_b2b_price_price b2bking_b2b_price_id_'.esc_attr($product_id).'">'.$wholesale_price.'</span>';
	}


	return $price;
}
Leave a Comment