代码片段允许在WooCommerce订阅中仅1个活跃订阅



我有一个片段,它允许只有一个活动订阅,所以他们不能订购另一个,直到取消。

但是这段代码也阻塞了up-downgrade-函数。有任何想法,我可以得到只是交换机订阅部分可用?

add_filter('woocommerce_add_to_cart_validation', 'check_num_of_subscriptions', 10, 2);
function check_num_of_subscriptions( $valid, $product_id )
{
$product_to_add = wc_get_product( $product_id );
if ( $product_to_add instanceof WC_Product_Subscription || $product_to_add instanceof WC_Product_Variable_Subscription) {
// alternative use: $has_sub = wcs_user_has_subscription( '', '', 'active' );
if ( has_active_subscription() ) {
// cart validation must fail, because user is not allowed to have multiple subscriptions
wc_clear_notices();
wc_add_notice(__('Du hast bereits ein aktives Abonnement.', 'woocommerce'), 'error');
return false;
} else {
return true;
}
}
return $valid;
}
function has_active_subscription()
{
$user_id = get_current_user_id();
$active_subscriptions = get_posts(array(
'numberposts' => -1,
'meta_key' => '_customer_user',
'meta_value' => $user_id,
'post_type' => 'shop_subscription',
'post_status' => 'wc-active'
));
if (!empty($active_subscriptions))
return true;
else
return false;
}
add_filter('woocommerce_add_to_cart_validation', 'check_num_of_subscriptions', 10, 2);

function check_num_of_subscriptions( $valid, $product_id ){
$product_to_add = wc_get_product( $product_id );
if ($product_to_add instanceof WC_Product_Subscription || $product_to_add instanceof WC_Product_Variable_Subscription){
// alternative use: $has_sub = wcs_user_has_subscription( '', '', 'active' );

if ( has_active_subscription() && (! has_woocommerce_subscription('',$product_id,''))) {
// cart validation must fail, because user is not allowed to have multiple subscriptions
wc_clear_notices();
wc_add_notice(__('You already have an active subscription.', 'woocommerce'), 'error');
return false;
} else{
return true;
}
}
return $valid;
}
function has_active_subscription(){
$user_id = get_current_user_id();
$active_subscriptions = get_posts(array(
'numberposts' => -1,
'meta_key' => '_customer_user',
'meta_value' => $user_id,
'post_type' => 'shop_subscription',
'post_status' => 'wc-active'

));
if (!empty($active_subscriptions))
return true;
else
return false;
}

function has_woocommerce_subscription($the_user_id, $the_product_id, $the_status) {
$current_user = wp_get_current_user();
if (empty($the_user_id)) {
$the_user_id = $current_user->ID;
}
if (WC_Subscriptions_Manager::user_has_subscription( $the_user_id, $the_product_id, $the_status)) {
return true;
}
}

此代码将运行升级和降级,测试和工作。

相关内容

  • 没有找到相关文章

最新更新