Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

371
Views
Disabling HTML button on condition with PHP (WordPress Plugin)

I'm making a custom plugin for a WordPress powered page to disable a certain button on condition.

Context: To disable place order button if the user breached a certain credit limit.

I'm expecting that upon entering checkout page, the plugin will fire and check if the user exceeded the credit limit. If yes, it will then alert the user and disable the place order button.(OR create an overlay OR any other methods)

For now I have this:

function credit_limit_check()
{
    /** page 13 is the check out page ID*/
    if (is_page(13)) {
        if (is_user_logged_in()) {
            /* For reference purpose*/
            /* get_user_meta( int $user_id, string $key = '', bool $single = false ) */
            $ID = get_current_user_id();

            $credit_val = get_user_meta($ID, 'credit_limit', true);
            $outstanding_val = get_user_meta($ID, 'outstanding_credit', true);

            $credit_breach = $credit_val - $outstanding_val;

            
             if ($credit_breach <= 0) {
                 /*disable checkout button if not enough credit limit*/
                 echo '<button type="submit" class="button alt" name="woocommerce_checkout_place_order" id="place_order" value="Place order" data-value="Place order" disabled>Place order</button>';
                 echo '<script>alert("You have exceeded your credit limit, please make sure you have no outstanding bill")</script>';
             } else {
                 print_r("Huzzah! Good news! You have enough credit to proceed!");
             }
        } else {
            print_r("Please login with your account before proceeding.");
        }
    }

}

The only problem is that the functions actually creates an extra button on top of the page instead of disabling the original button. So I am wondering if this is actually doable, or do I have to modify the html files directly to achieve what is intended.(Preferably not)

Now, I do see some similar questions, but all of them require directly applying php in the html tags. It is not applicable for my situation as I am creating a wordpress custom plugin. (Which is an individual php file).

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Well, I would solve this in two parts.

Step 1 . Adding appropriate notices.

 function add_notices_for_checkout_credit() { if(is_checkout()) { if (is_user_logged_in()) { $ID = get_current_user_id(); $credit_breach = getUserCredit($ID); if ($credit_breach <= 0) { wc_add_notice( 'You have exceeded your credit limit, please make sure you have no outstanding bill', 'error' ); } else { wc_add_notice( 'Huzzah! Good news! You have enough credit to proceed!', 'success' ); } } } } add_action( 'wp', 'add_notices_for_checkout_credit'); function getUserCredit($ID) { $credit_val = get_user_meta($ID, 'credit_limit', true); $outstanding_val = get_user_meta($ID, 'outstanding_credit', true); $credit_breach = $credit_val - $outstanding_val; return $credit_breach; }

With less credit it shows. enter image description here

When enough credit is shown. enter image description here

Step 2 . restriction button

 function change_button($order_button) { if (is_user_logged_in()) { $ID = get_current_user_id(); $credit_breach = getUserCredit($ID); if ($credit_breach <= 0) { $order_button_text = __( "No Credit", "woocommerce" ); $style = ' style="color:#fff;cursor:not-allowed;background-color:#999;text-align:center"'; return '<a class="button alt"'.$style.' name="woocommerce_checkout_place_order" id="place_order" >' . esc_html( $order_button_text ) . '</a>'; } } return $order_button; } add_filter( 'woocommerce_order_button_html', 'change_button');

enter image description here

This will disable the button, and you should be good to go.

Don't check for guests in your code, change it from your woocommerce settings. In the account settings, mark it as required to log in when making the payment.

PS: For added security, use woocommerce_checkout_update_order_review to check if it's a valid request because someone can create a submit button using the dev tool.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!