Woocommerce为特定用户角色设置最低订单 [英] Woocommerce set minimum order for a specific user role

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

问题描述

我有一个php代码,它设置了整个站点范围内的100个最低订购量,效果很好。
我想使用相同的代码或通过将其克隆并将其包装在if()语句中,为特定角色公司设置不同的最低订购量250,只有我不知道如何编写。不胜感激。

这是当前的代码(不要满足条件的希伯来语文本,只是错误消息):

I have a php code that sets a 100 minimum order site wide, that works good. I want to set a different minimum order of 250 for a specific role 'company' using the same code or by cloning it and wrapping it in an if() statement, only I have no idea how to write it. Would appreciate any kind of help.
This it the currant code (don't mind the Hebrew text, its just the error messages, when the condition is not met):

// Set a minimum dollar amount per order
add_action( 'woocommerce_check_cart_items', 'spyr_set_min_total' );
function spyr_set_min_total() {
// Only run in the Cart or Checkout pages
if( is_cart() || is_checkout() ) {
    global $woocommerce;

    // Set minimum cart total
    $minimum_cart_total = 100;

    // Total we are going to be using for the Math
    // This is before taxes and shipping charges
    $total = WC()->cart->subtotal;

    // Compare values and add an error is Cart's total
    // happens to be less than the minimum required before checking out.
    // Will display a message along the lines of
    // A Minimum of 10 USD is required before checking out. (Cont. below)
    // Current cart total: 6 USD 
    if( $total <= $minimum_cart_total  ) {
        // Display our error message
        wc_add_notice( sprintf( '<strong>לקוח יקר, יש צורך במינימום הזמנה 
של %s %s₪ על מנת לבצע רכישה באתר.</strong>'
            .'<br />סכום הביניים בעגלה הינו: %s %s₪',
            $minimum_cart_total,
            get_option( 'woocommerce_currency_symbol'),
            $total,
            get_option( 'woocommerce_currency_symbol') ),
        'error' );
    }
  }
}

谢谢!

推荐答案

使用 current_user_can()尝试以下操作,因此在代码中:

Try the following using current_user_can(), so in your code:

// Set a minimum amount per order (and user role)
add_action( 'woocommerce_check_cart_items', 'set_min_total_per_user_role' );
function set_min_total_per_user_role() {
    // Only run in the Cart or Checkout pages
    if( is_cart() || is_checkout() ) {

        // Set minimum cart total
        $minimum_cart_total = current_user_can('company') ? 250 : 100;

        // Total (before taxes and shipping charges)
        $total = WC()->cart->subtotal;

        // Add an error notice is cart total is less than the minimum required
        if( $total <= $minimum_cart_total  ) {
            // Display our error message
            wc_add_notice( sprintf( '<strong>Dear customer, minimum order of %s is required to make a purchase on your site.</strong> <br>
                Your actual cart amount is: %s',
                wc_price($minimum_cart_total),
                wc_price($total)
            ), 'error' );
        }
    }
}

在function.php上执行代码活动子主题(或活动主题)的文件。

Code goes on function.php file of your active child theme (or active theme). It should works.


要格式化显示价格,可以使用专用的 wc_price()格式化功能。

相关:为Woocommerce中的特定用户角色应用折扣

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

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