简体   繁体   中英

Set minimum order amount in WooCommerce by user role and order history

I'm using a snippet code from WooCommerce Minimum Order Amount to set a minimum order total by user role.

For the distributors role, I want to change the minimum order total if they have previous order completed.

So for the first order, the minimum would be 3000, for the orders afterward the minimum would become 1000.

Here is my code:

add_action( 'woocommerce_check_cart_items', 'wc_minimum_order_amount' );
function wc_minimum_order_amount() {
    // minimum order value by user role
    if ( current_user_can('distributors') )
        $minimum = 3000; 
    elseif ( current_user_can('wholesale_customer') )
        $minimum = 200;
    elseif ( current_user_can('wholesale_vat_exc') )
        $minimum = 200;
    else 
        $minimum = 200; // default

    if ( WC()->cart->subtotal < $minimum ) {

        if( is_cart() ) {
            wc_print_notice( sprintf( 
                'You must have an order with a minimum of %s to place your order, your current order total is %s.' , 
                wc_price( $minimum ), 
                wc_price( WC()->cart->subtotal )
            ), 'error' );
        } else {
            wc_add_notice( sprintf( 
                'You must have an order with a minimum of %s to place your order, your current order total is %s.' , 
                wc_price( $minimum ), 
                wc_price( WC()->cart->subtotal )
            ), 'error' );
        }
    }
}

To change the minimum order total for the distributors role based on previous completed orders, you can extend this if condition:

 if ( current_user_can('distributors') )

Where you will count the number of previous orders based on the user_id and status wc-completed , if the count is greater than 0, change the mimimun


wc_get_orders() is used for this, this can be further expanded with additional parameters such as certain order statuses, etc.

So you get:

function action_woocommerce_check_cart_items() {
    // minimum order value by user role
    if ( current_user_can( 'distributors' ) ) {         
        // Get completed orders by customer id
        $completed_orders_by_customer_id = wc_get_orders( array(
            'customer_id' => get_current_user_id(),
            'status' => array( 'wc-completed' ),
        ));

        // Number of previous completed orders is greater than 0
        if ( sizeof( $completed_orders_by_customer_id ) > 0 ) {
            $minimum = 1000;
        } else {
            $minimum = 3000; 
        }
    } elseif ( current_user_can( 'wholesale_customer' ) ) {
        $minimum = 200;
    } elseif ( current_user_can( 'wholesale_vat_exc' ) ) {
        $minimum = 200;
    } else { 
        $minimum = 200; // default
    }

    if ( WC()->cart->subtotal < $minimum ) {

        if ( is_cart() ) {
            wc_print_notice( sprintf( 
                'You must have an order with a minimum of %s to place your order, your current order total is %s.' , 
                wc_price( $minimum ), 
                wc_price( WC()->cart->subtotal )
            ), 'error' );
        } else {
            wc_add_notice( sprintf( 
                'You must have an order with a minimum of %s to place your order, your current order total is %s.' , 
                wc_price( $minimum ), 
                wc_price( WC()->cart->subtotal )
            ), 'error' );
        }
    }
}
add_action( 'woocommerce_check_cart_items' , 'action_woocommerce_check_cart_items', 10, 0 );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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