来自外部的 Woocommerce 订阅访问 php
Woocommerce Subscription access from external php
我有一个带有 Woocommerce 和 Woocommerce 订阅插件的 Wordpress 网站。
我需要编写一些 PHP 脚本来检查用户是否具有活动订阅状态。
我想从数据库中获取信息:用户 ID、状态(如果它处于活动状态 - 如果已付款并续订)、订阅结束日期...
问题是我连订阅信息保存在哪里都不知道?
谁能给我写一段代码,其中包含一个查询,该查询会给我一个订阅用户列表,以及前面提到的必要信息?
稍后我会 post 更新代码,目前,我只需要查询方面的帮助和在数据库表中查找位置的指导。
我想你的意思是你想从 Wordpress 安装中的 PHP 文件访问 Wordpress/Woocommerce 订阅 API。为此,我认为以下内容会有所帮助:
在 wp-content/plugins 中创建一个名为 woocommerce-subscriptions-status-checker
的文件夹
在上面的新文件夹中创建一个名为 woocommerce-subscriptions-status-checker.php 的文件。
在文件中添加这段代码:
/*
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 的插件部分并激活您的新插件。
您可以尝试 Woocommerce REST API。
https://docs.woocommerce.com/document/woocommerce-rest-api/
基本上,Woocommerce 提供 restful API 来访问数据并在传统 woocommerce 工作流程之外与电子商务系统交互。
这是我的解决方案(不是说它是最好的,但对我来说,它解决了我遇到的问题)。
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' );
我有一个带有 Woocommerce 和 Woocommerce 订阅插件的 Wordpress 网站。
我需要编写一些 PHP 脚本来检查用户是否具有活动订阅状态。
我想从数据库中获取信息:用户 ID、状态(如果它处于活动状态 - 如果已付款并续订)、订阅结束日期...
问题是我连订阅信息保存在哪里都不知道?
谁能给我写一段代码,其中包含一个查询,该查询会给我一个订阅用户列表,以及前面提到的必要信息?
稍后我会 post 更新代码,目前,我只需要查询方面的帮助和在数据库表中查找位置的指导。
我想你的意思是你想从 Wordpress 安装中的 PHP 文件访问 Wordpress/Woocommerce 订阅 API。为此,我认为以下内容会有所帮助:
在 wp-content/plugins 中创建一个名为 woocommerce-subscriptions-status-checker
的文件夹
在上面的新文件夹中创建一个名为 woocommerce-subscriptions-status-checker.php 的文件。
在文件中添加这段代码:
/*
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 的插件部分并激活您的新插件。
您可以尝试 Woocommerce REST API。
https://docs.woocommerce.com/document/woocommerce-rest-api/
基本上,Woocommerce 提供 restful API 来访问数据并在传统 woocommerce 工作流程之外与电子商务系统交互。
这是我的解决方案(不是说它是最好的,但对我来说,它解决了我遇到的问题)。
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' );