基于该问题:Disable cancel subscription for a specific product in WooCommerce我们尝试调整代码,因为这里的代码不能100%满足我们的需求。
我们创建了以下代码来隐藏我的帐户页面上的订阅按钮,但仅适用于特定的订阅产品。适用于可变产品的所有变化。为此,我们希望使用数组来定义所有变体 ID。但我认为,如果我们只为每个变体添加产品中的对象 ID,它也会起作用。然而,这就是我到目前为止所拥有的。目前它隐藏了每个订阅的按钮。所以我认为我的代码存在逻辑问题。
这很奇怪......当我设置 ID 时,它会以某种方式隐藏其他产品的按钮,而不是具有特定 ID 的产品的按钮。因此,该代码适用于所有其他产品,而不适用于具有确切 ID 的产品。为什么?
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() == 16189, 16190 ) {
$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 );
知道这里出了什么问题吗? :)
干杯&谢谢
最佳答案
首先,此行 if ( $product->get_id() == 16189, 16190 ) {
存在错误。您正在寻找的是 in_array
函数。
定义您的特定产品 ID 数组。
$specific_products = array( 16189, 16190 );
然后使用 in_array
函数添加一个条件,判断您的产品是否在您定义的 $specic_products
中。
if ( in_array( $product->get_id(), $specific_products ) ) {
您还必须使用 woocommerce_my_account_my_orders_actions
过滤器 Hook 。
“woocommerce_my_account_my_orders_actions”过滤器 Hook 的完整代码。
function eg_remove_my_subscriptions_button( $actions, $subscription ) {
$is_my_product = false;
$specific_products = array( 16189, 16190 );
if ( sizeof( $subscription_items = $subscription->get_items() ) > 0 ) {
foreach ( $subscription_items as $item_id => $item ) {
$product = $item->get_product();
if ( in_array( $product->get_id(), $specific_products ) ) {
$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( 'woocommerce_my_account_my_orders_actions', 'eg_remove_my_subscriptions_button', 100, 2 );
之前
之后
“woocommerce_my_account_my_orders_actions”过滤器 Hook 的完整代码。
function eg_remove_my_subscriptions_button( $actions, $subscription ) {
$is_my_product = false;
$specific_products = array( 16189, 16190 );
if ( sizeof( $subscription_items = $subscription->get_items() ) > 0 ) {
foreach ( $subscription_items as $item_id => $item ) {
$product = $item->get_product();
if ( in_array( $product->get_id(), $specific_products ) ) {
$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 );
之前
之后
关于php - WooCommerce 订阅隐藏特定产品的按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71085566/