Woocommerce为特定用户角色设置最低订单

时间:2019-02-26 19:21:07

标签: php wordpress woocommerce cart user-roles

我有一个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' );
    }
  }
}

谢谢!

2 个答案:

答案 0 :(得分:1)

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

   <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>

现在,您要检查角色$roles = is_user_logged_in() ? (array) wp_get_current_user()->roles : []; 是否在用户角色中以确定最小数量。

company

答案 1 :(得分: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

相关问题