php - 基于购物车总数和 WooCommerce 中用户角色的累进折扣
问题描述
我在 WooCommerce 答案代码中使用基于购物车总数的累进折扣来制作一些 Woocommerce 订单总折扣(见下文)。但我想根据用户角色打折,因为我的每个客户角色看到不同的价格。
我有一些自定义用户角色:wholesale_prices
、wholesale_vat_exc
和distributor_prices
.
我想让代码只为wholesale_prices
和wholesale_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
角色?
解决方案
正如您在为 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;
推荐阅读
- python - 如果函数抛出异常则不返回是不好的做法吗?
- r - gghighlight 不会在 ggplot2 图中显示灰线?
- excel - MAcro:选择要修改的文件,修改每张表,导出xlsx和pdf
- regex - 如何使用正则表达式(RegEx)在某些模式之间提取文本?
- swift - 如何将 NSSet 的内容附加到 [NSManagedObject]?
- r - 如何对齐 R 或 Excel 中不同列的日期
- php - 根据嵌套数组中的值从数组中删除重复值
- android - 是否可以撤销使用 Espresso 的许可?
- excel - VBA中基于父子的范围排序
- php - 用另一个数组递归替换具有特定键的一个数组