• Hello,

    We are using the increment plug-in and it is great. Only we have a problem now on our custom category page where the increment buttons are loaded. In the loop you can add simple products directly to your cart with a quantity input field we added with this function:

    add_filter( 'woocommerce_loop_add_to_cart_link', 'quantity_inputs_for_loop_ajax_add_to_cart', 10, 2 );
    function quantity_inputs_for_loop_ajax_add_to_cart( $html, $product ) {
        if ( $product && $product->is_type( 'simple' ) && $product->is_purchasable() && $product->is_in_stock() && ! $product->is_sold_individually() ) {
            // Get the necessary classes
            $class = implode( ' ', array_filter( array(
                'button',
                'product_type_' . $product->get_type(),
                $product->is_purchasable() && $product->is_in_stock() ? 'add_to_cart_button' : '',
                $product->supports( 'ajax_add_to_cart' ) ? 'ajax_add_to_cart' : '',
            ) ) );
    
            // Embedding the quantity field to Ajax add to cart button
            $html = sprintf( '%s<a rel="nofollow" href="%s" data-quantity="%s" data-product_id="%s" data-product_sku="%s" class="%s">%s</a>',
                woocommerce_quantity_input( array(), $product, false ),
                esc_url( $product->add_to_cart_url() ),
                esc_attr( isset( $quantity ) ? $quantity : 1 ),
                esc_attr( $product->get_id() ),
                esc_attr( $product->get_sku() ),
                esc_attr( isset( $class ) ? $class : 'button' ),
                esc_html( $product->add_to_cart_text() )
            );
        }
        return $html;
    }

    Only when setting the value to 4 it only adds one product.

    Any idea why this is happening?

Viewing 1 replies (of 1 total)
  • Plugin Author taisho

    (@taisho)

    Hello,

    I’m sorry for the late answer. Adding the quantity field is one thing, it will cause the plugin to detect it as a quantity field, create the buttons, and apply CSS.

    The posted code we can say is more or less what this code in the plugin does for archive pages where there are no quantity fields by default:

    	function qib_quantity_field_archive() {		
    		$product = wc_get_product( get_the_ID() );
    		if ( $product->is_purchasable() && ! $product->is_sold_individually() && $product->is_in_stock() && 'variable' != $product->get_type() && 'bundle' != $product->get_type() ) {
    			woocommerce_quantity_input( [ 'min_value' => 1, 'max_value' => $product->backorders_allowed() ? '' : $product->get_stock_quantity() ] );
    		}
    	}	

    Another step is to actually link the quantity change within the input field to the quantity that will be passed when adding the product to the cart – it’s in Add to cart button properties. The below action happens only on the pages recognized as archive pages according to plugin settings:

    		// Add script that allows adding custom quantity on Add to cart button click for archive pages.
    		add_action( 'template_redirect', 'qib_add_to_cart_quantity_handler' );

    And below the function assigned:

    	function qib_add_to_cart_quantity_handler() {
    		
    		wc_enqueue_js( '
    		
    			jQuery(document).on( "click", ".quantity input", function() {
    				return false;
    			});
    			
    			jQuery(document).on( "change input", ".quantity .qty", function() {					
    				
    				var add_to_cart_button = jQuery( this ).closest( ".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() );				
    			});
    			
    		' );
    
    	}

    Best regards,

    Ryszard

    • This reply was modified 4 years, 7 months ago by taisho.
Viewing 1 replies (of 1 total)
  • The topic ‘Only one product added’ is closed to new replies.