Skip to content

Instantly share code, notes, and snippets.

@woogists
Last active July 9, 2021 09:53
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save woogists/c5e5d73bb4d7d44f5642313b8094937c to your computer and use it in GitHub Desktop.
Save woogists/c5e5d73bb4d7d44f5642313b8094937c to your computer and use it in GitHub Desktop.
[Frontend Snippets] Override loop template and show quantities next to add to cart buttons.
/**
* 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;
}
@mathieurdm
Copy link

Hi,
Great code! How can I've the price on the same line than quantiies? ( -Qty+ 5,55$ on same line)
Cheers

@Yvon-Medesse
Copy link

hello
i put the code in my function.php, but it don't work

@Spreeuw
Copy link

Spreeuw commented Aug 31, 2018

Even prettier, using the internal template:

/**
 * 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 = wc_get_template_html( 'single-product/add-to-cart/simple.php' );
	}
	return $html;
}

@takroohamid
Copy link

Hi dear friend Not compatible with Woodmart template !!What to do

@slavakudr
Copy link

After adding the code, the ajax buttons stopped working and throws to the top of the page. How to remove it? http://mir-charma.ru/shop/

@takroohamid
Copy link

Thanks 💓

@kushanhasantha
Copy link

i have tried this snippet and I keep getting the same error "The snippet has been deactivated due to an error on line 6:

Cannot redeclare function quantity_inputs_for_woocommerce_loop_add_to_cart_link."

@jackbillstrom
Copy link

@kushanhasantha Because it's already defined in functions.php (or in any theme file)

@justbolo
Copy link

justbolo commented May 3, 2020

@Spreeuw
Please let us know how to add a background color for that (- +) box. As for me the add to cart is hovered on the image, so it is not visible to user properly

@demicoli00
Copy link

Hi the code works out for me for manually added products and does not work for imported products using the CSV file. Any ideas what is happening please? Thanks

@nishantturekar
Copy link

Dear Team,

After Adding snippets the button is not alligned well. attached screenshot for your reference,
https://snipboard.io/qhb1ur.jpg

Please help to resolve it

Thanks,
Nishant

@webLoromir
Copy link

Even prettier, using the internal template:

/**
 * 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 = wc_get_template_html( 'single-product/add-to-cart/simple.php' );
	}
	return $html;
}

https://gist.github.com/woogists/c5e5d73bb4d7d44f5642313b8094937c#gistcomment-2694606
Этот способ работает с темой Woodmart советую

@ayoub-bousetta
Copy link

what about a variable product ?

@lgdelai
Copy link

lgdelai commented May 17, 2021

Hi,
I found a possible bug in the code.

On the product page, when UpSell and Related Products are being displayed.

When changing the quantity in one, the others are also changed.

Note this error does not occur with CrossSell products on the Cart page.

Does anyone know what may be causing this?

@jvdurme
Copy link

jvdurme commented Jun 1, 2021

Hi,
I found a possible bug in the code.

On the product page, when UpSell and Related Products are being displayed.

When changing the quantity in one, the others are also changed.

Note this error does not occur with CrossSell products on the Cart page.

Does anyone know what may be causing this?

I noticed this bug today as well. When on the product page, changing the quantities will also change the quantities of related products or upsells with the same increment on the whole page. I'm don't have the skills to fix it. Let's hope for someone who has.

@lgdelai
Copy link

lgdelai commented Jun 4, 2021

I noticed this bug today as well. When on the product page, changing the quantities will also change the quantities of related products or upsells with the same increment on the whole page. I'm don't have the skills to fix it. Let's hope for someone who has.

What a sad situation.
He was very excited about this feature.

I'm looking for the solution elsewhere, but I'm not getting any feedback.

Could you also try in the groups you participate in? maybe be luckier than me.

@lgdelai
Copy link

lgdelai commented Jun 4, 2021

Even prettier, using the internal template:

/**
 * 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 = wc_get_template_html( 'single-product/add-to-cart/simple.php' );
	}
	return $html;
}

https://gist.github.com/woogists/c5e5d73bb4d7d44f5642313b8094937c#gistcomment-2694606
Этот способ работает с темой Woodmart советую

Hey.

On the product page, when UpSell and Related Products are being displayed.

When changing the quantity in one, the others are also changed.

Note this error does not occur with CrossSell products on the Cart page.

Do you know what may be causing this?

@jvdurme
Copy link

jvdurme commented Jun 7, 2021

Hey @lgdelai, what theme are you using. I'm using OceanWP. Could it be a theme thing?

@jvdurme
Copy link

jvdurme commented Jun 12, 2021

I got it all working, but with the plugin https://nl.wordpress.org/plugins/qty-increment-buttons-for-woocommerce/
I removed this above code snippet, then installed the plugin and added some css to fix some style in the related products section:
.woocommerce ul.products li.product .button { float:right; padding: 8px 18px; height:35px; }
Now the quantities change individually, not together.

check my site: https://www.apotheekbogaerts.be/wakken/product/vineactiv-detox-nachtolie-30-ml/

@lgdelai
Copy link

lgdelai commented Jun 15, 2021

Hi,
I found a possible bug in the code.
On the product page, when UpSell and Related Products are being displayed.
When changing the quantity in one, the others are also changed.
Note this error does not occur with CrossSell products on the Cart page.
Does anyone know what may be causing this?

I noticed this bug today as well. When on the product page, changing the quantities will also change the quantities of related products or upsells with the same increment on the whole page. I'm don't have the skills to fix it. Let's hope for someone who has.

I Found the solution.

I was using the GeneratePress theme, and it already had the ability to display the arrows next to the buttons.

I then went into the theme settings and disabled the + and - (theme) buttons so I implemented my own buttons.

In short, I've been disabling the display of buttons next to the quantity field in the theme.

@lgdelai
Copy link

lgdelai commented Jun 15, 2021

Even prettier, using the internal template:

/**
 * 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 = wc_get_template_html( 'single-product/add-to-cart/simple.php' );
	}
	return $html;
}

Hey @Spreeuw

Using the Original code of this post, the Ajax Button stop working.
I Found other way who work with Ajax.

But they cause another problem, with this other solution, the "- +" button doesn't apear on product list showed by a ContentViews Plugin.

The developers of ContentViews told me that:
And our plugin uses the "add_to_cart" shortcode of WooCommerce to show the "Add to cart" button.

What change can I make so that new code to displays the quantities field in contentviews plugin too?

See code: (Works with Ajax, but doesn't display the quantity field in ContentViews).

https://gist.github.com/webaware/6260468

@Spreeuw
Copy link

Spreeuw commented Jun 15, 2021

@lgdelai the add_to_cart shortcode is rendered by WooCommerce with this function:
includes/class-wc-shortcodes.php#L305-L368 and the woocommerce_loop_add_to_cart_link hook is called in the loop/add-to-cart.php template called by that very function.
This means that if this doesn't work in the list generated by the ConventViews plugin, they are loading the shortcode before your filter is applied. You could try putting the filter in an mu-plugin to see if that resolves the issue.

@lgdelai
Copy link

lgdelai commented Jun 15, 2021

@Spreeuw thanks for your explanations.

Could you explain better what the mu-plugin you mentioned is about?

I didn't quite understand what I need to try to do.

@Spreeuw
Copy link

Spreeuw commented Jun 16, 2021

it's an alternative method of adding custom code to your sites (compared to the 'traditional' method of using functions.php in your theme): mu-plugins load before regular plugins. This was the first google result:
https://wordpress.org/support/article/must-use-plugins/
but there are many other resources and tutorials.

Alternatively you could try the Code Snippets plugin, which also loads filters before functions.php and most plugins.

It's still possible the root cause lies somewhere else (for example, when the woocommerce_loop_add_to_cart_link is missing from the loop/add-to-cart.php template of your theme), or something else sepecific to ContentViews, but the developers of that plugin should be able to help with this. I'm not with WooCommerce and not the author of the original gist, just provided an alternative solution.

@lgdelai
Copy link

lgdelai commented Jun 16, 2021

@Spreeuw
I find a plugin who works fine in all situations and is ajax compatible.

https://br.wordpress.org/plugins/quantity-field-on-shop-page-for-woocommerce/

He is capable of show quantity field a side of add to cart button.

Thanks all for help.

@scienceandnature
Copy link

I am using
/** * 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;}

The number picker works fine but it overrides the ajax add to cart to throw to the top of the page instead of adding: View cart
Can anyone suggest a change to code?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment