我有一个基于 WooCommerce 的 Wordpress 会员网站,带有 WooCommerce-Memberships 插件。
场景:
- Membership Plan = Gold-Plan-Membership / 1-month
- simple Product = Gold-Product-Membership / €10,00
当客户购买“Gold-Product-Membership”产品时,激活订阅“Gold-Plan-Membership”1个月。到目前为止一切顺利。
我想一次将成员限制为一个活跃的成员。如果任何其他会员资格处于活动状态,我想要做的是阻止从购买中创建任何新会员资格。
现在。以下代码检查有效的客户会员计划及其状态('active'、'complimentary'、'pending'、'free_trial')和 Memberships 不会为此订单创建新的用户会员资格。以下代码将检查所有有权访问受限内容的会员状态。
但只检查会员计划,而不是购买其产品。
我会解释的。客户购买产品"Gold-Product-Membership"
,完成结帐程序并实际执行付款,该产品的订单得到处理。虽然实际上"Gold-Plan-Membership"
没有激活。
很明显,这是一个问题。
我正在寻找一种适用于产品但同时与其与会员计划状态的关系集成的解决方案。
- 我想一次将成员限制为一个活跃的成员。
提前致谢
编码
/**
* There are two ways we could prevent purchasing more than one membership:
* 1. prevent access if the customer already has an active membership
* 2. prevent access if the customer already has any membership
*
* This snippet shows the second scenario.
*/
/**
* Do not grant membership access to purchasers if they already hold any membership, regardless of status
*
* @param bool $grant_access true if the purchaser should get memberships access from this order
* @param array $args {
* @type int $user_id purchaser's WordPress user ID
* @type int $product_id the ID of the access-granting product
* @type int $order_id the ID of order for this purchase
* }
* @return bool $grant_access
*/
function sv_wc_memberships_limit_to_one_membership( $grant_access, $args ) {
// get all active memberships for the purchaser, regardless of status
$memberships = wc_memberships_get_user_memberships( $args['user_id'] );
// if there are any memberships returned, do not grant access from purchase
if ( ! empty( $memberships ) ) {
return false;
}
return $grant_access;
}
add_filter( 'wc_memberships_grant_access_from_new_purchase', 'sv_wc_memberships_limit_to_one_membership', 1, 2 );