如果价格为0,00€

时间:2016-02-29 14:02:08

标签: javascript jquery

如果我有价格span.amount 0,00€我如何禁用其他div中的button.single_add_to_cart_button?

看下面的代码:

<form class="cart" enctype="multipart/form-data" method="post" novalidate="novalidate">
    <div id="tm-extra-product-options" class="tm-extra-product-options tm-custom-prices tm-product-id-7045 tm-cart-main" data-product-id="7045" data-cart-id="main">
        <div class="tm-totals-form-main" data-product-id="7045">
            <input class="cpf-product-price" type="hidden" name="cpf_product_price" value="0">
            <div id="tm-epo-totals" class="tm-epo-totals tm-custom-prices-total tm-cart-main" data-variations="[]" data-variations-subscription-period="[]" data-subscription-period="" data-variations-subscription-sign-up-fee="[]" data-subscription-sign-up-fee="0" data-prices-include-tax="" data-tax-display-mode="excl" data-tax-string="" data-tax-rate="22" data-taxable="1" data-force-quantity="0" data-tm-epo-dpd-suffix="" data-tm-epo-dpd-prefix="" data-fields-price-rules="0" data-product-price-rules="[]" data-price="0" data-type="simple" data-is-sold-individually="" data-is-subscription="" data-cart-id="main" data-theme-name="">
                <dl class="tm-extra-product-options-totals tm-custom-price-totals">
                    <dt class="tm-options-totals">Options amount</dt>
                    <dd class="tm-options-totals">
                        <dt class="tm-final-totals">Prezzo Totale:</dt>
                        <dd class="tm-final-totals">
                            <span class="amount final">0,00€</span>
                        </dd>
                </dl>
            </div>
        </div>
        <div class="iva_esc">
            <div class="quantity">
                <input type="hidden" value="7045" name="add-to-cart">
                <button class="single_add_to_cart_button button alt" type="submit" style="display: block;">Vai al pagamento</button>

其他用户,已向我发送此代码:

<script>
$(function() {
  //check if all items are zero
  var disable = $(".cart span.amount.final").toArray().every(function(item) {
    return $(item).text() === "0,00€";
  });

  //disable button if necessary
  $(".cart button.single_add_to_cart_button").prop("disabled", disable);
}); 
</script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

这段代码很完美,但现在我需要在价格不同于0,00时才能激活

4 个答案:

答案 0 :(得分:1)

使用jquery之类的东西:

var value = $('.amount').text();
if(value === '0,00€') {
  $('.single_add_to_cart_button').hide();
} else { 
  $('.single_add_to_cart_button').show();
}

答案 1 :(得分:0)

var price = $(span.amount).val().split( '').map(Number)

这将分割任何不是数字的内容,因此您可以使用以下条件:

if(price >= 0 )
   // do smth
else
   // do smth else

答案 2 :(得分:0)

使用jQuery:

<script>
    var amount = parseFloat($('.amount').html().replace(",", "."));
    if(amount === 0){
        $('.single_add_to_cart_button').prop('disabled', true);
    }else{
        $('.single_add_to_cart_button').prop('disabled', false);                
    }
</script>

JSFiddle Demo

答案 3 :(得分:0)

在WordPress 5.1.1 WooCommerce 3.5.5中,隐藏等于0.00美元的价格,隐藏“添加到购物车”按钮并隐藏数量。

jQuery(document).ready(function($) {

    var ptext = $(this).find('span.amount').text();
    ptext = ptext.replace('$', '');
    var nptext = parseInt(ptext);
    if (nptext === 0) {
        $('.quantity').hide();
        $('.single_add_to_cart_button').hide();
        $('.amount').hide();
    } else {}

});

在WordPress 5.1.1 WooCommerce 3.5.5中,这是完整的代码,将$ 0.00替换为文本,隐藏数量,并将按钮替换为单击以调用图像。

jQuery(document).ready(function($) {

    var ptext = $(this).find('span.amount').text();
    ptext = ptext.replace('$', '');
    var nptext = parseInt(ptext);
    if (nptext === 0) {
        $('.quantity').hide();
        $('.single_add_to_cart_button').replaceWith('<a href="tel:1-877-992-3377"><img src="/wp-content/uploads/2019/03/fine-jewelry-call-button.jpg"/></a>');
        $('.amount').html('<span style="color:#8160C4;font-size:18px;">Call <a href="tel:1-877-992-3377" style="color:#8160C4;font-weight:bold;font-size:22px;">877-992-3377</a> for Promotion Price</span>');
    } else {}
});