通过用户 ID 禁用下订单按钮

问题描述 投票:0回答:1

是否可以在 WooCommerce 中通过用户 ID 禁用下订单按钮?

add_filter( 'woocommerce_order_button_html', 'replace_order_button_html', 10, 2 );
function replace_order_button_html( $order_button ) {

        if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    $wpuser_object = wp_get_current_user_id();
    $allowed_users = array( 1, 2);

    if ( array_intersect($allowed_users, $wpuser_object->id) ){
    
    $order_button_text = __( "Place Order", "woocommerce" );

    $style = ' style="color:#fff;cursor:not-allowed;background-color:#999;"';
    return '<a class="button alt"'.$style.' name="woocommerce_checkout_place_order" id="place_order" >' . esc_html( $order_button_text ) . '</a>';
}
}
php woocommerce
1个回答
0
投票

您的代码中存在一些错误,请尝试以下简化和修改后的代码:

add_filter( 'woocommerce_order_button_html', 'disable_conditionally_order_button_html', 10, 2 );
function disable_conditionally_order_button_html( $button_html ) {
    $allowed_users_ids = array( 1, 2 );

    if ( ! in_array( get_current_user_id(), $allowed_users_ids ) ) {
        return str_replace(['type="submit"', 'class="button alt'], ['type="button"', 'class="button alt disabled'], $button_html );
    }
    return $button_html;
}

代码位于子主题的functions.php 文件中(或插件中)。已测试并有效。

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.