Documentation is very scarce. I’m trying to create some code that uses WooCommerce subscriptions (a plugin for WooCommerce) API to get a list of subscriptions and all of the details of each subscription. The docs and examples out there are just so scarce and weak I can’t get it right. The following code didn’t produce any errors but isn’t outputting any subscriptions (just a blank page). How can I list all the details of all the subscriptions?
<?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";
}
That is not how you include WordPress functions in external code. Try this.
Piggybacking on @ChuckMac’s answer, I think it can be refined further by respecting WordPress’s Plugin API and knowing at which point the various parts of WordPress are up and running. Pretty much all of WP is loaded by the
init
hook so that’s a safe place to run functions that are “listening” for a$_REQUEST
variable. You may be able to get away withplugins_loaded
depending on your ultimate use case.