【问题标题】:Avoid displaying a checkout notice to a specific user role避免向特定用户角色显示结帐通知
【发布时间】:2017-05-30 07:34:44
【问题描述】:

在我的 WooCommerce 网店中,我使用了一个显示结帐通知的插件,具有以下功能:

public function action_add_checkout_notice() {
    $balance        = (float) get_user_meta( get_current_user_id(), 'affwp_wc_credit_balance', true );
    $cart_coupons   = WC()->cart->get_applied_coupons();
    $coupon_applied = $this->check_for_coupon( $cart_coupons );

    $notice_subject = __( 'You have an account balance of', 'affiliatewp-store-credit' );
    $notice_query   = __( 'Would you like to use it now', 'affiliatewp-store-credit' );
    $notice_action  = __( 'Apply', 'affiliatewp-store-credit' );

    // If the user has a credit balance,
    // and has not already generated and applied a coupon code
    if( $balance && ! $coupon_applied ) {
        wc_print_notice(
            sprintf( __( '%1$s <strong>%2$s</strong>. %3$s <a href="%4$s" class="button">%5$s</a>' ) ,
                $notice_subject,
                wc_price( $balance ),
                $notice_query,
                add_query_arg( 'affwp_wc_apply_credit', 'true', WC()->cart->get_checkout_url() ),
                $notice_action
            ),
        'notice' );
    }
}

我想向所有用户角色显示结帐通知,“订阅者”用户角色除外。我尝试过没有成功。

我怎样才能做到这一点?

谢谢。

【问题讨论】:

    标签: php wordpress woocommerce checkout user-roles


    【解决方案1】:

    您可以尝试从当前用户那里获取用户对象和当前角色。然后您将在现有条件下使用它,即显示检查通知。

    这是自定义代码:

    public function action_add_checkout_notice() {
        $balance        = (float) get_user_meta( get_current_user_id(), 'affwp_wc_credit_balance', true );
        $cart_coupons   = WC()->cart->get_applied_coupons();
        $coupon_applied = $this->check_for_coupon( $cart_coupons );
    
        $notice_subject = __( 'You have an account balance of', 'affiliatewp-store-credit' );
        $notice_query   = __( 'Would you like to use it now', 'affiliatewp-store-credit' );
        $notice_action  = __( 'Apply', 'affiliatewp-store-credit' );
    
        ## ## Getting The User object and role ## ##
        $user = wp_get_current_user();
        $user_roles = $user->roles;
    
        // If the user has a credit balance,
        // and has not already generated and applied a coupon code
        ## (+) if user role isn’t 'subscriber' ##
        if( $balance && ! $coupon_applied && !in_array('subscriber', $user_roles)) { 
            wc_print_notice(
                sprintf( __( '%1$s <strong>%2$s</strong>. %3$s <a href="%4$s" class="button">%5$s</a>' ) ,
                    $notice_subject,
                    wc_price( $balance ),
                    $notice_query,
                    add_query_arg( 'affwp_wc_apply_credit', 'true', WC()->cart->get_checkout_url() ),
                    $notice_action
                ),
            'notice' );
        }
    }
    

    由于我无法真正对其进行测试,因此我不确定 100% 是否可行……

    【讨论】:

    • 太棒了!它完美无缺!非常感谢 LoicTheAztec!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多