【问题标题】:Disabling HTML button on condition with PHP (WordPress Plugin)使用 PHP(WordPress 插件)禁用 HTML 按钮
【发布时间】: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


    【解决方案1】:

    我会分两部分解决这个问题。

    第 1 步。添加适当的通知。

    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;
    }
    

    它显示的信用较少。

    当有足够的信用时,它就会显示出来。

    第 2 步。限制按钮

    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');
    

    这将禁用按钮,您应该一切顺利。

    不要在您的代码中检查来宾,从您的 woocommerce 设置中更改它。在帐户设置中标记结帐时必须登录。

    PS:为了额外的安全,请使用woocommerce_checkout_update_order_review 来检查它是否是一个有效的请求,因为有人可以使用开发者工具创建一个提交按钮。

    【讨论】:

    • 嗨,bhanu,感谢您的详细回复,我会尽快回复您。感谢您提出对安全问题的担忧,请放心,我知道这一点。我只是在一个未发布的网站上测试更改。
    • 经过修改和测试,效果非常棒!感谢您的指导bhanu!我已接受您的回复作为答案。我想我需要更多时间在 woocommerce 文档上。你有推荐的频道吗?
    • @seraph 由于 WooCommerce 已经很老了,stackoverflow 已经成为一个很好的问题库。大多数时候,“好”的谷歌搜索会让你上钩。在您不喜欢的其他情况下,我喜欢进入 WooCommerce 插件并查看我要更改的代码,并尝试查看是否定义了我可以使用的任何过滤器或挂钩。这适用于所有 wordpress 插件。一些好的链接是:developer.woocommerce.comwoocommerce.github.io/code-reference/index.html
    猜你喜欢
    • 1970-01-01
    • 2015-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-06
    • 2011-09-29
    相关资源
    最近更新 更多