Site logo

Archived topic

Show the add to cart panel on single product pages

3 replies · Started by Randall on May 7, 2022

Viewing posts 1–4 of 4

Hi,

I'd like to display the cart panel when a product is added to the cart on a singular product page.

I've added a plugin to make the Ajax call and a snippet to filter the cart panel as shown in a previous forum post: https://generatepress.com/forums/topic/add-to-cart-banner-on-single-product-page-and-on-page-using-wc-shortcodes/
With those in place, the product is added to the cart via Ajax and the panel slides in-- but the panel is blank.

It looks like the generatepress_wc_add_to_cart_helper() function is using ! is_singular( 'product' ) conditionally when it writes the contents of the cart panel. Could that be why the panel is blank when it shows up?

What would I need to do to get the contents of the panel to show up?

Hi Randall,

In that case you'll need to re-write the entire function:

add_filter( 'generate_woocommerce_show_add_to_cart_panel', '__return_true', 10 );
remove_action( 'generate_after_footer', 'generatepress_wc_add_to_cart_helper' );
add_action( 'generate_after_footer', 'custom_wc_add_to_cart_helper' );

function custom_wc_add_to_cart_helper() {
	if ( ! generatepress_wc_get_setting( 'off_canvas_panel_on_add_to_cart' ) && ! generatepress_wc_get_setting( 'sticky_add_to_cart_panel' ) ) {
		return;
	}

	$outer_classes = array(
		'add-to-cart-panel',
	);

	$inner_classes = array(
		'inside-add-to-cart-panel',
	);

	if ( function_exists( 'generate_get_option' ) ) {
		if ( 'contained-nav' === generate_get_option( 'nav_layout_setting' ) ) {
			$outer_classes[] = 'grid-container grid-parent';
		}

		if ( 'contained' === generate_get_option( 'nav_inner_width' ) ) {
			$inner_classes[] = 'grid-container grid-parent';
		}
	}
	?>
		<div id="wc-sticky-cart-panel" class="<?php echo implode( ' ', $outer_classes ); ?>">
			<div class="<?php echo implode( ' ', $inner_classes ); ?>">

				<?php
				if ( generatepress_wc_get_setting( 'off_canvas_panel_on_add_to_cart' )  ) :
					$svg_icon = '';

					if ( function_exists( 'generate_get_svg_icon' ) ) {
						$svg_icon = generate_get_svg_icon( 'pro-close' );
					}
					?>
						<div class="continue-shopping <?php echo $svg_icon ? 'has-svg-icon' : ''; ?>">
							<?php echo $svg_icon; ?>
							<a href="#" class="continue-shopping-link"><span class="continue-shopping-text"><?php _e( 'Continue Shopping', 'gp-premium' ); ?> &rarr;</span></a>
						</div>

						<div class="cart-info">
							<div class="item-added">
								<?php _e( 'Item added to cart.', 'gp-premium' ); ?>
							</div>

							<div class="cart-data">
								<?php
								if ( isset( WC()->cart ) ) {
									echo sprintf( _n( '%d item', '%d items', WC()->cart->get_cart_contents_count(), 'gp-premium' ), WC()->cart->get_cart_contents_count() ); ?> - <?php echo WC()->cart->get_cart_total();
								}
								?>
							</div>
						</div>

						<?php
							// phpcs:ignore -- No need to escape full thing.
							echo apply_filters(
								'generate_wc_cart_panel_checkout_button_output',
								sprintf(
									'<div class="checkout">
										<a href="%s" class="button">%s</a>
									</div>',
									esc_url( wc_get_checkout_url() ),
									esc_html__( 'Checkout', 'gp-premium' )
								)
							);
						?>
					<?php
				endif;

				if ( generatepress_wc_show_sticky_add_to_cart() ) :
					$product = wc_get_product( get_the_ID() );
					$quantity_buttons = '';

					if ( generatepress_wc_get_setting( 'quantity_buttons' ) ) {
						$quantity_buttons = ' do-quantity-buttons';
					}
					?>
						<div class="product-image">
							<?php the_post_thumbnail( 'thumbnail' ); ?>
						</div>

						<div class="product-title">
							<?php the_title(); ?>
						</div>

						<?php if ( $product->get_price() ) : ?>
							<div class="product-price">
								<?php echo $product->get_price_html(); ?>
							</div>
						<?php endif;

						$action = '';

						if ( $product->is_type( 'simple' ) ) {
							$args = array(
								'min_value' => apply_filters( 'woocommerce_quantity_input_min', $product->get_min_purchase_quantity(), $product ),
								'max_value' => apply_filters( 'woocommerce_quantity_input_max', $product->get_max_purchase_quantity(), $product ),
							);

							$action = sprintf(
								'<form action="%1$s" class="cart%2$s" method="post" enctype="multipart/form-data">
									%3$s
									<button type="submit" class="button alt">%4$s</button>
								</form>',
								esc_url( $product->add_to_cart_url() ),
								$quantity_buttons,
								woocommerce_quantity_input( $args, $product, false ),
								esc_html( $product->add_to_cart_text() )
							);
						}

						if ( $product->is_type( 'variable' ) ) {
							$action = sprintf(
								'<button type="submit" class="button alt go-to-variables">%s</button>',
								esc_html( $product->add_to_cart_text() )
							);
						}

						if ( $product->is_type( 'external' ) ) {
							$action = sprintf(
								'<form action="%1$s" class="cart" method="post" enctype="multipart/form-data">
									<button type="submit" class="button alt">%2$s</button>
								</form>',
								esc_url( $product->add_to_cart_url() ),
								esc_html( $product->add_to_cart_text() )
							);
						}

						echo apply_filters( 'generate_wc_sticky_add_to_cart_action', $action, $product ); // phpcs:ignore -- No escaping needed.
				endif;
				?>

			</div>
		</div>
	<?php
}

Thank you, Ying!

It is working with that code added to the snippet.

Glad to hear that :)

This archived topic is closed to new replies.