【问题标题】:Woocommerce set minimum order for a specific user roleWoocommerce 为特定用户角色设置最低订单
【发布时间】:2019-07-20 10:01:32
【问题描述】:

我有一个 php 代码,它在网站范围内设置了 100 个最小订单,效果很好。 我想使用相同的代码或通过克隆它并将其包装在 if() 语句中为特定角色“公司”设置不同的最小订单 250,只是我不知道如何编写它。将不胜感激任何形式的帮助。
这是醋栗代码(不要介意希伯来语文本,它只是错误消息,当条件不满足时):

// 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' );
    }
  }
}

谢谢!

【问题讨论】:

    标签: php wordpress woocommerce cart user-roles


    【解决方案1】:

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

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

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

    相关:Apply a discount for a specific user role in Woocommerce

    【讨论】:

    • 谢谢你!我在线收到错误:wc_price($minimum_cart_total),
    • @fafchook 这是因为希伯来字符...我已经用英文代替了,它可以工作...
    • 好吧,这很奇怪。因为它在前面的代码中运行良好。不知道有没有翻译的方法?也许通过 PO 文件?
    • 知道了。由于某种原因,复制带有希伯来字符的代码会产生错误,但在粘贴后修改它不会。再次感谢我的朋友,你是一个救生员!
    • @fafchook 只需在希伯来语中输入所需的字符串并在您需要格式化的“最低购物车价格”和格式化的“总价”的地方添加 %s。然后你可以在其中添加html标签&lt;strong&gt;、&lt;strong&gt;和&lt;br&gt;…我自己做不到,因为我不懂希伯来语,因为它是一种RTL语言。
    【解决方案2】:

    首先您必须检索当前用户的角色。

    $roles = is_user_logged_in() ? (array) wp_get_current_user()->roles : [];
    

    现在您要检查角色company 是否在用户的角色中以确定最小值。

    $minimum_cart_total = in_array('company', $roles) ? 250 : 100;
    

    【讨论】:

      猜你喜欢
      • 2018-12-24
      • 1970-01-01
      • 2021-07-09
      • 2020-10-18
      • 2021-05-08
      • 2020-09-01
      • 2021-07-22
      • 1970-01-01
      • 2015-08-16
      相关资源
      最近更新 更多