Archived topic
Display cart panel on add to cart: not working when quantity is >1
7 replies · Started by Michele on April 10, 2021
Hi guys,
I tried to display cart panel on add to cart and it works fine, but when I write a custom function to have the quantity buttons in the shop and product archive pages, it breaks.
The function is this one:
/**
* Override loop template and show quantities next to add to cart buttons
*/
add_filter( 'woocommerce_loop_add_to_cart_link', 'quantity_inputs_for_woocommerce_loop_add_to_cart_link', 10, 2 );
function quantity_inputs_for_woocommerce_loop_add_to_cart_link( $html, $product ) {
if ( $product && $product->is_type( 'simple' ) && $product->is_purchasable() && $product->is_in_stock() && ! $product->is_sold_individually() ) {
$html = '<form action="' . esc_url( $product->add_to_cart_url() ) . '" class="cart" method="post" enctype="multipart/form-data">';
$html .= woocommerce_quantity_input( array(), $product, false );
$html .= '<button type="submit" class="button alt">' . esc_html( $product->add_to_cart_text() ) . '</button>';
$html .= '</form>';
}
return $html;
}
Is there any possibility to make it work?
Thanks in advance.
PS: I thought in this situation it was important to choose the products quantity in the archives, since the products are mostly wine bottles.
Hi there,
I'm not seeing how that function would break it. Any chance you can link us to the page so we can take a look?
What exactly is that function doing? Doesn't WooCommerce add the quantity field by default?
Hey Tom! Unfortunately I'm working locally right now.
As I wrote before, I added that snippet in my functions file to have quantity buttons in product archive pages. You know: normally in the shop page you can't select quantities 😊
Hi there,
I've checked your code and it looked fine.
https://docs.woocommerce.com/document/override-loop-template-and-show-quantities-next-to-add-to-cart-buttons/
I've even tested it on my sandbox site here:
Sandbox WooCommerce Shop Page
And it looks like it's working fine on my sandbox site.
Can you explain a bit more on how it breaks? Let us know.
Hi Elvin,
Yeah the code looks absolutely fine! What doesn't work (as I wrote in the topic title) is the cart panel on add to cart.
You see, even in your sandbox, when the custom snippet is active the GPP function doesn't work:
Check the screenshot
When you delete the custom function the cart panel is showing correctly:
Check the screenshot
Any chance to make it work even with the function activated?
Ah, I see. So, our add to cart panel fires when WooCommerce fires the added_to_cart javascript trigger. In this case, filtering the add to cart link seems to be preventing WooCommerce from firing that trigger.
I wonder if there are any alternative solutions out there that might be worth trying that doesn't alter the default add to cart link.
Hi Tom,
I tried to search on the internet but unfortunately I couldn't find anything better.
Any chance you can give me a hand?
Thanks in advance!
Hi there,
try the method provided here:
https://stackoverflow.com/a/48728429
You may just require the Javascript part ( second part of the script ) to make your current method work.