如果在 Woocommerce 中没有应用优惠券,则自动将折扣应用于用户角色
Automatically apply discount to user role if no coupon is applied in Woocommerce
我在下面有这段代码,但我希望它仅在用户未使用其他代码时才有效,并且如果用户使用其他优惠券,则禁用此优惠券。
根据"" 答案码,一直在想怎么查有没有coupon,一直想不通。
// Applying conditionally a discount for a specific user role
add_action( 'woocommerce_cart_calculate_fees', 'discount_based_on_user_role', 20, 1 );
function discount_based_on_user_role( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return; // Exit
// Only for 'company' user role
if ( ! current_user_can('affiliate') )
return; // Exit
// Only for 'company' user role
if ( ! current_user_can('affiliate') )
return; // Exit
// HERE define the percentage discount
$percentage = 15;
$discount = $cart->get_subtotal() * $percentage / 100;
// Applying discount
$cart->add_fee( sprintf( __("Affiliate Discount (%s)", "woocommerce"), $percentage . '%'), -$discount, true );
}
感谢任何帮助。
如果购物车中没有已应用的优惠券,请尝试以下将根据特定用户角色设置自定义折扣的方法:
// Applying conditionally a discount for a specific user role and no applied coupons
add_action( 'woocommerce_cart_calculate_fees', 'discount_based_on_user_role', 20, 1 );
function discount_based_on_user_role( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return; // Exit
// Only for 'affiliate' user role
if ( ! current_user_can('affiliate') )
return; // Exit
// Only if there is no applied coupons in cart
if ( ! empty( $cart->get_applied_coupons() ) )
return; // Exit
// HERE define the percentage discount
$percentage = 15;
$discount = $cart->get_subtotal() * $percentage / 100; // Calculation
// Applying discount
$cart->add_fee( sprintf( __("Affiliate Discount (%s)", "woocommerce"), $percentage . '%'), -$discount, true );
}
代码继续在您的活动子主题(或活动主题)的 functions.php
文件中。已测试并有效。
我在下面有这段代码,但我希望它仅在用户未使用其他代码时才有效,并且如果用户使用其他优惠券,则禁用此优惠券。
根据"
// Applying conditionally a discount for a specific user role
add_action( 'woocommerce_cart_calculate_fees', 'discount_based_on_user_role', 20, 1 );
function discount_based_on_user_role( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return; // Exit
// Only for 'company' user role
if ( ! current_user_can('affiliate') )
return; // Exit
// Only for 'company' user role
if ( ! current_user_can('affiliate') )
return; // Exit
// HERE define the percentage discount
$percentage = 15;
$discount = $cart->get_subtotal() * $percentage / 100;
// Applying discount
$cart->add_fee( sprintf( __("Affiliate Discount (%s)", "woocommerce"), $percentage . '%'), -$discount, true );
}
感谢任何帮助。
如果购物车中没有已应用的优惠券,请尝试以下将根据特定用户角色设置自定义折扣的方法:
// Applying conditionally a discount for a specific user role and no applied coupons
add_action( 'woocommerce_cart_calculate_fees', 'discount_based_on_user_role', 20, 1 );
function discount_based_on_user_role( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return; // Exit
// Only for 'affiliate' user role
if ( ! current_user_can('affiliate') )
return; // Exit
// Only if there is no applied coupons in cart
if ( ! empty( $cart->get_applied_coupons() ) )
return; // Exit
// HERE define the percentage discount
$percentage = 15;
$discount = $cart->get_subtotal() * $percentage / 100; // Calculation
// Applying discount
$cart->add_fee( sprintf( __("Affiliate Discount (%s)", "woocommerce"), $percentage . '%'), -$discount, true );
}
代码继续在您的活动子主题(或活动主题)的 functions.php
文件中。已测试并有效。