【发布时间】:2021-09-25 22:50:28
【问题描述】:
我正在为 WordPress 驱动的页面制作一个自定义插件,以在某些情况下禁用某个按钮。
上下文:如果用户违反了一定的信用额度,则禁用下订单按钮。
我希望在进入结帐页面时,插件会触发并检查用户是否超过信用额度。如果是,它将提醒用户并禁用下订单按钮。(或创建覆盖或任何其他方法)
现在我有这个:
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.");
}
}
}
唯一的问题是这些函数实际上在页面顶部创建了一个额外的按钮,而不是禁用原始按钮。所以我想知道这是否真的可行,或者我是否必须直接修改html文件才能达到预期目的。(最好不要)
现在,我确实看到了一些类似的问题,但它们都需要在 html 标签中直接应用 php。它不适用于我的情况,因为我正在创建一个 wordpress 自定义插件。 (这是一个单独的 php 文件)。
【问题讨论】:
标签: javascript php wordpress