我正在使用 WooCommerce 和 YITH Subscriptions 设置订阅产品,并试图找到一种方法将订阅的开始时间延迟到特定月份(4 月 1 日或 10 月 1 日 - 以接下来的日期为准)。真的很难找到任何关于 YITH 的钩子或覆盖等的参考资料,但我以前见过它完成过,所以希望它不会太难做到。
我还看到您可以从后端手动更改订阅日期,但需要它是自动的。
所以理论上它会像这样工作:
将订阅添加到购物车。在结帐时,将完成以下计算并在创建订阅设置之前将其应用于订阅设置。
`
$mn = date("m"); // this month
if ( date("m") >= 4 || date("m") < 10 ) { // if it's April - Sep, first sub in Oct this year
$start = date("Y-m-d H:i:s", strtotime("1st October this year"));
} else if ( date("m") >= 10 ) {
$start = date("Y-m-d H:i:s", strtotime("1st April next year"));
} else {
$start = date("Y-m-d H:i:s", strtotime("1st April this year"));
}
`
我想我已经明白了,这似乎有效。通过添加到购物车和结帐过程进行了测试。
使用此页面的基本功能:根据 Woocommerce 中的自定义购物车项目数据更改购物车项目价格
还有购物车项目的 var_dump 以查找我想要的数组项目。
add_filter( 'woocommerce_add_cart_item', 'add_custom_cart_item_data', 10, 2 );
function add_custom_cart_item_data( $cart_item_data, $cart_item ) {
$mn = date("m"); // this month
if ( date("m") >= 4 || date("m") < 10 ) { // if it's April - Sep, first sub in Oct this year
$start = strtotime("1st October this year");
} else if ( date("m") >= 10 ) { // if it's Oct - Dec, first sub in April next year
$start = strtotime("1st April next year");
} else { // if it's Jan - Mar, first sub in April this year
$start = strtotime("1st April this year");
}
if ( isset($cart_item_data["ywsbs-subscription-info"]["next_payment_due_date"]) ) {
$cart_item_data["ywsbs-subscription-info"]["next_payment_due_date"] = $start;
}
return $cart_item_data;
}
add_action( 'woocommerce_before_calculate_totals', 'set_custom_cart_item_price', 20, 1 ); 函数 set_custom_cart_item_price( $cart ) {
$mn = date("m"); // this month
if ( date("m") >= 4 || date("m") < 10 ) { // if it's April - Sep, first sub in Oct this year
$start = strtotime("1st October this year");
} else if ( date("m") >= 10 ) { // if it's Oct - Dec, first sub in April next year
$start = strtotime("1st April next year");
} else { // if it's Jan - Mar, first sub in April this year
$start = strtotime("1st April this year");
}
// Loop through cart items and apply new date value.
foreach ( $cart->get_cart() as $cart_item ){
if ( isset($cart_item_data["ywsbs-subscription-info"]["next_payment_due_date"]) ) {
$cart_item_data["ywsbs-subscription-info"]["next_payment_due_date"] = $start;
}
}
}