3

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).

1 Answer 1

2

Well I would solve this in two parts.

Step 1 . Adding proper 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 sufficient credit it shows. enter image description here

Step 2 . Restricting 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 guest in your code, change it from your woocommerce setting. In Account setting mark it compuslory to login when checking out.

PS : For extra security use woocommerce_checkout_update_order_review to check if its a valid request because someone can create a submit button using developer tool.

Sign up to request clarification or add additional context in comments.

3 Comments

Hey bhanu, thanks for the detailed response, I'll get back to you. Thanks for raising concerns over security issues, rest assured I am aware of it. And I was just testing out changes on an unpublished website.
Modified and tested, and it worked like a charm! Thanks for the guide bhanu! I have accepted your response as the answer. I guess what I needed was more time on woocommerce documentations. Do you have a recommended channel for it?
@seraph As WooCommerce is pretty old, stackoverflow has become a pretty good acrchive of questions. Most of the time a "good" google search will get you your hook. In other situations when you don't I like to go inside the WooCommerce plugin and look at the code which I am trying to alter, and try to see is any filter or hook is defined that I can use. This works with all wordpress plugins. Some good links are : developer.woocommerce.com , woocommerce.github.io/code-reference/index.html.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.