WooCommerce 订阅隐藏特定产品的按钮
WooCommerce Subscription hide buttons for specific products
基于这个问题: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
函数添加您的产品是否在您定义的 $specific_products
中的条件。
if ( in_array( $product->get_id(), $specific_products ) ) {
您还必须使用 woocommerce_my_account_my_orders_actions
过滤器钩子。
'woocommerce_my_account_my_orders_actions' 过滤器挂钩的完整代码。
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' 过滤器挂钩的完整代码。
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 );
之前
之后
基于这个问题: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
函数添加您的产品是否在您定义的 $specific_products
中的条件。
if ( in_array( $product->get_id(), $specific_products ) ) {
您还必须使用 woocommerce_my_account_my_orders_actions
过滤器钩子。
'woocommerce_my_account_my_orders_actions' 过滤器挂钩的完整代码。
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' 过滤器挂钩的完整代码。
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 );
之前
之后