问题
WooCommerce Add To Cart Button text change using jQuery if Quantity increase.
jQuery(document).on("change",'.minus', function() {
var productquantity = jQuery('.qty').attr('value');
if (productquantity == 15) {
jQuery('.single-product').each(function() {
jQuery(".single_add_to_cart_button").text("REQUEST A QUOTE");
});
}
})
While selecting buttons value never changes :S
HTML FOR BUTTON AND SELECTOR
<div class="quantity buttons_added">
<input type="button" value="-" class="minus button is-form"><input type="number" step="1" min="1" max="9999" name="quantity" value="1" title="Qty" class="input-text qty text" size="4" pattern="[0-9]*" inputmode="numeric"><input type="button" value="+" class="plus button is-form"></div>
<button type="submit" name="add-to-cart" value="1520" class="single_add_to_cart_button button alt" style="display: block !important">Add to Cart</button>
jsFiddle
回答1:
$(document).ready(function ()
{
$('body').on("click",'.minus',function ()
{
var qtyval = $('.qty').val();
if(qtyval >= 0)
{
var newQTY = parseInt(qtyval) - parseInt(1);
if(newQTY >= 15)
{
$('.single_add_to_cart_button').text("Request a Quote");
}
if(newQTY < 15)
{
$('.single_add_to_cart_button').text("Add to Cart");
}
$('.qty').val(newQTY);
}
});
$('body').on("click",'.plus',function ()
{
var qtyval = $('.qty').val();
var newQTY = parseInt(qtyval) + parseInt(1);
if(newQTY >= 15)
{
$('.single_add_to_cart_button').text("Request a Quote");
}
$('.qty').val(newQTY);
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"></script>
<div class="quantity buttons_added">
<input type="button" value="-" class="minus button is-form"><input type="number" step="1" min="1" max="9999" name="quantity" value="1" title="Qty"
class="input-text qty text" size="4" pattern="[0-9]*" inputmode="numeric"><input type="button" value="+" class="plus button is-form"></div>
<button type="submit" name="add-to-cart" value="1520" class="single_add_to_cart_button button alt" style="display: block !important">Add to Cart</button>
回答2:
jQuery(document).on("click",'.plus, .minus', function() {
var productquantity = jQuery('.qty').attr('value');
if (productquantity == 15) {
jQuery('.single-product').each(function() {
jQuery(".single_add_to_cart_button").text("REQUEST A QUOTE");
});
} else if(productquantity != 15) {
jQuery('.single-product').each(function() {
jQuery(".single_add_to_cart_button").text("ADD TO CART NOW");
});
}
})
Only problem was on change to onclick.
来源:https://stackoverflow.com/questions/45033017/woocommerce-add-to-cart-button-text-change-using-jquery-if-quantity-increase