根据 WooCommerce 中的日期自定义字段应用客户生日折扣
Apply a customer birthday discount based on date custom field in WooCommerce
根据 对我上一个问题的回答,我正在尝试申请客户生日折扣。
这是我的代码:
add_action( 'woocommerce_cart_calculate_fees', 'give_birthday_discount', 10, 3 );
function give_birthday_discount( $cart, $user_id, $date = 'now' ) {
if ( is_user_logged_in() && $date == get_user_meta( $user_id, 'birthday_field', true ) ) {
$discount_percentage = 10;
$cart->add_fee( __( 'Birthday Discount', 'woocommerce' ), -( $cart->subtotal * $discount_percentage / 100 ));
}
}
但它给了我这个致命错误:
Uncaught ArgumentCountError: Too few arguments to function give_birthday_discount()
所以我需要一些帮助来申请客户生日折扣。
$user_id
和 $date
不是来自 woocommerce_cart_calculate_fees
操作挂钩的参数。
PHP 使用的函数
- date - 格式化本地 time/date
- d - 一个月中的两位数日期(带前导零)- 01 到 31
- m - 月份的两位数表示 - 01(一月)到 12(十二月)
所以你得到:
function action_woocommerce_cart_calculate_fees( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// NOT logged in
if ( ! is_user_logged_in() )
return;
// Get current user id
$user_id = get_current_user_id();
// Get value
$birthday = get_user_meta( $user_id, 'birthday_field', true );
// NOT empty
if ( ! empty( $birthday ) ) {
// Convert
$birthday = date( 'd.m', strtotime( $birthday ) );
// Today
$today = date( 'd.m' );
// Compare, equal to
if ( $birthday == $today ) {
// Discount percentage
$discount_percentage = 10;
// Discount
$cart->add_fee( __( 'Birthday Discount', 'woocommerce' ), - ( $cart->subtotal * $discount_percentage / 100 ), false );
}
}
}
add_action( 'woocommerce_cart_calculate_fees', 'action_woocommerce_cart_calculate_fees', 10, 1 );
根据
这是我的代码:
add_action( 'woocommerce_cart_calculate_fees', 'give_birthday_discount', 10, 3 );
function give_birthday_discount( $cart, $user_id, $date = 'now' ) {
if ( is_user_logged_in() && $date == get_user_meta( $user_id, 'birthday_field', true ) ) {
$discount_percentage = 10;
$cart->add_fee( __( 'Birthday Discount', 'woocommerce' ), -( $cart->subtotal * $discount_percentage / 100 ));
}
}
但它给了我这个致命错误:
Uncaught ArgumentCountError: Too few arguments to function give_birthday_discount()
所以我需要一些帮助来申请客户生日折扣。
$user_id
和 $date
不是来自 woocommerce_cart_calculate_fees
操作挂钩的参数。
PHP 使用的函数
- date - 格式化本地 time/date
- d - 一个月中的两位数日期(带前导零)- 01 到 31
- m - 月份的两位数表示 - 01(一月)到 12(十二月)
所以你得到:
function action_woocommerce_cart_calculate_fees( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// NOT logged in
if ( ! is_user_logged_in() )
return;
// Get current user id
$user_id = get_current_user_id();
// Get value
$birthday = get_user_meta( $user_id, 'birthday_field', true );
// NOT empty
if ( ! empty( $birthday ) ) {
// Convert
$birthday = date( 'd.m', strtotime( $birthday ) );
// Today
$today = date( 'd.m' );
// Compare, equal to
if ( $birthday == $today ) {
// Discount percentage
$discount_percentage = 10;
// Discount
$cart->add_fee( __( 'Birthday Discount', 'woocommerce' ), - ( $cart->subtotal * $discount_percentage / 100 ), false );
}
}
}
add_action( 'woocommerce_cart_calculate_fees', 'action_woocommerce_cart_calculate_fees', 10, 1 );