How Can I Properly Use WooCommerce Subscriptions API in PHP? How Can I Properly Use WooCommerce Subscriptions API in PHP? wordpress wordpress

How Can I Properly Use WooCommerce Subscriptions API in PHP?


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";}


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";    }}