繁体   English   中英

根据用户在 WooCommerce 中的总花费显示自定义文本

[英]Show custom text based on total spent by user in WooCommerce

所以我想在 WooCommerce 上运行一个 Tier 程序,我需要根据网站上的总花费向用户显示 Tier 级别。

我正在使用如何获取用户(客户)在 WooCommerce 中花费的总金额? 答案代码

我有 4 个层级,所以我想显示用户是否花费了超过 600 美元的一些文字,例如“恭喜你现在是第 1 层”等等。

我感谢您的帮助

您可以添加if条件。 试试下面的代码。

function get_user_total_spent( $atts ) {

    extract( shortcode_atts( array(
        'user_id' => get_current_user_id(),
    ), $atts, 'user_total_spent' ) );

    if( $user_id > 0 ) {
        $customer = new WC_Customer( $user_id ); // Get WC_Customer Object

        $total_spent = $customer->get_total_spent(); // Get total spent amount

        if( $total_spent > 600 && $total_spent < 1200 ){
            $text = __( 'congrats you are now tier 1', 'woocommerce' );
        }elseif( $total_spent > 1200 ){
            $text = __( 'congrats you are now tier 2', 'woocommerce' );
        }else{
            $text = __( 'congrats you are now tier 3', 'woocommerce' );
        }

        $total_spent = wc_price( $total_spent );

        return "Total Amount Spent: ".$total_spent." ".$text;
    }
}
add_shortcode('user_total_spent', 'get_user_total_spent');

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM