1

The minimum order amount has to be 1500. I try to make add to cart button disabled when the amount is less then needed, and to enable it back when it's 1500 or more. I use this code

add_action( 'woocommerce_single_product_summary', 'woocommerce_total_product_price', 31 );
function woocommerce_total_product_price() {
    global $woocommerce, $product;
    // let's setup our divs
    echo sprintf('<div id="product_total_price" style="margin-bottom:20px;">%s %s</div>',__('Your order amount:','woocommerce'),'<span class="price">'.$product->get_price().'</span><p style="font-weight: bold;">The minimum order amount is<span style="color:red;">1500 ₽</span></p>');
    ?>
        <script>
            jQuery(function($){
                var price = <?php echo $product->get_price(); ?>,
                    currency = '<?php echo get_woocommerce_currency_symbol(); ?>';

                $('[name=quantity]').change(function(){
                        if ((this.value < 1500)) {
                        var product_total = parseFloat(price * this.value);                        
                         $('.button.single_add_to_cart_button.button.alt.fa-cart').addClass('minimum-disabled');
                        $('#product_total_price').html('Your order amount: '+  product_total.toFixed(2) + ' ' + currency + '<p style="font-weight: bold;">The minimum order amount is <span style="color:red;">1500 ₽</span></p>');                      
                    }
                    else{
                        var product_total = parseFloat(price * this.value);
                        $('.button.single_add_to_cart_button.button.alt.fa-cart').removeClass('minimum-disabled');
                        $('#product_total_price .price').html( product_total.toFixed(2) + ' ' + currency);
                        
                    }
                });
            });
        </script>
    <?php
}

But when the amount is became 1500+, the class 'minimum-disabled' doesn't remove. The button still has it. What can be wrong and how to fix it? CSS of this class

.minimum-disabled{
    pointer-events: none!important;
    background: #dfe1f0!important;
    border-color: #dfe1f0!important; 
    color: rgba(51,51,51,.5); 
    cursor: no-drop!important;
}

The website is here, you can see it.

There's the needed quantity is 53. You can enter 52 and 53 to see the difference.

libertarian
  • 345
  • 1
  • 15

1 Answers1

0

I figured out. I was wrong in if ((this.value < 1500)) It's product quantity, not a price. More actual version is below(I haven't translated strings, but the text is in the question, if it will be needed for someone). I was wrong, but maybe someone will need my idea - to disable this button.

add_action( 'woocommerce_single_product_summary', 'woocommerce_total_product_price', 31 );
function woocommerce_total_product_price() {
    global $woocommerce, $product;
    // let's setup our divs
    echo sprintf('<div id="product_total_price" style="margin-bottom:20px;">%s %s</div>',__('Сумма заказа:','woocommerce'),'<span class="price">'.$product->get_price().'</span><p style="font-weight: bold;">Минимальная сумма заказа <span style="color:red;">1500 ₽</span></p>');
    ?>
        <script>
            jQuery(function($){
                var price = <?php echo $product->get_price(); ?>,
                    currency = '<?php echo get_woocommerce_currency_symbol(); ?>';

                $('[name=quantity]').change(function(){
                     var product_total = parseFloat(price * this.value);

                        if (( product_total < 1500) ) {
                          $('#product_total_price p').removeClass('d-none');                        
                          $('.summary .button.single_add_to_cart_button.button.alt.fa-cart').addClass('minimum-disabled');
                          $('#product_total_price ').html('Cумма заказа: '+  product_total.toFixed(2) + ' ' + currency + '<p style="font-weight: bold;">Минимальная сумма заказа <span style="color:red;">1500 ₽</span></p>');                      
                    }
                    else{                       
                        $('.summary .button.single_add_to_cart_button.button.alt.fa-cart').removeClass('minimum-disabled');
                         $('#product_total_price ').html('Cумма заказа: '+  product_total.toFixed(2) + ' ' + currency); 
                        $('#product_total_price p').addClass('d-none');                        
                        }
                      }); 
                    });
               
        </script>
    <?php
}
libertarian
  • 345
  • 1
  • 15