Tagged: add to cart, Product page, woocommerce
-
AuthorPosts
-
September 23, 2016 at 11:09 am #690610
Hi,
I wanted to add a quantity field to woocommerce archive pages and used this snippet from https://shopplugins.com/add-a-quantity-field-to-the-woocommerce-archive-pages//** * Add quantity field on the archive page. */ function custom_quantity_field_archive() { $product = wc_get_product( get_the_ID() ); if ( ! $product->is_sold_individually() && 'variable' != $product->product_type && $product->is_purchasable() ) { woocommerce_quantity_input( array( 'min_value' => 1, 'max_value' => $product->backorders_allowed() ? '' : $product->get_stock_quantity() ) ); } } add_action( 'woocommerce_after_shop_loop_item', 'custom_quantity_field_archive', 0, 9 ); /** * Add requires JavaScript. */ function custom_add_to_cart_quantity_handler() { wc_enqueue_js( ' jQuery( ".post-type-archive-product" ).on( "click", ".quantity input", function() { return false; }); jQuery( ".post-type-archive-product" ).on( "change input", ".quantity .qty", function() { var add_to_cart_button = jQuery( this ).parents( ".product" ).find( ".add_to_cart_button" ); // For AJAX add-to-cart actions add_to_cart_button.data( "quantity", jQuery( this ).val() ); // For non-AJAX add-to-cart actions add_to_cart_button.attr( "href", "?add-to-cart=" + add_to_cart_button.attr( "data-product_id" ) + "&quantity=" + jQuery( this ).val() ); }); ' ); } add_action( 'init', 'custom_add_to_cart_quantity_handler' );
It was working before but after the latest updates from WordPress and Enfold it add’s only one article to the shopping cart instead of for example 3 if I select 3.
Or sometimes when you select more then one item in related articles it will go to the product page instead of adding the article to the shopping cart when you hit ‘add to shoppingcart’
I have no idear where it’s going wrong, could you take a look?
The url is: http://kweker.nl/product/ekoland/ (product page), shopping page: http://kweker.nl/kerst/kerstpakketten/September 27, 2016 at 12:08 am #692012Hey,
Try changing this:
add_action( 'woocommerce_after_shop_loop_item', 'custom_quantity_field_archive', 0, 9 );
To:
add_action( 'woocommerce_after_shop_loop_item', 'custom_quantity_field_archive', 0, 999 );
So the buttons sit outside the
a
tag because when you try to interact with them it gets you to the product single view.Best regards,
JosueSeptember 28, 2016 at 11:31 am #692560Thanks Josue, that helped. But the real problem was that the class name in
jQuery( ".post-type-archive-product" ).on( "click", ".quantity input", function() {
did not exist anymore.
When I changed post-type-archive-product to type-product, which does exist, it was working.Cheers, Ariane
October 5, 2017 at 12:46 pm #860575hello to all, I want to make the total visible according to the quantities directly on the product page, before clicking the button add to the cart, how can i do it?
October 9, 2017 at 2:49 pm #861921Hi vartgraphic,
Here is a solution for you
If you need further assistance please let us know.
Best regards,
VictoriaJune 20, 2020 at 6:55 pm #1224295Hi all since a few days the quantity field on the shop overview page stopped working and only one product is added to the basket.
Could you please help website http://www.mosa-mosa.be + also another one that is still in development
ps: this was my old post requesting the functionality https://kriesi.at/support/topic/adding-quantity-field-in-shop-overview-page-woocommerce/
June 20, 2020 at 7:06 pm #1224297Ow I solved it – I found this PHP code and it worked again, the JQueri part is changed
/** * Add quantity field on the archive page. */ function custom_quantity_field_archive() { $product = wc_get_product( get_the_ID() ); if ( ! $product->is_sold_individually() && 'variable' != $product->product_type && $product->is_purchasable() ) { woocommerce_quantity_input( array( 'min_value' => 1, 'max_value' => $product->backorders_allowed() ? '' : $product->get_stock_quantity() ) ); } } add_action( 'woocommerce_after_shop_loop_item', 'custom_quantity_field_archive', 15, 9 ); function custom_add_to_cart_quantity_handler() { wc_enqueue_js( ' jQuery( "body" ).on( "click", ".quantity input", function() { return false; }); jQuery( "body" ).on( "change input", ".quantity .qty", function() { var add_to_cart_button = jQuery( this ).parents( ".product" ).find( ".add_to_cart_button" ); // For AJAX add-to-cart actions add_to_cart_button.attr( "data-quantity", jQuery( this ).val() ); // For non-AJAX add-to-cart actions add_to_cart_button.attr( "href", "?add-to-cart=" + add_to_cart_button.attr( "data-product_id" ) + "&quantity=" + jQuery( this ).val() ); }); ' ); } add_action( 'init', 'custom_add_to_cart_quantity_handler' );
- This reply was modified 4 years, 6 months ago by Yannick77.
June 20, 2020 at 8:04 pm #1224302Hi,
I’m glad this was resolved for you. If you need additional help, please let us know here in the forums.
Best regards,
Jordan Shannon -
AuthorPosts
- The topic ‘Add a quantity field to woocommerce products’ is closed to new replies.