Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #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/

    #692012

    Hey,

    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,
    Josue

    #692560

    Thanks 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

    #860575

    hello 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?

    image

    #861921

    Hi vartgraphic,

    Here is a solution for you

    Show Product Price times Selected Quantity on WooCommerce Product Page

    If you need further assistance please let us know.
    Best regards,
    Victoria

    #1224295

    Hi 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/

    #1224297

    Ow 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, 4 months ago by Yannick77.
    #1224302

    Hi,

    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

Viewing 8 posts - 1 through 8 (of 8 total)
  • The topic ‘Add a quantity field to woocommerce products’ is closed to new replies.