如何在PHP中正确使用WooCommerce订阅API?

文档非常稀缺.我正在尝试创建一些使用woocommerce订阅(一个woocommerce插件)api的代码来获取订阅列表和每个订阅的所有细节.那里的文档和例子是如此稀缺和虚弱,我无法做到正确.以下代码不会产生任何错误,但不会输出任何订阅(只是一个空白页).如何列出所有订阅的所有详细信息?

<?php

if(isset($_REQUEST['Action']))
{
    $Action = $_REQUEST['Action'];
    switch($Action)
    {
        case "ValidateSubscription":
            chdir("../wp-content/plugins/woocommerce-subscriptions/classes");
            include '../../woocommerce/woocommerce.php';
            //include '../woo-includes/woo-functions.php';
            //include '../woo-includes/class-wc-dependencies.php';
            include 'class-wc-subscriptions-manager.php';

            $Subscriptions = WC_Subscriptions_Manager::get_all_users_subscriptions();
            print_r($Subscriptions);
            break;
        default:
            echo "invalid action";
    }
}else
{
    echo "no action specified";
}

解决方法:

这不是你在外部代码中包含WordPress功能的方式.试试这个.

if(isset($_REQUEST['Action']))
{
    $Action = $_REQUEST['Action'];
    switch($Action)
    {
        case "ValidateSubscription":

            include('../wp-load.php'); //Guessing this path based on your code sample... should be wp root

            $Subscriptions = WC_Subscriptions_Manager::get_all_users_subscriptions();
            print_r($Subscriptions);
            break;
        default:
            echo "invalid action";
    }
}else
{
    echo "no action specified";
}
上一篇:处理时间序列数据的高端模型Transformer和代码实现


下一篇:rt_container_of 原理(通过结构体成员地址反推出结构体地址,进而访问结构体中的所有成员)