WooCommerce Add To Cart Button text change using jQuery if Quantity increase

跟風遠走 提交于 2021-01-29 09:42:55

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!