• Respected sir/madam,

    I have added a feature for displaying quantity selectors on the shop/archive pages and also using the WPC product quantity plugin. After selecting the quantity, when I add to the cart using ajax then it adds only 1 item in the cart irrespective of any other qty. I have written a custom js add to cart handler and need to attach it with Fly cart add to cart handler.

    My code is as the following.

    /**
     * Add quantity field on the shop page.
     */
    function bhutatva_shop_page_add_quantity_field() {
    
    	/** @var WC_Product $product */
    	$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', 'bhutatva_shop_page_add_quantity_field', 12 );
    
    /**
     * Add required JavaScript.
     */
    function bhutatva_shop_page_quantity_add_to_cart_handler() {
    
    	wc_enqueue_js( '
    		$(".woocommerce .products").on("click", ".quantity input", function() {
    			return false;
    		});
    		$(".woocommerce .products").on("change input", ".quantity .qty", function() {
    			var add_to_cart_button = $(this).parents( ".product" ).find(".add_to_cart_button");
    			// For AJAX add-to-cart actions
    			add_to_cart_button.data("quantity", $(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=" + $(this).val());
    		});
    		// Trigger on Enter press
    		$(".woocommerce .products").on("keypress", ".quantity .qty", function(e) {
    			if ((e.which||e.keyCode) === 13) {
    				$( this ).parents(".product").find(".add_to_cart_button").trigger("click");
    			}
    		});
    	' );
    
    }
    add_action( 'init', 'bhutatva_shop_page_quantity_add_to_cart_handler' );

    Please let me know what needs to change so that the selected qty gets reflected in the fly cart. Let me know in case of queries. Have a nice day.

    Regards & Thanks
    Indiano

    The page I need help with: [log in to see the link]

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author WPClever

    (@wpclever)

    Hi @thesaintindiano

    Our plugin shows the products in the cart only, doesn’t affect the add-to-cart progress.

    About your code, please try to change this line:

    add_to_cart_button.data("quantity", $(this).val());

    to:

    add_to_cart_button.attr("data-quantity", $(this).val());

    I tested and it works with the Storefront theme https://www.screencast.com/t/tPrVYUikE

    Thread Starter thesaintindiano

    (@thesaintindiano)

    Hi @wpclever;

    Thanks for the answer. It’s working now.

    1) Do I need to change any code for For non-AJAX add-to-cart actions.

    2) woocommerce_quantity_input isn’t able to control min and max quantity. How do I connect it with the WPC quantity plugin to control min/max amount?

    Have a beautiful day.

    Regards & Thanks
    Indiano

    Plugin Author WPClever

    (@wpclever)

    @thesaintindiano

    #1. No, you don’t need to change.

    #2. When using WPC Product Quantity, please try to change this line:

    woocommerce_quantity_input( array( 'min_value' => 1, 'max_value' => $product->backorders_allowed() ? '' : $product->get_stock_quantity() ) );

    to:

    woocommerce_quantity_input( array(), $product );

    Then the min/max/step will be controlled from WPC Product Quantity.

    Thread Starter thesaintindiano

    (@thesaintindiano)

    Hi @wpclever,

    Thanks for the response.

    1) Where can I find the logic to control max quantity in the plugin files? I wanna change logic to suit my requirements eg. $product->get_stock_quantity() >= $product->get_weight()

    2) Why quantity button is not being shown for the last remaining stock quantity?
    eg. If the remaining stock for an item is 1 then the qty button isn’t being shown. I wanna change this behaviour to show qty button even though the quanity can’t be increased.
    https://pasteboard.co/K2AewWD.png

    Have a nice day.

    Regards & Thanks
    Indiano

    Plugin Author WPClever

    (@wpclever)

    #1. Please read more about ‘woocommerce_quantity_input’ function or filter hook ‘woocommerce_quantity_input_max’.

    #2. This isn’t related to our plugin. You can find the reason in the core template of WooCommerce https://www.screencast.com/t/bpyyqstk8cmW

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Where can I find the add to cart handler for quantity field?’ is closed to new replies.