Reputation: 2522
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";
}
Upvotes: 5
Views: 1884
Reputation: 26329
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 with plugins_loaded
depending on your ultimate use case.
add_action( 'init', 'so_26193801_event_listener' );
function so_26193801_event_listener(){
if(isset($_REQUEST['Action'])){
$Action = $_REQUEST['Action'];
switch($Action){
case "ValidateSubscription":
$Subscriptions = WC_Subscriptions_Manager::get_all_users_subscriptions();
print_r($Subscriptions);
break;
default:
echo "invalid action";
}
} else {
echo "no action specified";
}
}
Upvotes: 2
Reputation: 432
That is not how you include WordPress functions in external code. Try this.
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";
}
Upvotes: 4