Woocommerce按用户角色最小订单


Woocommerce Minimum Order by User Role

之前有人问了一个类似的问题,但是提供的解决方案似乎对我没有帮助。

我在一个批发网站工作。对于首次购买者,最低订购额为500美元。对于退货买家/重新订购,没有最低限额。我假设我需要做2个不同的批发用户角色-一个为第一次买家和另一个返回/重新订购的买家。我觉得批发新货和批发再订货可以。如何将其应用到functions。php中?这是我目前使用的代码:

add_action( 'woocommerce_checkout_process', 'wc_minimum_order_amount' );
add_action( 'woocommerce_before_cart' , 'wc_minimum_order_amount' );
function wc_minimum_order_amount() {
// Set this variable to specify a minimum order value
$minimum = 500;
if ( WC()->cart->total < $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->total )
            ), '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->total )
            ), 'error' 
        );
    }
}

您需要current_user_can()来测试当前用户做某事的能力。

创建角色

首先,您需要为没有任何限制的用户创建一个新角色。Members插件是创建角色和编辑角色功能的天才。

例如,您可以创建一个Returning Wholesaler角色。

定义新角色的能力

你应该复制"客户"角色的能力……所以read, edit_posts, delete_posts

然后,添加一个名为wholesale_reorder的新功能

修改条件逻辑

然后你可以将if语句转换为:

if ( WC()->cart->total < $minimum )

:

if ( WC()->cart->total < $minimum && ! current_user_can( 'wholesale_reorder' ) )

没有新角色的情况下的简化

如果你没有任何现有客户,不需要多个级别的批发,不需要与常规客户一起批发,你没有办法注册而不订购,那么表面上你可以跳过添加新角色,只测试current_user_can('customer'),因为唯一可能成为客户的人将是那些已经订购了一些东西的人。