登录
首页 >  文章 >  php教程

WooCommerce订阅限制:试用期需查购买记录

时间:2025-08-17 16:54:30 311浏览 收藏

想要更精准地控制 WooCommerce 订阅增强插件的试用期?本文将深入讲解如何修改插件代码,**实现限制试用期需检查用户购买记录**的功能。不同于原始代码仅针对特定产品ID的检查,本文提供的解决方案利用`WP_Query()`函数,**遍历所有产品**,判断用户是否已购买过任何产品。详细代码示例展示了如何替换`$product->get_id()`为`get_the_ID()`,并循环检查用户订阅记录,从而**更全面地限制试用期**。同时,文章还探讨了潜在的性能影响以及代码位置等注意事项,助你优化WooCommerce订阅设置,提升用户体验。

WooCommerce 订阅增强:检查用户是否已购买任何产品以限制试用期

本文将指导你如何修改 WooCommerce 订阅增强插件中的代码,以实现更全面的试用期控制。原始代码仅针对特定产品 ID 检查用户是否已购买,而修改后的代码将检查用户是否购买过任何产品,从而更精确地限制试用期。

修改 limit_trial 函数

原始的 limit_trial 函数使用 $product->get_id() 来获取当前产品的 ID,并以此为依据判断用户是否已经购买过该产品。为了检查所有产品,我们需要使用 WP_Query() 来获取所有产品,然后循环遍历这些产品,检查用户是否订阅了其中任何一个。

以下是修改后的代码:

public static function limit_trial( $trial_length, $product ) {
    if ( $trial_length <= 0 ) {
        return $trial_length ;
    }

    $user_id = get_current_user_id() ;

    if ( ! $user_id ) {
        return $trial_length ;
    }

    $params = array(
        'posts_per_page' => -1,
        'post_type'      => 'product'
    );

    $products = new WP_Query( $params );

    if ( $products->have_posts() ) {  while ( $products->have_posts() ) {  $products->the_post();

        if ( isset( self::$onetime_trial_cache[ $user_id ][ get_the_ID() ] ) ) {
            return self::$onetime_trial_cache[ $user_id ][ get_the_ID() ] ? 0 : $trial_length ;
        }

        if ( $product->is_type( 'variation' ) ) {
            $parent_product = wc_get_product( $product->get_parent_id() ) ;
        } else {
            $parent_product = wc_get_product( get_the_ID() ) ;
        }

        if ( 'no' !== self::get_product_limitation( $parent_product ) ) {
            self::$onetime_trial_cache[ $user_id ][ get_the_ID() ] = false ;
            return $trial_length ;
        }

        if ( 'yes' !== get_post_meta( $parent_product->get_id(), '_enr_limit_trial_to_one', true ) ) {
            self::$onetime_trial_cache[ $user_id ][ get_the_ID() ] = false ;
            return $trial_length ;
        }

        $subscriptions = wcs_get_users_subscriptions( $user_id ) ;

        foreach ( $subscriptions as $subscription ) {
            if ( $subscription->has_product( get_the_ID() ) && ( '' !== $subscription->get_trial_period() || 0 !== $subscription->get_time( 'trial_end' ) ) ) {
                self::$onetime_trial_cache[ $user_id ][ get_the_ID() ] = true ;
                return 0 ;
            }
        }

        self::$onetime_trial_cache[ $user_id ][ get_the_ID() ] = false ;

    } wp_reset_postdata(); }

    return $trial_length ;
}

代码解释:

  1. WP_Query 设置:
    • 'posts_per_page' => -1: 获取所有产品。
    • 'post_type' => 'product': 指定查询的产品类型为 'product'。
  2. 循环遍历产品:
    • if ( $products->have_posts() ) { while ( $products->have_posts() ) { $products->the_post();: 循环遍历所有产品。
    • get_the_ID(): 在循环内部,get_the_ID() 获取当前循环到的产品的 ID。
  3. 替换 $product->get_id():
    • 在原代码中所有使用 $product->get_id() 的地方,都替换为 get_the_ID()。
  4. wp_reset_postdata():
    • 在循环结束后,调用 wp_reset_postdata() 以恢复全局 $post 对象。

注意事项

  • 性能影响: 当产品数量非常多时,使用 WP_Query 获取所有产品可能会影响性能。 可以考虑使用缓存机制来优化性能。
  • 代码位置: 确保将修改后的代码放置在正确的文件中,通常是 WooCommerce 订阅增强插件的相关 PHP 文件。
  • 测试: 在生产环境中使用之前,务必在测试环境中 thoroughly 测试修改后的代码。

总结

通过使用 WP_Query 获取所有产品,并循环遍历它们,可以实现对所有产品而非单个产品的检查,从而更灵活地控制 WooCommerce 订阅增强插件的试用期功能。 这种方法允许你根据用户是否已经购买过任何产品来决定是否提供试用期,从而更好地满足业务需求。

本篇关于《WooCommerce订阅限制:试用期需查购买记录》的介绍就到此结束啦,但是学无止境,想要了解学习更多关于文章的相关知识,请关注golang学习网公众号!

相关阅读
更多>
最新阅读
更多>
课程推荐
更多>