根据 WooCommerce 中的日期自定义字段应用客户生日折扣

问题描述

根据 Only allow edit of custom field once per customer on My account > edit account 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 ));
    }
}

但它给了我这个致命的错误:

未捕获的 ArgumentCountError:函数 give_birthday_discount() 的参数太少

所以我需要一些帮助来为客户的生日申请折扣。

解决方法

HTML: <div> <span class="flex items-center justify-center p-2"> <svg class="mr-2 fill-current" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="24" height="24"> <path fill="none" d="M0 0H24V24H0z"/><path d="M12 13c1.657 0 3 1.343 3 3 0 .85-.353 1.616-.92 2.162L12.17 20H15v2H9v-1.724l3.693-3.555c.19-.183.307-.438.307-.721 0-.552-.448-1-1-1s-1 .448-1 1H9c0-1.657 1.343-3 3-3zm6 0v4h2v-4h2v9h-2v-3h-4v-6h2zM4 12c0 2.527 1.171 4.78 3 6.246v2.416C4.011 18.933 2 15.702 2 12h2zm8-10c5.185 0 9.449 3.947 9.95 9h-2.012C19.446 7.054 16.08 4 12 4 9.25 4 6.824 5.387 5.385 7.5H8v2H2v-6h2V6c1.824-2.43 4.729-4 8-4z"/> </svg> {{ days }} days {{ hours }} hours {{ mins }} mins {{ secs }} secs </span> </div> JS: export default { mounted() { // Set the date we're counting down to var countDownDate = new Date("Jan 5,2021 15:37:25").getTime(); // Update the count down every 1 second var x = setInterval(function() { // Get today's date and time var now = new Date().getTime(); // Find the distance between now and the count down date var distance = countDownDate - now; // Time calculations for days,hours,minutes and seconds this.days = Math.floor(distance / (1000 * 60 * 60 * 24)); this.hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)); this.mins = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60)); this.secs = Math.floor((distance % (1000 * 60)) / 1000); // If the count down is finished,write some text if (distance < 0) { clearInterval(x); } },1000); },name: "Splash",data() { return { secs: 0,mins: 0,hours: 0,days: 0 } } }; $user_id 不是来自 $date 动作挂钩的参数。

使用的 PHP 函数

  • date - 格式化本地时间/日期
  • d - 月份中的两位数(带前导零)- 01 到 31
  • m - 月份的两位数表示 - 01(一月)到 12(十二月)

所以你得到:

woocommerce_cart_calculate_fees

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...