WooCommerce 订阅 - 只允许用户一个有效订阅

WooCommerce Subscriptions - Only Allow user one active subscription

我正在为我的客户构建基于会员/订阅的网站,我们正在使用 Woocommerce Subscriptions and Woocommerce Memberships 插件。

现在的问题是我的客户正在构建一些基本上允许用户购买升级的促销页面。这很好,但我的客户只希望客户 (及其关联的会员) 有一份独特的订阅。

因此商定的解决方案是,在购买任何新订阅产品时,应取消所有其他订阅。所有关联的会员 deleted/cancelled 和只有最新的订阅应与其随附的会员一起保持活跃。

所以我尝试构建这个解决方案,但它就是行不通,所以任何 advise/direction 都将受到欢迎。

我尝试过的:

function wp56908_new_order_housekeeping ($order_id)
{
    $args = array(
        'subscriptions_per_page' => -1,
        'customer_id'            => get_current_user_id(),
    );

    $subscriptions = wcs_get_subscriptions($args);

    foreach ($subscriptions as $subscription) {
        $s_order_id = method_exists( $subscription, 'get_parent_id' ) ? $subscription->get_parent_id() : $subscription->order->id;
        if ($s_order_id != $order_id) {
            $cancel_note = 'Customer purchased new subscription in order #' . $order_id;
            $subscription->update_status( 'cancelled', $cancel_note );
        }
    }
}
add_action( 'woocommerce_thankyou', 'wp56908_new_order_housekeeping',  10, 1  );

这是我从 WooCommerce 收到的关于函数 wcs_get_subscriptions().

问题的支持电子邮件

Thanks for contacting support and report us this issue!

I've tried it in a local install and I confirm that this seems to be a bug! I've already reported it to our development team and a patch should be included in next updates. By now, if you're in a real hurry and want to get it working, you could search for this code (in wcs-functions.php line 483):

// We need to restrict subscriptions to those which contain a certain product/variation if ( ( 0 != $args['product_id'] && is_numeric( $args['product_id'] ) ) || ( 0 != $args['variation_id'] && is_numeric( $args['variation_id'] ) ) ) { $query_args['post__in'] = wcs_get_subscriptions_for_product( array( $args['product_id'], $args['variation_id'] ) ); }

And replace it with something like:

// We need to restrict subscriptions to those which contain a certain product/variation if ( ( 0 != $args['product_id'] && is_numeric( $args['product_id'] ) ) || ( 0 != $args['variation_id'] && is_numeric( $args['variation_id'] ) ) ) { $prod_args = array( $args['product_id'], $args['variation_id'] ); $prod_args = array_filter($prod_args, function($prod_args) { return ($prod_args !== 0); }); $query_args['post__in'] = wcs_get_subscriptions_for_product( $prod_args ); }

Please take in mind that this is not the ideal solution because you'll be modifying our plugin's code directly and these changes can be lost after updating, but if you feel comfortable with code and want to take the risk as a temporary workaround until we add the proper patch in our plugin, you can do it. ;)

Let me know if you need any help with this!

Thanks, Bernat

WC 订阅插件中有一项新设置,可让您限制为一个有效订阅。您可以在此处找到更多信息:https://woocommerce.com/document/subscriptions/store-manager-guide/#:~:text=Limit%20subscriptions,the%20subscription%20from%20the%20dropdown.

最终用户将收到消息: “您订阅了该产品。选择新的订阅将替换您现有的订阅。”