Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

372
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda