【问题标题】:Woocommerce Subscription access from external php从外部 php 访问 Woocommerce 订阅
【发布时间】:2018-05-25 20:22:24
【问题描述】:

我有一个带有 Woocommerce 和 Woocommerce 订阅插件的 Wordpress 网站。

我需要编写一些 PHP 脚本来检查用户是否有活跃的订阅状态。

我想从数据库中获取信息:用户 ID、状态(如果它处于活动状态 - 如果已付款并续订)、订阅结束日期...

问题是我什至不知道订阅信息保存在哪里?

谁能给我写一个sn-p代码,其中包括一个查询,它会给我一个订阅的用户列表,以及前面提到的必要信息?

稍后我将发布带有代码的更新,目前,我只需要查询方面的帮助和在数据库表中查找位置的指导。

【问题讨论】:

    标签: php wordpress woocommerce subscription


    【解决方案1】:

    我认为您的意思是您想从 Wordpress 安装中的 PHP 文件访问 Wordpress/Woocommerce 订阅 API。为此,我认为以下内容会有所帮助:

    1. 在 wp-content/plugins 中创建一个名为 woocommerce-subscriptions-status-checker 的文件夹

    2. 在上述新文件夹中创建一个名为 woocommerce-subscriptions-status-checker.php 的文件。

    3. 在文件中添加这段代码:

    /* 
    Plugin Name: Woocommerce Subscriptions Status Checker
    Plugin URI: http://www.XXXXXX.com
    Description: XXXXXXXXX
    Author: XXXXXX
    Version: 2.0 
    Author URI: http://www.XXXXX.com
    */
    
    if ( ! defined( 'ABSPATH' ) ) {
      exit;
    }
    
    class WC_Subscriptions_Status_Checker {
    
      function __construct() {
    
        // Avoids the function firing if we are in wp-admin backend
        if ( !is_admin() ) {
          // Fires on every page load after WordPress, all plugins, and the theme are fully loaded and instantiated
          add_action( 'wp-loaded', array( $this, 'check_current_user' ) );
        }
      }
    
    
      function check_current_user() {
    
        // Check if user is even logged in, if not exit
        if ( !is_user_logged_in() ) return;
    
        $current_user   = wp_get_current_user(); // get current WP_User
        $user_id    = $current_user->ID; // get user id
    
        $is_subscription = wcs_user_has_subscription( $user_id ); // check if user has subscription
        if ( $is_subscription )
        {
          $subscriptions = wcs_get_users_subscriptions( $user_id ); // get array of all subscriptions
    
          // Check if there is one subscription or multiple subscriptions per user
          if ( count( $subscriptions ) > 1 ) {
    
            // Example if you wanted to loop through all subscriptions, in the case of the user having multiple subscriptions
            foreach ( $subscriptions as $sub_id => $subscription ) {
              if ( $subscription->get_status() == 'active' ) {
                // Do something
              }
            }
          } else { // Only 1 subscription
    
            $subscription  = reset( $subscriptions ); // gets first and only value
            if ( $subscription->get_status() == 'active' ) {
                // Do something
              }
          }
        }
    
      }
    }
    new WC_Subscriptions_Status_Checker();
    

    访问 wp-admin 的插件部分并激活您的新插件。

    【讨论】:

    • 您好!谢谢回复。我的订阅计划将是每月或每年。我记得每次用户登录并访问该网站时我都会检查订阅状态?!我复制了您的代码并进行了测试。我得到一个活跃订阅列表。 $post->ID 显示活动订阅的 ID。如果我有登录用户的 ID,如何将结果限制为一个用户和当前订阅状态?
    • 嗨,你肯定想在插件中做到这一点。我将编辑答案,向您展示如何操作。
    • @Tyler 是否可以获取客户的支付网关详细信息。或者是否可以通过您的插件让客户自动付款?
    【解决方案2】:

    您可以尝试 Woocommerce REST API。

    https://docs.woocommerce.com/document/woocommerce-rest-api/

    基本上,Woocommerce 提供了一个 RESTful API 来访问数据并与传统 woocommerce 工作流程之外的电子商务系统进行交互。

    【讨论】:

      【解决方案3】:

      这是我的解决方案(不是说它是最好的,但对我来说,它解决了我遇到的问题)。

      function.php 中的代码(WP 子主题)

      add_action('init','getWPuser');
      function getWPuser(){
      $current_user = wp_get_current_user();
      return $current_user;
      }
      

      自定义网站

      我的自定义网站中需要用户信息的代码。

      require_once '../wp-load.php';
      require_once '../wp-content/themes/h-code-child/functions.php';
      require_once '../wp-includes/pluggable.php';
      require_once '../wp-blog-header.php';
      $current_user = getWPuser();
      

      保存用户 ID 信息。

      $logged_user_id = $current_user->ID;
      

      检查特定订阅是否活动

      $has_free = wcs_user_has_subscription( $logged_user_id, $product_id, 'active' );
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-04-04
        • 1970-01-01
        • 2015-11-30
        • 2019-01-03
        • 1970-01-01
        • 2015-01-31
        • 2020-08-09
        • 1970-01-01
        相关资源
        最近更新 更多