首页 > 解决方案 > 基于购物车总数和 WooCommerce 中用户角色的累进折扣

问题描述

在 WooCommerce 答案代码中使用基于购物车总数的累进折扣来制作一些 Woocommerce 订单总折扣(见下文)。但我想根据用户角色打折,因为我的每个客户角色看到不同的价格。

我有一些自定义用户角色:wholesale_priceswholesale_vat_excdistributor_prices.

我想让代码只为wholesale_priceswholesale_vat_exc用户角色工作,而不是为distributor_prices (因为他们不能看到折扣)

这是我实际重新访问的代码版本:

// Order total discount

add_action( 'woocommerce_cart_calculate_fees', 'progressive_discount_based_on_cart_total', 10, 1 );
function progressive_discount_based_on_cart_total( $cart_object ) {

    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    $cart_total = $cart_object->cart_contents_total; // Cart total

    if ( $cart_total >= 3000.00 && $cart_total < 5000.00 )
        $percent = 15; // 15%
    elseif ( $cart_total >= 1500.00 && $cart_total < 3000.00 )
        $percent = 10; // 10%
    elseif ( $cart_total >= 750.00 && $cart_total < 1500.00 )
        $percent =  5; // 5%
    else
        $percent = 0;

    if ( $percent != 0 ) {
        $discount =  $cart_total * $percent / 100;
        $cart_object->add_fee( "Bulk Order Discount ($percent%)", -$discount, true );
    }
}

如何使此代码仅适用于wholesale_prices用户wholesale_vat_exc角色?

标签: phpwordpresswoocommercecartdiscount

解决方案


正如您在为 Woocommerce 相关答案中的特定用户角色应用折扣中看到的那样,我使用 WordPress 条件函数current_user_can()来定位特定用户角色......</p>

因此,您需要根据 WooCommerce 答案代码中的购物车总数在渐进式折扣中使用它,就像:

add_action( 'woocommerce_cart_calculate_fees', 'progressive_discount_based_on_cart_total', 10, 1 );
function progressive_discount_based_on_cart_total( $cart ) {
    // HERE we target other user roles than 'distributor_prices' (allowing guests)
    if ( current_user_can('distributor_prices') && is_user_logged_in() )
        return;

    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    $cart_total = $cart->get_cart_contents_total(); // Cart total

    if ( $cart_total >= 3000.00 && $cart_total < 5000.00 )
        $percent = 15; // 15%
    elseif ( $cart_total >= 1500.00 && $cart_total < 3000.00 )
        $percent = 10; // 10%
    elseif ( $cart_total >= 750.00 && $cart_total < 1500.00 )
        $percent =  5; // 5%
    else
        $percent = 0;

    if ( $percent > 0 ) {
        $discount   = $cart_total * $percent / 100;
        $label_text = sprintf( __("Bulk Order Discount %s"), '('.$percent.'%)' );
        $cart->add_fee( $label_text, -$discount, true );
    }
}

代码在您的活动子主题(或活动主题)的functions.php 文件中。它应该有效。


对于多个用户角色,您将用于wp_get_current_user()获取当前WP_User对象,然后您可以获得如下roles属性:

$user       = wp_get_current_user();
$user_roles = $user->roles;  // An array of the user roles

然后你将在代码中替换:

    // HERE we target other user roles than 'distributor_prices' (allowing guests)
    if ( current_user_can('distributor_prices') && is_user_logged_in() )
        return;

经过:

    // HERE we target "wholesale_prices" and "wholesale_vat_exc" user roles (allowing guests)
    if ( ! array_intersect( wp_get_current_user()->roles, array('wholesale_prices', 'wholesale_vat_exc') && is_user_logged_in() )
        return;

或者

    // HERE we target "wholesale_prices" and "wholesale_vat_exc" user roles (allowing guests)
    if ( ! ( current_user_can('wholesale_prices') || current_user_can('wholesale_vat_exc') ) && is_user_logged_in() )
        return;

推荐阅读