根据用户角色和订单历史在 WooCommerce 中设置最低订单金额

时间:2021-04-14 10:54:27

标签: php wordpress woocommerce cart orders

我正在使用 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' );
        }
    }
}

1 个答案:

答案 0 :(得分:1)

要根据之前完成的订单更改经销商角色的最低订单总额,您可以扩展此条件:

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