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

oyxsuwqo  于 2023-11-16  发布在  PHP
关注(0)|答案(1)|浏览(115)

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

字符串

zy1mlcev

zy1mlcev1#

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

if ( current_user_can('distributors') )

字符串
其中,您将根据user_id和状态wc-completed统计先前订单的数量,如果计数大于0,则更改最小值
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 );

相关问题