I use a script to change the qty in the product view page and in the shopping cart page but I have a problem. when I decrease the qty instead the latest value to be 0 is -1. How I can stop the decrease button to remain on the 0 value?
require([
'jquery'
], function ($) {
$(document).ready(function(){
/********************* Qty Holder **************************/
$(document).on("click", ".qtyplus", function(e) {
// Stop acting like a button
e.preventDefault();
// Get its current value
var currentVal = parseInt($(this).parents('.qty-changer').find('input.input-text.qty').val());
// If is not undefined
if (!isNaN(currentVal)) {
// Increment
$(this).parents('.qty-changer').find('input.input-text.qty').val(currentVal + 1);
$(this).parents('.qty-changer').find('.update-cart-item').show();
} else {
// Otherwise put a 0 there
$(this).parents('.qty-changer').find('input.input-text.qty').val(1);
}
});
// This button will decrement the value till 0
$(document).on("click", ".qtyminus", function(e) {
// Stop acting like a button
e.preventDefault();
// Get the field name
fieldName = $(this).attr('field');
// Get its current value
var currentVal = parseInt($(this).parents('.qty-changer').find('input.input-text.qty').val());
// If it isn't undefined or its greater than 0
if (!isNaN(currentVal) && currentVal >= 0) {
// Decrement one
$(this).parents('.qty-changer').find('input.input-text.qty').val(currentVal - 1);
$(this).parents('.qty-changer').find('.update-cart-item').show();
} else {
// Otherwise put a 0 there
$(this).parents('.qty-changer').find('input.input-text.qty').val(1);
}
});
$(".qtyplus").unbind('click').click(function(){
if($(this).parent().parent().children(".control").children("input.input-text.qty").is(':enabled')){
$(this).parent().parent().children(".control").children("input.input-text.qty").val((+$(this).parent().parent().children(".control").children("input.input-text.qty").val() + 1) || 0);
$(this).parent().parent().children(".control").children("input.input-text.qty").trigger('change');
$(this).focus();
}
});
$(".qtyminus").unbind('click').click(function(){
if($(this).parent().parent().children(".control").children("input.input-text.qty").is(':enabled')){
$(this).parent().parent().children(".control").children("input.input-text.qty").val(($(this).parent().parent().children(".control").children("input.input-text.qty").val() - 1 > 0) ? ($(this).parent().parent().children(".control").children("input.input-text.qty").val() - 1) : 0);
$(this).parent().parent().children(".control").children("input.input-text.qty").trigger('change');
$(this).focus();
}
});
});
});
Thank you