繁体   English   中英

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

[英]Woocommerce minimum order total based on user roles

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

        }
    }

使用 Wordpress 有条件的 function current_user_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' );
        }
    }
}

代码继续 functions.php 活动子主题(或活动主题)的文件。 它应该有效。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM