按用户角色和订单历史记录在 WooCommerce 中设置最低订单金额

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

我正在使用 WooCommerce Minimum Order Amount 中的代码片段来设置用户角色的最低订单总数。

对于分销商的角色,我想更改最低订单总数,如果他们已经完成了之前的订单。

所以对于第一笔订单,最低金额为 3000,之后的订单最低金额为 1000。

这是我的代码:

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' );
        }
    }
}

要根据以前完成的订单更改分销商角色的最小订单总数,您可以扩展此条件:

 if ( current_user_can('distributors') )

这里会根据user_id和状态wc-completed统计之前的订单数量,如果数量大于0,则更改mimimun


wc_get_orders() 用于此,可以使用其他参数进一步扩展,例如某些订单状态等。

所以你得到:

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 );