Home > Blockchain >  How to disable in an button in an if condition
How to disable in an button in an if condition

Time:01-14

i am trying to disable an button when the $needs_minimum_amount is not reached. I have tried to do it with: document.getElementById("checkout-button button alt wc-forward").disabled = true;. but didn't quite get it to work. Could anyone help?

function wc_minimum_order_amount() {
##  SETTINGS  ##
$minimum_amount = 13; // Define a minimum order amount
$category_ids   = array( 25, 28, 29 ); // Define your category ids in the array (or an empty array to disable)
$product_ids    = array(); // Define your product ids in the array (or an empty array to disable)



// Only on cart or checkout pages
if( WC()->cart->is_empty() || ! ( is_cart() || is_checkout() ) ) 
    return; // Exit

$total_amount = WC()->cart->subtotal; // Items subtotal including taxes

if ( $total_amount < $minimum_amount ) {
    $needs_minimum_amount = false; // Initializing

    // Loop through cart items
    foreach ( WC()->cart->get_cart() as $cart_item ) {
        $product_id   = $cart_item['product_id'];
        $variation_id = $cart_item['variation_id'];
        
        // 1. Check for matching product categories
        if( sizeof($category_ids) > 0 ) {
            $taxonomy = 'product_cat';

            if ( has_term( $category_ids, $taxonomy, $product_id ) ) { 
                $needs_minimum_amount = true;
                break;
            }
        }

        // 2. Check for matching product Ids
        if( sizeof($product_ids) > 0 ) {
            if ( array_intersect( $product_ids, array($product_id, $variation_id) ) ) { 
                $needs_minimum_amount = true;
                break; // Stop the loop
            }
        }
    }

    if( $needs_minimum_amount ) {
        wc_print_notice( sprintf( 
            __("Minimale bestelling voor sweets is 12,50 voor een bestelling."), 
            wc_price( $minimum_amount ), 
            wc_price( $total_amount )
        ), 'error' );
        
    }
}

}

And this is the button that i am trying to disable:

<a href="https://smileyscandyshop.nl/afrekenen/" >
Doorgaan naar afrekenen</a>

CodePudding user response:

<input type="button" value="Add to Cart" <?php if ($prod_qty == '0'){ ?> disabled <?php   } ?> onclick="addtocart(<?php echo $row["prod_id"]?>)" />

CodePudding user response:

You should really give more information to your project. The only thing I can think of is, that you try using document.getElementById("checkout-button button alt wc-forward").setAttribute("disabled","disabled");

  •  Tags:  
  • Related