在网上store 中,我想针对特定群体的顾客推出%的折扣.
我已经创建了一个新群组和客户,并添加了下面的代码,这将为购物车提供折扣.
/* discount biblioteka */
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 'biblioteka' role
if ( ! current_user_can('biblioteka') )
return; // Exit
// Kortings percentage
$percentage = 40;
$discount = $cart->get_subtotal() * $percentage / 100; // Calculation
// Applying discount
$cart->add_fee( sprintf( __("biblioteka discount (%s)", "woocommerce"), $percentage . '%'), - $discount, true );
}
我希望这个折扣也作为促销价格显示在store 中,我在下面添加了代码,这使得网站停止工作.
/* Custom prices by user role */
add_filter('woocommerce_product_get_price', 'custom_price_assign', 10, 2);
add_filter('woocommerce_product_variation_get_price', 'custom_price_assign', 10, 2); // For product variations (optional)
function custom_price_assign( $price, $product ) {
// Check if the user has a role of wholesaler
if ( current_user_can('biblioteka') ){
return $price * 0.40;
}
return $price;
}
对于特殊用户群体,我最简单的方式是什么?将正价和40%的价格显示为促销价?