为Woocommerce中的特定用户角色设置最小订购量 [英] Set a minimal order amount for a specific User role in Woocommerce

查看:103
本文介绍了为Woocommerce中的特定用户角色设置最小订购量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个批发网站。看这里,我已经能够找到在客户下第一笔订单后在两个用户角色之间切换的代码。现在,我只需要能够在购物车/结帐页面上为不同的用户角色设置最低订购量,而我所发现的大多数功能远远超出了我的需要。在网上搜索时,我能够找到以下代码:

I'm creating a wholesale website. Looking on here I was able to already find code for switching between two user roles after customers place a first order. Now I just need to be able to set minimum order amounts for different user roles on the cart/checkout page and most of what I've been finding does way more than I need it to. Searching online I was able to find this bit of code:

<?php
/*
Author: Doug Edgington
Description: Require a minimum order total in Woocommerce for a user role
*/
function dee_minimum_order_total_required() {
    if( is_user_logged_in() ) { 
        global $woocommerce;
        global $current_user;

        get_currentuserinfo();

        $dee_user_roles = $current_user->roles;
        $dee_user_role = array_shift($dee_user_roles);

        $dee_minimum = 50;
        if ( $woocommerce->cart->subtotal < $dee_minimum  && $dee_user_role == 'wholesale_buyer') {
            $woocommerce->add_error( sprintf( 'Wholesale users must have a minimum order total of $%s to place an order.' , $dee_minimum ) );
        }
    } //end main if statement
}
add_action( 'woocommerce_checkout_process', 'dee_minimum_order_total_required' );

如何在此代码中添加多个用户角色?

How would I add more than 1 user role to this code?

推荐答案

您使用的代码确实过时且过时。以下代码将检查批发用户角色的购物车项目,该购物车项目应具有最小订单金额:

The code you are using is really old and obsolete. The code below will check cart items for Wholesale user role that should have a minimal order amount:

// Cart and checkout validation
add_action( 'woocommerce_check_cart_items', 'minimal_total_required' ); // Cart and Checkout
add_action( 'woocommerce_checkout_process', 'minimal_total_required' ); // Checkout (optional)
function minimal_total_required() {
    $user = wp_get_current_user();

    ## -- YOUR SETTINGS BELOW -- ##

    $min_amount    = 50; // Minimal order amount
    $targeted_role = 'wholesale_buyer'; // User role

    // Exit for non logged users or when minimal order amout is reached
    if( $user->ID == 0 || WC()->cart->subtotal >= $min_amount ) 
        return; 

    // Display an error notice for Wholesale user role
    if ( in_array( $targeted_role, $user->roles ) ) 
        wc_add_notice( sprintf( __("As a Wholesale user you must have a minimal order total of %s.") , wc_price($min_amount) ), 'error' );
}

代码进入活动子主题(或活动主题)的function.php文件)。经过测试并有效。

Code goes in function.php file of your active child theme (or active theme). Tested and works.

对于两个用户角色和两个最小金额,您将使用以下内容:

For Two user roles and two minimal amounts you will use the following:

// Cart and checkout validation
add_action( 'woocommerce_check_cart_items', 'minimal_total_required' ); // Cart and Checkout
add_action( 'woocommerce_checkout_process', 'minimal_total_required' ); // Checkout (optional)
function minimal_total_required() {
    $user = wp_get_current_user();

    // Exit for non logged users
    if( $user->ID == 0 ) return;

    ## -- YOUR SETTINGS BELOW (For 2 user roles and 2 minimal amounts) -- ##

    $min_amount    = array( 50, 40 ); // Minimal order amounts
    $targeted_role = array('wholesale_buyer', 'customer'); // Targetted User roles

    $cart_subtotal  = WC()->cart->subtotal;

    // Wholesale user
    if ( in_array( $targeted_role[0], $user->roles ) && $cart_subtotal < $min_amount[0]){
        $text = sprintf( __('As a Wholesale user you must have a minimal order total amount of %s.'), wc_price($min_amount[0]) );
    }
    // Customer user
    elseif ( in_array( $targeted_role[1], $user->roles ) && $cart_subtotal < $min_amount[1]){
        $text = sprintf( __('You must have a minimal order total amount of %s.'), wc_price($min_amount[1]) );
    }

    // Display an error notice for Wholesale user role
    if( isset($text) )
        wc_add_notice( $text, 'error' );
}

代码进入活动子主题(或活动主题)的function.php文件)。经过测试并有效。

Code goes in function.php file of your active child theme (or active theme). Tested and works.


它将为每个用户角色显示不同的通知。

It will display a different notice for each user role.

这篇关于为Woocommerce中的特定用户角色设置最小订购量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆