• Efs

    (@stevendigital)


    i have the function below on function.php

    /**
     * Set a minimum order amount for checkout
     */
     add_action( 'woocommerce_checkout_process', 'wc_minimum_order_amount' );
     add_action( 'woocommerce_before_cart' , 'wc_minimum_order_amount' );
    
    function wc_minimum_order_amount($min_amount) {
    // Set this variable to specify a minimum order value
    //$minimum = $('#amount').val(); 
    //$minimum = 30; 
    
    $minimum = $min_amount; 
    
    if ( WC()->cart->total < $minimum ) {
    
        if( is_cart() ) {
    
            wc_print_notice( 
                sprintf( 'Your current order total is %s — you must have an order with a minimum of %s to place your order.' , 
                    wc_price( WC()->cart->total ), 
                    wc_price( $minimum )
                ), 'error' 
            );
    
        } else {
    
            wc_add_notice( 
                sprintf( 'Your current order total is %s — you must have an order with a minimum of %s to place your order.' , 
                    wc_price( WC()->cart->total ), 
                    wc_price( $minimum )
                ), 'error' 
            );
    
        }
    }
    }

    Also, i have an input field and a button

    <form>
     <input type="text" id="amount" name="min_amount_ID"/>
    <input type="button" onclick="wc_minimum_order_amount(this.form.min_amount_ID.value)" class="min_amount"/>
    </form>

    How can i call the function wc_minimum_order_amount() from function.php file on click button?
    Or how i can use the result of the script below inside wc_minimum_order_amount() function and assign it as a result to the variable $minimum ?

       <script type="text/javascript">
     $(document).ready(function(){
       $('.min_amount').on('click', function(){
        var text_val = $('#amount').val();
       //alert(text_val );
          });
     });
    </script>
Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    To get a value on a page from server side script without reloading the entire page, you need to use Ajax. Information on using Ajax that is specific to WP is available in the Plugin Handbook.

    Thread Starter Efs

    (@stevendigital)

    Is there any other way that will involve also the reload of the page? And not use Ajax?

    Moderator bcworkz

    (@bcworkz)

    The button’s onclick handler could call document.location.reload(), but there is no mechanism for the form to remember what the user entered in the field. Do you really need to go to the server to get the cart total? Isn’t it on the current page somewhere? $('#amount').val() maybe? Why wouldn’t you use JavaScript/jQuery to grab the value and decide what message to display based on that? No reload and no Ajax.

    Might it be better to run the price check script on an event that does not require the user to click a button? The onchange event of any fields affecting the total perhaps? Or at least run on the submit order button click, prevent the submission until the total is checked and determined to be adequate.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘How to call a function from functions.php on button click event?’ is closed to new replies.