php - 在每月 20 日和 3 日之间隐藏取消按钮(Woocommerce 订阅)
问题描述
我正在研究 Woocommerce 订阅。
下面的代码隐藏了“取消”和“重新激活”按钮,特定产品 (ID:1812) 除外。
我想更深入地隐藏每月 20 日和 3 日之间的“取消”和“重新激活”按钮。
你能帮我实现吗?
/**
* Remove the "Change Payment Method" button from the My Subscriptions table.
*
* This isn't actually necessary because @see eg_subscription_payment_method_cannot_be_changed()
* will prevent the button being displayed, however, it is included here as an example of how to
* remove just the button but allow the change payment method process.
*/
function eg_remove_my_subscriptions_button( $actions, $subscription ) {
$is_my_product = false;
if ( sizeof( $subscription_items = $subscription->get_items() ) > 0 ) {
foreach ( $subscription_items as $item_id => $item ) {
$product = $item->get_product();
if ( $product->get_id() == 1812 ) {
$is_my_product = true;
break;
}
}
}
if ( $is_my_product ) return $actions;
foreach ( $actions as $action_key => $action ) {
switch ( $action_key ) {
case 'change_payment_method': // Hide "Change Payment Method" button?
// case 'change_address': // Hide "Change Address" button?
// case 'switch': // Hide "Switch Subscription" button?
// case 'resubscribe': // Hide "Resubscribe" button from an expired or cancelled subscription?
// case 'pay': // Hide "Pay" button on subscriptions that are "on-hold" as they require payment?
case 'reactivate': // Hide "Reactive" button on subscriptions that are "on-hold"?
case 'cancel': // Hide "Cancel" button on subscriptions that are "active" or "on-hold"?
unset( $actions[ $action_key ] );
break;
default:
error_log( '-- $action = ' . print_r( $action, true ) );
break;
}
}
return $actions;
}
add_filter( 'wcs_view_subscription_actions', 'eg_remove_my_subscriptions_button', 100, 2 );
谢谢!
解决方案
您可以像这样检查今天的日期是在当月 19 日之后还是在当月 4 日之前。
获取今天的日期:
date( 'j' )
检查是否在 $n->$m 范围内:
date( 'j' ) > $n-1 && date( 'j' ) < $m+1
最后:
function eg_remove_my_subscriptions_button( $actions, $subscription ) {
$is_my_product = false;
if ( sizeof( $subscription_items = $subscription->get_items() ) > 0 ) {
foreach ( $subscription_items as $item_id => $item ) {
$product = $item->get_product();
if ( $product->get_id() == 1812 ) {
$is_my_product = true;
break;
}
}
}
if ( $is_my_product ) return $actions;
if ( date( 'j' ) > 3 && date( 'j' ) < 20 ) return $actions;
foreach ( $actions as $action_key => $action ) {
switch ( $action_key ) {
case 'change_payment_method': // Hide "Change Payment Method" button?
// case 'change_address': // Hide "Change Address" button?
// case 'switch': // Hide "Switch Subscription" button?
// case 'resubscribe': // Hide "Resubscribe" button from an expired or cancelled subscription?
// case 'pay': // Hide "Pay" button on subscriptions that are "on-hold" as they require payment?
case 'reactivate': // Hide "Reactive" button on subscriptions that are "on-hold"?
case 'cancel': // Hide "Cancel" button on subscriptions that are "active" or "on-hold"?
unset( $actions[ $action_key ] );
break;
default:
error_log( '-- $action = ' . print_r( $action, true ) );
break;
}
}
return $actions;
}
add_filter( 'wcs_view_subscription_actions', 'eg_remove_my_subscriptions_button', 100, 2 );
推荐阅读
- docker - 如何使用 gitlab CI/CD 变量在 docker-compose.yml 中进行更改
- javascript - 重构具有相同对象属性值的数组
- mediawiki - 如何在 Mediawiki 的信息框中调整图像大小?
- css - 角材料在条件下移除垫子无线电容器
- xml - 将html字符串转换为html文档时,DOMParser将子样式添加到父项
- android - Xamarin 自定义渲染器事件未从 TabbedPage 触发
- php - PHP conditional logic AND OR
- android - 片段中的Android小部件视图在Rx订阅中获得IllegalStateException
- php - 如何在 PHP 中使用 Firestore 检索带有时间戳的文档的日期?
- r - 过滤R中指定单词旁边的单词的所有行