【问题标题】:Set a minimal order amount for a specific User role in Woocommerce在 Woocommerce 中为特定用户角色设置最小订单量
【发布时间】:2018-07-16 22:39:34
【问题描述】:

我正在创建一个批发网站。看这里,我已经能够找到在客户首次下订单后在两个用户角色之间切换的代码。现在我只需要能够在购物车/结帐页面上为不同的用户角色设置最低订单金额,而我发现的大部分内容都比我需要的要多得多。在网上搜索我能找到这段代码:

<?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' );

如何向此代码添加 1 个以上的用户角色?

【问题讨论】:

    标签: php woocommerce cart checkout user-roles


    【解决方案1】:

    您使用的代码非常陈旧且过时。下面的代码将检查应具有最小订单量的 Wholesale 用户角色的购物车项目:

    // 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 文件中。经过测试并且可以工作。


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

    // 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 文件中。经过测试并且可以工作。

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

    【讨论】:

    • 感谢洛伊克!除了批发之外,我还需要至少设置一个用户角色。如果我制作两个这样的插件会起作用吗?或者我将如何为另一个用户添加额外的最小 amt。抱歉,我不太擅长代码。
    • @Vash 我在答案中添加了一些其他代码来处理 2 个用户角色、2 个最小数量和 2 个不同的错误消息。
    猜你喜欢
    • 2018-12-24
    • 2019-07-20
    • 2020-09-01
    • 2021-07-09
    • 2021-07-22
    • 2020-12-02
    • 1970-01-01
    • 2020-12-14
    • 1970-01-01
    相关资源
    最近更新 更多