Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #690610

    Hi,
    I wanted to add a quantity field to woocommerce archive pages and used this snippet from https://shopplugins.com/ (Purchase code hidden if logged out) -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
    http://reigelgallarde.me/programming/ (Purchase code hidden if logged out)(Purchase code hidden if logged out) /

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

Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.