Untitled

webwizards avatar
webwizards
plain_text
09/17/2026 5:24 PM
5.1 KB
9
Indexable
$b2b_group_id = 1234; // Change this to the applicable B2BKing group ID.

$b2bking_guest_registered_price = function( $price_html, $product ) use ( $b2b_group_id ) {
	if (
		is_user_logged_in() ||
		! $product instanceof WC_Product ||
		'' === $price_html ||
		strpos( $price_html, 'b2bking-guest-registered-prices' ) !== false
	) {
		return $price_html;
	}

	$regular_prices = array();
	$active_prices  = array();

	if ( $product->is_type( 'variable' ) ) {
		$cache_key = 'b2bking_registered_price_' . $b2b_group_id . '_' . $product->get_id();
		$prices    = get_transient( $cache_key );

		if ( false === $prices ) {
			foreach ( $product->get_visible_children() as $variation_id ) {
				$variation = wc_get_product( $variation_id );

				if ( ! $variation ) {
					continue;
				}

				$regular = get_post_meta( $variation_id, 'b2bking_regular_product_price_group_' . $b2b_group_id, true );
				$sale    = get_post_meta( $variation_id, 'b2bking_sale_product_price_group_' . $b2b_group_id, true );

				if ( '' === trim( (string) $regular ) ) {
					continue;
				}

				$regular = (float) wc_format_decimal( $regular );
				$sale    = '' !== trim( (string) $sale ) ? (float) wc_format_decimal( $sale ) : '';

				$display_regular = wc_get_price_to_display(
					$variation,
					array( 'price' => $regular )
				);

				$display_active = $display_regular;

				if ( '' !== $sale && $sale < $regular ) {
					$display_active = wc_get_price_to_display(
						$variation,
						array( 'price' => $sale )
					);
				}

				$regular_prices[] = $display_regular;
				$active_prices[]  = $display_active;
			}

			$prices = array(
				'regular' => $regular_prices,
				'active'  => $active_prices,
			);

			set_transient( $cache_key, $prices, 12 * HOUR_IN_SECONDS );
		}

		$regular_prices = $prices['regular'];
		$active_prices  = $prices['active'];
	} else {
		$regular = get_post_meta( $product->get_id(), 'b2bking_regular_product_price_group_' . $b2b_group_id, true );
		$sale    = get_post_meta( $product->get_id(), 'b2bking_sale_product_price_group_' . $b2b_group_id, true );

		if ( '' === trim( (string) $regular ) ) {
			return $price_html;
		}

		$regular = (float) wc_format_decimal( $regular );
		$sale    = '' !== trim( (string) $sale ) ? (float) wc_format_decimal( $sale ) : '';

		$regular_prices[] = wc_get_price_to_display(
			$product,
			array( 'price' => $regular )
		);

		$active_prices[] = '' !== $sale && $sale < $regular
			? wc_get_price_to_display( $product, array( 'price' => $sale ) )
			: $regular_prices[0];
	}

	if ( empty( $active_prices ) ) {
		return $price_html;
	}

	$min_price   = min( $active_prices );
	$max_price   = max( $active_prices );
	$min_regular = min( $regular_prices );
	$max_regular = max( $regular_prices );

	if ( $min_price !== $max_price ) {
		$registered_price = wc_format_price_range( $min_price, $max_price );
	} elseif ( $min_regular === $max_regular && $min_price < $min_regular ) {
		$registered_price = wc_format_sale_price( $min_regular, $min_price );
	} else {
		$registered_price = wc_price( $min_price );
	}

	return '<span class="b2bking-guest-registered-prices">'
		. '<span>Your price: ' . $price_html . '</span><br>'
		. '<span>Price for registered users: ' . $registered_price . '</span>'
		. '</span>';
};

add_filter( 'woocommerce_get_price_html', $b2bking_guest_registered_price, 999999, 2 );
add_filter( 'woocommerce_variation_get_price_html', $b2bking_guest_registered_price, 999999, 2 );

$b2bking_clear_registered_price_cache = function( $product_id ) use ( $b2b_group_id ) {
	$parent_id = wp_get_post_parent_id( $product_id );
	$product_id = $parent_id ? $parent_id : $product_id;

	delete_transient( 'b2bking_registered_price_' . $b2b_group_id . '_' . $product_id );
};

add_action( 'woocommerce_process_product_meta', $b2bking_clear_registered_price_cache, 999 );
add_action( 'woocommerce_save_product_variation', $b2bking_clear_registered_price_cache, 999 );
Editor is loading...
Leave a Comment