基于用户角色的 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' 

            );


        }

    }


繁华开满天机
浏览 104回答 1
1回答

哈士奇WWW

使用 WordPress 条件函数,current_user_can()例如:add_action( 'woocommerce_check_cart_items', 'wc_minimum_order_amount' );function wc_minimum_order_amount() {&nbsp; &nbsp; // minimum order value by user role&nbsp; &nbsp; if ( current_user_can('distributor_prices') )&nbsp; &nbsp; &nbsp; &nbsp; $minimum = 3000;&nbsp;&nbsp; &nbsp; elseif ( current_user_can('wholesale_prices') )&nbsp; &nbsp; &nbsp; &nbsp; $minimum = 1000;&nbsp; &nbsp; elseif ( current_user_can('wholesale_vat_exc') )&nbsp; &nbsp; &nbsp; &nbsp; $minimum = 600;&nbsp; &nbsp; else&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; $minimum = 300; // default&nbsp; &nbsp; if ( WC()->cart->subtotal < $minimum ) {&nbsp; &nbsp; &nbsp; &nbsp; if( is_cart() ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; wc_print_notice( sprintf(&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'You must have an order with a minimum of %s to place your order, your current order total is %s.' ,&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; wc_price( $minimum ),&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; wc_price( WC()->cart->subtotal )&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ), 'error' );&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; wc_add_notice( sprintf(&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'You must have an order with a minimum of %s to place your order, your current order total is %s.' ,&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; wc_price( $minimum ),&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; wc_price( WC()->cart->subtotal )&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ), 'error' );&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}代码位于活动子主题(或活动主题)的functions.php 文件中。它应该有效。
打开App,查看更多内容
随时随地看视频慕课网APP