提问者:小点点

基于用户角色的Woocommerce最低订单总数


我正在使用来自WooCommerce最小订单金额的代码片段来设置最小订单总额。但是我想为每个用户角色设置不同的最小值。

我有一些自定义用户角色:wholesale_priceswholesale_vat_excdistributor_prices。我想让代码基于使用角色工作,每个角色的最低金额不同。

这是我的代码:

// Minimum order total

add_action( 'woocommerce_check_cart_items', 'wc_minimum_order_amount' );
 
function wc_minimum_order_amount() {
    // Set this variable to specify a minimum order value
    $minimum = 300;

    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个答案

匿名用户

使用Wordpress条件函数当前用户\u can()

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('distributor_prices') )
        $minimum = 3000; 
    elseif ( current_user_can('wholesale_prices') )
        $minimum = 1000;
    elseif ( current_user_can('wholesale_vat_exc') )
        $minimum = 600;
    else 
        $minimum = 300; // 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' );
        }
    }
}

代码在函数上运行。活动子主题(或活动主题)的php文件。它应该有效。