Access woo-commerce methods in custom plugin cron callback

I have developed a plugin in which i need to create woo-commerce order from cron job. I have created simple test function as follow:

<?php
if ( ! defined( ‘ABSPATH’ ) ) exit; // Exit if accessed directly

function amwscp_order_interval() {
$current_delay = 15*60;
return array(
‘order_interval’ => array(‘interval’ => $current_delay, ‘display’ => ‘Order refresh interval’),
);
}

class AMWSCPF_Order_Cron {

public static function doSetup() {
add_filter(‘cron_schedules’, ‘amwscp_order_interval’);
//Delete old (faulty) scheduled cron job from prior versions
$next_refresh = wp_next_scheduled(‘order_interval’);
if ($next_refresh)
wp_unschedule_event($next_refresh, ‘order_interval’);
}

public static function scheduleUpdate () {
//Set the Cron job here. Params are (when, display, hook)
$next_refresh = wp_next_scheduled(‘amwscpf_order_import_hook’);
if (!$next_refresh )
wp_schedule_event(time(), ‘order_interval’, ‘amwscpf_order_import_hook’);
}

}
AMWSCPF_Order_Cron::doSetup();
AMWSCPF_Order_Cron::scheduleUpdate();

add_action(‘amwscpf_order_import_hook’,’amwscpf_import_all_order’);
do_action(‘amwscpf_order_import_hook’);

function amwscpf_import_all_order(){
// use wc_get_product() on WC 2.1+
if ( function_exists(‘wc_get_product’) ) {
print_r(wc_get_product( 52 ));
// use get_product() on WC 2.0+
} else ( function_exists(‘get_product’) ) {
print_r(get_product( 52 ));exit;
}
}

But it gives me this fatal error

Fatal error: Class ‘WC_Product’ not found in /var/www/html/exportfeed/wp-content/plugins/exportfeed-woocommerce-data-feed-for-amazon-marketplace/amwscpf-order-cron.php on line 45

When I hit the same function manually it returns me product, but with cron it gives me this error. Is there any method with which I can get access to all the woo-commerce methods when cron runs ?
I am really stuck on this one. I would really appreciate any kind of gelp and suggestion. thanks.

Read more here:: Access woo-commerce methods in custom plugin cron callback

Leave a Reply

Your email address will not be published. Required fields are marked *