根据用户角色向未登录用户显示 WooCommerce 折扣的可能折扣
Show possible discount to unlogged users for WooCommerce discount based on user roles
我使用 解决方案创建了两个片段,它们将在这个黑色星期五为我的客户和批发商提供折扣。
但是,客户的折扣仅在他们登录时显示。有没有办法在客户登录前显示客户折扣?
// Apply Discount for Customer
add_action( 'woocommerce_cart_calculate_fees', 'discount_based_on_customer', 20, 1 );
function discount_based_on_customer( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
if ( ! current_user_can('customer') )
return;
$percentage = 50;
$discount = $cart->get_subtotal() * $percentage / 100; // Calculation
$cart->add_fee( sprintf( __("Black Friday Discount (%s)", "woocommerce"), $percentage . '%'), -$discount, true );
}
// Apply Discount for Wholesaler
add_action( 'woocommerce_cart_calculate_fees', 'discount_based_on_wholesaler', 10, 1 );
function discount_based_on_wholesaler( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
if ( ! ( current_user_can('default_wholesaler') || current_user_can('wholesaler-non-vat-registered') ) )
return;
$percentage = 10;
$discount = $cart->get_subtotal() * $percentage / 100; // Calculation
$cart->add_fee( sprintf( __("Black Friday Reseller Discount (%s)", "woocommerce"), $percentage . '%'), -$discount, true );
}
首先,您可以将它们合并到一个独特的挂钩函数中,以处理所有必需的用户角色,而不是在同一个挂钩上使用多个片段。
然后对于未登录的用户,您可以在购物车和结帐页面上打印一条通知,显示用户登录后可以获得的折扣金额和百分比(在右侧还显示登录 link通知的一侧)
完整重温代码:
// User roles discount percentage
add_action( 'woocommerce_cart_calculate_fees', 'discount_percentage_based_on_user_roles' );
function discount_percentage_based_on_user_roles( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// Logged in users only
if ( is_user_logged_in() ) {
$text_domain = 'woocommerce'; // Text domain for translations
$percentage = 0; // Initializing
$user_obj = wp_get_current_user(); // Get the WP_User Object
// 1. Customer user role
if ( in_array('customer', $user_obj->roles ) ) {
$percentage = 50;
$label_text = sprintf( __("Black Friday Discount (%s)", $text_domain), $percentage.'%');
}
// 2. Wholesalers users roles
elseif ( array_intersect( array('default_wholesaler', 'wholesaler-non-vat-registered'), $user_obj->roles ) ) {
$percentage = 10;
$label_text = sprintf( __("Black Friday Reseller Discount (%s)", $text_domain), $percentage.'%');
}
// THE DISCOUNT
if ( $percentage > 0 ) {
$discount = $cart->get_subtotal() * $percentage / 100; // Calculation
$cart->add_fee( $label_text, -$discount, true ); // Apply calculated discount
}
}
}
// For unlogged Users: display a notice with the discount percentage and the loging link (cart and checkout pages)
add_action( 'woocommerce_before_checkout_form', 'discount_percentage_notice_for_unlogged_users' );
add_action( 'woocommerce_before_cart_table', 'discount_percentage_notice_for_unlogged_users' );
function discount_percentage_notice_for_unlogged_users() {
if ( ! is_user_logged_in() ) {
$text_domain = 'woocommerce'; // Text domain for translations
$percentage = 50;
$login_link = get_permalink( wc_get_page_id( 'myaccount' ) );
$login_text = __("Login / Register", $text_domain);
wc_print_notice( sprintf(
__('You could get %s off on Black Friday Discount (%s).%s', $text_domain),
'<strong>' . wc_price(WC()->cart->subtotal * $percentage / 100) . '</strong>',
$percentage . '%',
'<a href="' . $login_link . '" class="button" style="float:right;">' . $login_text . '</a>'
), 'notice' );
}
}
代码进入活动子主题(或活动主题)的 functions.php 文件。已测试并有效。
或者,如果您想将“客户”用户角色的折扣应用于来宾用户 (未登录),您将改为使用:
// User roles and guests discount percentage
add_action( 'woocommerce_cart_calculate_fees', 'discount_percentage_based_on_user_roles' );
function discount_percentage_based_on_user_roles( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
$text_domain = 'woocommerce'; // Text domain for translations
$percentage = 0; // Initializing
$user_obj = wp_get_current_user(); // Get the WP_User Object
// 1. Wholesalers user roles
if ( array_intersect( array('default_wholesaler', 'wholesaler-non-vat-registered'), $user_obj->roles ) ) {
$percentage = 10;
$label_text = sprintf( __("Black Friday Reseller Discount (%s)", $text_domain), $percentage.'%');
}
// 2. Customer user role and guest users
elseif ( in_array('customer', $user_obj->roles ) || ! is_user_logged_in() ) {
$percentage = 50;
$label_text = sprintf( __("Black Friday Discount (%s)", $text_domain), $percentage.'%');
}
// THE DISCOUNT
if ( $percentage > 0 ) {
$discount = $cart->get_subtotal() * $percentage / 100; // Calculation
$cart->add_fee( $label_text, -$discount, true ); // Apply calculated discount
}
}
代码进入活动子主题(或活动主题)的 functions.php 文件。已测试并有效。
我使用
但是,客户的折扣仅在他们登录时显示。有没有办法在客户登录前显示客户折扣?
// Apply Discount for Customer
add_action( 'woocommerce_cart_calculate_fees', 'discount_based_on_customer', 20, 1 );
function discount_based_on_customer( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
if ( ! current_user_can('customer') )
return;
$percentage = 50;
$discount = $cart->get_subtotal() * $percentage / 100; // Calculation
$cart->add_fee( sprintf( __("Black Friday Discount (%s)", "woocommerce"), $percentage . '%'), -$discount, true );
}
// Apply Discount for Wholesaler
add_action( 'woocommerce_cart_calculate_fees', 'discount_based_on_wholesaler', 10, 1 );
function discount_based_on_wholesaler( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
if ( ! ( current_user_can('default_wholesaler') || current_user_can('wholesaler-non-vat-registered') ) )
return;
$percentage = 10;
$discount = $cart->get_subtotal() * $percentage / 100; // Calculation
$cart->add_fee( sprintf( __("Black Friday Reseller Discount (%s)", "woocommerce"), $percentage . '%'), -$discount, true );
}
首先,您可以将它们合并到一个独特的挂钩函数中,以处理所有必需的用户角色,而不是在同一个挂钩上使用多个片段。
然后对于未登录的用户,您可以在购物车和结帐页面上打印一条通知,显示用户登录后可以获得的折扣金额和百分比(在右侧还显示登录 link通知的一侧)
完整重温代码:
// User roles discount percentage
add_action( 'woocommerce_cart_calculate_fees', 'discount_percentage_based_on_user_roles' );
function discount_percentage_based_on_user_roles( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// Logged in users only
if ( is_user_logged_in() ) {
$text_domain = 'woocommerce'; // Text domain for translations
$percentage = 0; // Initializing
$user_obj = wp_get_current_user(); // Get the WP_User Object
// 1. Customer user role
if ( in_array('customer', $user_obj->roles ) ) {
$percentage = 50;
$label_text = sprintf( __("Black Friday Discount (%s)", $text_domain), $percentage.'%');
}
// 2. Wholesalers users roles
elseif ( array_intersect( array('default_wholesaler', 'wholesaler-non-vat-registered'), $user_obj->roles ) ) {
$percentage = 10;
$label_text = sprintf( __("Black Friday Reseller Discount (%s)", $text_domain), $percentage.'%');
}
// THE DISCOUNT
if ( $percentage > 0 ) {
$discount = $cart->get_subtotal() * $percentage / 100; // Calculation
$cart->add_fee( $label_text, -$discount, true ); // Apply calculated discount
}
}
}
// For unlogged Users: display a notice with the discount percentage and the loging link (cart and checkout pages)
add_action( 'woocommerce_before_checkout_form', 'discount_percentage_notice_for_unlogged_users' );
add_action( 'woocommerce_before_cart_table', 'discount_percentage_notice_for_unlogged_users' );
function discount_percentage_notice_for_unlogged_users() {
if ( ! is_user_logged_in() ) {
$text_domain = 'woocommerce'; // Text domain for translations
$percentage = 50;
$login_link = get_permalink( wc_get_page_id( 'myaccount' ) );
$login_text = __("Login / Register", $text_domain);
wc_print_notice( sprintf(
__('You could get %s off on Black Friday Discount (%s).%s', $text_domain),
'<strong>' . wc_price(WC()->cart->subtotal * $percentage / 100) . '</strong>',
$percentage . '%',
'<a href="' . $login_link . '" class="button" style="float:right;">' . $login_text . '</a>'
), 'notice' );
}
}
代码进入活动子主题(或活动主题)的 functions.php 文件。已测试并有效。
或者,如果您想将“客户”用户角色的折扣应用于来宾用户 (未登录),您将改为使用:
// User roles and guests discount percentage
add_action( 'woocommerce_cart_calculate_fees', 'discount_percentage_based_on_user_roles' );
function discount_percentage_based_on_user_roles( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
$text_domain = 'woocommerce'; // Text domain for translations
$percentage = 0; // Initializing
$user_obj = wp_get_current_user(); // Get the WP_User Object
// 1. Wholesalers user roles
if ( array_intersect( array('default_wholesaler', 'wholesaler-non-vat-registered'), $user_obj->roles ) ) {
$percentage = 10;
$label_text = sprintf( __("Black Friday Reseller Discount (%s)", $text_domain), $percentage.'%');
}
// 2. Customer user role and guest users
elseif ( in_array('customer', $user_obj->roles ) || ! is_user_logged_in() ) {
$percentage = 50;
$label_text = sprintf( __("Black Friday Discount (%s)", $text_domain), $percentage.'%');
}
// THE DISCOUNT
if ( $percentage > 0 ) {
$discount = $cart->get_subtotal() * $percentage / 100; // Calculation
$cart->add_fee( $label_text, -$discount, true ); // Apply calculated discount
}
}
代码进入活动子主题(或活动主题)的 functions.php 文件。已测试并有效。