Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #1007809

    Hi,
    I would like to have a quantity selector field in the Enfold product boxes in the shop overview page.

    I have allready tried adding the quantity selector via the plugin “Quantity Field on Shop Page for WooCommerce”. This adds a working quantity selector but it puts it in between the Enfold shop button and more info button – maybe with some CSS tweaking this could be the solution… but I couldn’t fix the positioning of the quantity field.

    #1008227

    Hey Yannick77,

    Please try to add this code to the child theme functions.php:

    
    /**
     * 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( ".type-product" ).on( "click", ".quantity input", function() {
    			return false;
    		});
    		jQuery( ".type-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' );
    

    Another user reported it worked (see https://kriesi.at/support/topic/add-a-quantity-field-to-woocommerce-products/#post-690610 – I already modified the code for you [new class name]).

    Best regards,
    Peter

    #1008491

    Hi Peter,
    thanks it worked, I had to play with CSS too make the positioning of the quantity field look better.

    #1008500

    Hi,

    I’m glad you got this resolved. If you need additional help, please let us know here in the forums.

    Best regards,
    Jordan Shannon

Viewing 4 posts - 1 through 4 (of 4 total)
  • The topic ‘Adding Quantity field in shop overview page – woocommerce’ is closed to new replies.