编程语言
首页 > 编程语言> > 如何在PHP中正确使用WooCommerce订阅API?

如何在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";
}

标签:wordpress,php,woocommerce,subscriptions
来源: https://codeday.me/bug/20190528/1172634.html