【问题标题】:Woocommerce minimum order total based on user roles基于用户角色的 Woocommerce 最低订单总数
【发布时间】:2020-10-18 06:24:35
【问题描述】:

我正在使用来自 Woocommerce Minimum Order Amount 的 sn-p 代码来设置最低订单总额。但我想为每个用户角色设置不同的最小值。

我有一些自定义用户角色: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' 
            );

        }
    }

【问题讨论】:

  • 如果您使用的是预先存在的代码,请在问题中提及来源 - docs.woocommerce.com/document/minimum-order-amount
  • 一年多以前我把那个代码粘贴到我的函数文件中,我真的不记得我从哪里得到它但下次会尽力搜索。

标签: php wordpress woocommerce cart hook-woocommerce


【解决方案1】:

使用Wordpress条件函数current_user_can()like:

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 文件中。它应该可以工作。

【讨论】:

  • 我是否需要创建另一个类似这样的函数:function wc_minimum_order_amount_distributors() { if ( ! current_user_can('distributors') ) return; // Set this variable to specify a minimum order value $minimum = 5000; 以便为经销商用户角色设置不同的最小值?
  • @HuseinYuseinov 更新了……所以你有不同的用户角色。
  • 谢谢,你是最棒的!
  • 是否可以为特定用户角色的第一个订单和第一个订单之后的订单设置不同的最小订单总数?
猜你喜欢
  • 2019-07-20
  • 2017-08-14
  • 2017-07-15
  • 1970-01-01
  • 2021-07-09
  • 2021-06-14
  • 2020-10-18
  • 2018-12-24
  • 2019-07-27
相关资源
最近更新 更多