WPutopia

Show Dynamic Wholesale Price in Kadence sticky-add-to-cart+WooCommerce + B2Bking

The Site is build with Kadence along with the WooCommerce and B2Bking plugin.

My client asked “I wonder if we can match the price in the sticky banner real time based on the quantity (lower price based on the quantity tier that user selects)”

I know she is mentioning the dynamic wholesale price that changes based on the quantity the B2B client choose to buy. This function works in the B2Bking Plugin. The most challenging part is how to show it in Kadence’s stickybar at the bottom:

dynamic-wholesale-price

Here’s how I did it:

I’ve modified the plugin (kadence-pro\dist\woocommerce-addons\hooks.php)

Here’s the new code for “Single Product Sticky Add to Cart.” line 192-180:

/**
 * Single Product Sticky Add to Cart.
 */
function single_product_sticky_add_to_cart() {
	if ( is_product() && kadence()->option( 'product_sticky_add_to_cart' ) ) {
		global $post;
		$product = wc_get_product( $post->ID );
		if ( ( $product->is_purchasable() && ( $product->is_in_stock() || $product->backorders_allowed() ) ) || $product->is_type( 'external' ) ) {
			echo '<div id="kadence-sticky-add-to-cart" class="kadence-sticky-add-to-cart vs-md-false vs-sm-false kadence-sticky-add-to-cart-' . esc_attr( kadence()->option( 'product_sticky_add_to_cart_placement' ) ) . ' item-at-start">';
			echo '<div class="site-container">';
				echo '<div class="kadence-sticky-add-to-cart-content">';
					echo '<div class="kadence-sticky-add-to-cart-title-wrap">';
						echo wp_kses_post( woocommerce_get_product_thumbnail() );
						echo '<span class="kadence-sticky-add-to-cart-title">' . wp_kses_post( get_the_title() ) . '</span>';
					echo '</div>';
					echo '<div class="kadence-sticky-add-to-cart-action-wrap">';
					
					// B2BKing tiered active price display
					$user_id = get_current_user_id();
					if (function_exists('b2bking') && $user_id > 0) {
						$user_id = b2bking()->get_top_parent_account($user_id);
						$currentusergroupidnr = apply_filters('b2bking_b2b_group_for_pricing', b2bking()->get_user_group($user_id), $user_id, $product->get_id());
						$price_tiers = get_post_meta($product->get_id(), 'b2bking_product_pricetiers_group_'.$currentusergroupidnr, true);
						
						if (function_exists('b2bking') && method_exists(b2bking(), 'convert_price_tiers')) {
							$price_tiers = b2bking()->convert_price_tiers($price_tiers, $product);
						}
						
						if (!empty($price_tiers) && strlen($price_tiers) > 1) {
							$price = $product->get_price();
							if (function_exists('b2bking') && method_exists(b2bking(), 'b2bking_wc_get_price_to_display')) {
								$price = b2bking()->b2bking_wc_get_price_to_display($product, array('price' => $price));
							}
							echo '<span class="kadence-sticky-add-to-cart-action-price price b2bking_tiered_active_price">';
							echo apply_filters('b2bking_active_price_under_range', wc_price($price), $product, $price);
							echo '</span>';
							do_action('b2bking_after_tiered_active_price', $price, $product);
						} else {
							echo '<span class="kadence-sticky-add-to-cart-action-price price">' . wp_kses_post( $product->get_price_html() ) . '</span>';
						}
					} else {
						echo '<span class="kadence-sticky-add-to-cart-action-price price">' . wp_kses_post( $product->get_price_html() ) . '</span>';
					}
					
					if ( ( $product->is_type( 'simple' ) || $product->is_type( 'external' ) || $product->is_type( 'subscription' ) ) && apply_filters( 'kadence-pro-sticky-show-single-add-to-cart', true, $product ) ) {
						woocommerce_template_single_add_to_cart();
					} else {
						echo '<a href="#product-' . esc_attr( $product->get_ID() ) . '" class="single_link_to_cart_button button alt">' . esc_html( $product->add_to_cart_text() ) . '</a>';
					}
					echo '</div>';
				echo '</div>';
			echo '</div>';
			echo '</div>';
			if ( kadence()->option( 'product_sticky_mobile_add_to_cart' ) ) {
				echo '<div id="kadence-mobile-sticky-add-to-cart" class="kadence-sticky-add-to-cart vs-lg-false kadence-sticky-add-to-cart-' . esc_attr( kadence()->option( 'product_sticky_mobile_add_to_cart_placement' ) ) . ' item-at-start">';
					echo '<div class="kadence-sticky-add-to-cart-content">';
						echo '<div class="kadence-sticky-add-to-cart-title-wrap">';
							echo wp_kses_post( woocommerce_get_product_thumbnail() );
							echo '<span class="kadence-sticky-add-to-cart-title">' . wp_kses_post( get_the_title() ) . '</span>';
						echo '</div>';
						echo '<div class="kadence-sticky-add-to-cart-action-wrap">';
						
						// B2BKing tiered active price display for mobile
						$user_id = get_current_user_id();
						if (function_exists('b2bking') && $user_id > 0) {
							$user_id = b2bking()->get_top_parent_account($user_id);
							$currentusergroupidnr = apply_filters('b2bking_b2b_group_for_pricing', b2bking()->get_user_group($user_id), $user_id, $product->get_id());
							$price_tiers = get_post_meta($product->get_id(), 'b2bking_product_pricetiers_group_'.$currentusergroupidnr, true);
							
							if (function_exists('b2bking') && method_exists(b2bking(), 'convert_price_tiers')) {
								$price_tiers = b2bking()->convert_price_tiers($price_tiers, $product);
							}
							
							if (!empty($price_tiers) && strlen($price_tiers) > 1) {
								$price = $product->get_price();
								if (function_exists('b2bking') && method_exists(b2bking(), 'b2bking_wc_get_price_to_display')) {
									$price = b2bking()->b2bking_wc_get_price_to_display($product, array('price' => $price));
								}
								echo '<span class="kadence-sticky-add-to-cart-action-price price b2bking_tiered_active_price">';
								echo apply_filters('b2bking_active_price_under_range', wc_price($price), $product, $price);
								echo '</span>';
								do_action('b2bking_after_tiered_active_price', $price, $product);
							} else {
								echo '<span class="kadence-sticky-add-to-cart-action-price price">' . wp_kses_post( $product->get_price_html() ) . '</span>';
							}
						} else {
							echo '<span class="kadence-sticky-add-to-cart-action-price price">' . wp_kses_post( $product->get_price_html() ) . '</span>';
						}
						
						if ( ( $product->is_type( 'simple' ) || $product->is_type( 'external' ) || $product->is_type( 'subscription' ) ) && apply_filters( 'kadence-pro-sticky-show-single-add-to-cart', true, $product ) ) {
							woocommerce_template_single_add_to_cart();
						} else {
							echo '<a href="#product-' . esc_attr( $product->get_ID() ) . '" class="single_link_to_cart_button button alt">' . esc_html( $product->add_to_cart_text() ) . '</a>';
						}
						echo '</div>';
					echo '</div>';
				echo '</div>';
			}
		}
	}
}

add_action( 'wp_footer', 'Kadence_Pro\single_product_sticky_add_to_cart', 50 );

In case of any issues, here’s the old code snippet:

/**
 * Single Product Sticky Add to Cart.
 */
function single_product_sticky_add_to_cart() {
	if ( is_product() && kadence()->option( 'product_sticky_add_to_cart' ) ) {
		global $post;
		$product = wc_get_product( $post->ID );
		if ( ( $product->is_purchasable() && ( $product->is_in_stock() || $product->backorders_allowed() ) ) || $product->is_type( 'external' ) ) {
			echo '<div id="kadence-sticky-add-to-cart" class="kadence-sticky-add-to-cart vs-md-false vs-sm-false kadence-sticky-add-to-cart-' . esc_attr( kadence()->option( 'product_sticky_add_to_cart_placement' ) ) . ' item-at-start">';
			echo '<div class="site-container">';
				echo '<div class="kadence-sticky-add-to-cart-content">';
					echo '<div class="kadence-sticky-add-to-cart-title-wrap">';
						echo wp_kses_post( woocommerce_get_product_thumbnail() );
						echo '<span class="kadence-sticky-add-to-cart-title">' . wp_kses_post( get_the_title() ) . '</span>';
					echo '</div>';
					echo '<div class="kadence-sticky-add-to-cart-action-wrap">';
					if ( ( $product->is_type( 'simple' ) || $product->is_type( 'external' ) || $product->is_type( 'subscription' ) ) && apply_filters( 'kadence-pro-sticky-show-single-add-to-cart', true, $product ) ) {
						echo '<span class="kadence-sticky-add-to-cart-action-price price">' . wp_kses_post( $product->get_price_html() ) . '</span>';
						woocommerce_template_single_add_to_cart();
					} else {
						echo '<span class="kadence-sticky-add-to-cart-action-price price">' . wp_kses_post( $product->get_price_html() ) . '</span>';
						echo '<a href="#product-' . esc_attr( $product->get_ID() ) . '" class="single_link_to_cart_button button alt">' . esc_html( $product->add_to_cart_text() ) . '</a>';
					}
					echo '</div>';
				echo '</div>';
			echo '</div>';
			echo '</div>';
			if ( kadence()->option( 'product_sticky_mobile_add_to_cart' ) ) {
				echo '<div id="kadence-mobile-sticky-add-to-cart" class="kadence-sticky-add-to-cart vs-lg-false kadence-sticky-add-to-cart-' . esc_attr( kadence()->option( 'product_sticky_mobile_add_to_cart_placement' ) ) . ' item-at-start">';
					echo '<div class="kadence-sticky-add-to-cart-content">';
						echo '<div class="kadence-sticky-add-to-cart-title-wrap">';
							echo wp_kses_post( woocommerce_get_product_thumbnail() );
							echo '<span class="kadence-sticky-add-to-cart-title">' . wp_kses_post( get_the_title() ) . '</span>';
						echo '</div>';
						echo '<div class="kadence-sticky-add-to-cart-action-wrap">';
						if ( ( $product->is_type( 'simple' ) || $product->is_type( 'external' ) || $product->is_type( 'subscription' ) ) && apply_filters( 'kadence-pro-sticky-show-single-add-to-cart', true, $product ) ) {
							echo '<span class="kadence-sticky-add-to-cart-action-price price">' . wp_kses_post( $product->get_price_html() ) . '</span>';
							woocommerce_template_single_add_to_cart();
						} else {
							echo '<span class="kadence-sticky-add-to-cart-action-price price">' . wp_kses_post( $product->get_price_html() ) . '</span>';
							echo '<a href="#product-' . esc_attr( $product->get_ID() ) . '" class="single_link_to_cart_button button alt">' . esc_html( $product->add_to_cart_text() ) . '</a>';
						}
						echo '</div>';
					echo '</div>';
				echo '</div>';
			}
		}
	}
}

add_action( 'wp_footer', 'Kadence_Pro\single_product_sticky_add_to_cart', 50 );

Support My Work

If you enjoy my content, consider buying me a coffee or shopping through my Rakuten link to support me!

Victor

Founder of WPUtopia.com🕵️I would appreciate it if you could leave me a comment!

Send Message

Chat with me

Start a Conversation

Hi! Let's connect on your preferred platform.