douzhang5199 2015-07-07 11:45
浏览 45
已采纳

(WooCommerce)检查客户是否订购了超过一定数量的订单或花了一定数量?

Is there a way I can set a conditional statement to show a message to customers that have already made a certain number of orders or spent more than a certain amount of money on my store?

I basically want to offer a site-wide discount for valued customers, which I have already set up a system for, but ideally I'd like to show a form where users can apply for this discount, but to prevent everyone from applying I'd like to only show it to repeat customers.

  • 写回答

1条回答 默认 最新

  • duanqin4238 2015-07-07 18:49
    关注

    Turns out that we can steal... er borrow... a query for orders right out of the WooCommerce source. We just need to modify the posts_per_page parameter to -1 so that it will query all posts.

    This has the potential to chew up resources... so 1. I suggest only running it for logged in users. 2. Maybe only retrieve the number of posts you want to limit your coupon to (aka... 5 orders gets you a discount, only retrieve 5 posts) and 3. possibly even cache the value either in a transient or maybe even keep track of it as user meta. That last one is a bit trickier than I want to get into now so

    if ( is_user_logged_in() ) {
    
        $number_of_orders = 10;
    
        $customer_orders = get_posts( apply_filters( 'woocommerce_my_account_my_orders_query', array(
            'numberposts' => $number_of_orders,
            'meta_key'    => '_customer_user',
            'meta_value'  => get_current_user_id(),
            'post_type'   => wc_get_order_types( 'view-orders' ),
            'post_status' => array_keys( wc_get_order_statuses() )
        ) ) );
    
        if ( $customer_orders && count( $customer_orders > $number_of_orders ) ){
            // whoa you've got more than 10 orders
        }
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?