Reputation: 350
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).
Upvotes: 3
Views: 847
Reputation: 1860
Well I would solve this in two parts.
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;
}
When sufficient credit it shows.
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');
This will disable the button, and you should be good to go.
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.
Upvotes: 2