[Support request] Product quantity in category view

Home Forums Support [Support request] Product quantity in category view

Home Forums Support Product quantity in category view

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #1636084
    Alberto

    Hi.

    You would need to put the product quantity in the product category view, something similar to what is shown in the image.

    It’s possible?

    Thank you in advance.

    Capture

    #1636287
    Leo
    Staff
    Customer Support

    Hi there,

    That is handled by the WooCommerce plugin itself so you will need to check with their support team.

    It will require modifying their product category add to cart output unfortunately.

    #1636749
    Alberto

    Woocommerce support gave me the following code to add to the functions.php file.

    I don’t want to edit this file … could it be added in another way, for example using hooks?

    Thank you in advance

    
    <?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->get_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.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' );
    #1636809
    David
    Staff
    Customer Support

    Hi there,

    that needs to go in a Child Themes functions.php or you can add it using the Code Snippets plugin.

    #1636813
    Alberto

    Thanks for your answer. Would you tell me how I have to proceed using Code Snippets? Thank you!

    #1637100
    David
    Staff
    Customer Support

    Install this plugin:

    https://wordpress.org/plugins/code-snippets/

    It will add a Snippets menu to your Dashboard, create a New Snippet. Paste in that code, and Save 🙂

Viewing 6 posts - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.