基于 WooCommerce 中的购物车总数和用户角色的累进折扣
Progressive discount based on cart total and user roles in 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
用户角色可用?
正如您在 related answer, I use the WordPress conditional function current_user_can()
上看到的那样,针对特定的用户角色...
所以你需要在 答案代码中使用它,就像:
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;
我正在使用
我有一些自定义用户角色: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
用户角色可用?
正如您在 current_user_can()
上看到的那样,针对特定的用户角色...
所以你需要在
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;