1

We want to move a CRON job from being triggered in WordPress to a server-based job. Right now the code lives in the functions.php file in our WP theme, and we use the WPCrontrol plugin for scheduling. It runs fine, no problem there. The issue is the code has WooCommerce hooks in it. A server-based job has no idea what to do with WC_Orders, for example. How can we resolve this?

0

I think you can resolve this with this way:

  • Step 1: You create a PHP Page with URL you want in your theme like this (page-url-custom-cron.php) to call a Hook WooCommerce
  • Step 2: You use server-based job Curl to the URL: http://yourdomain/url-custom-cron. And now the PHP code page will call WooCommerce hooks for you.

Hope this can help you done your job

2
  • I spoke too soon. I was able to get the job to run using cURL, but it still doesn't know what to do with the hooks. PHP Fatal error: Uncaught Error: Call to undefined function wc_get_orders() – Thomas Oct 23 '20 at 19:03
  • If your WooCommerce version is 2.2+ you must use $order = wc_get_order( $order_id );. But with the older version you must use $order = new WC_Order( $order_id );. hope this help – xuan hung Nguyen Nov 23 '20 at 4:15
0

Here's the solution:

  1. Created a file (run_cron.php) which called the job I needed to run via cURL and placed it in the child theme folder: cURL:

    <?php
      define('RUN_CRON_URI', 'https://path-to-file-to-be-executed/process_orders.php');
    
      $curl = curl_init();
      curl_setopt_array($curl, [
          CURLOPT_URL => RUN_CRON_URI,
          CURLOPT_RETURNTRANSFER => true,
          CURLOPT_SSL_VERIFYPEER => false,
          CURLOPT_SSL_VERIFYHOST => false
      ]);
    
      $result = curl_exec($curl);
    
      if (curl_error($curl)) {
          throw new Exception(curl_error());
      } else {
          $httpStatusCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
          if ($httpStatusCode != 200) {
              throw new Exception('Submission failed with this status: ' . $httpStatusCode);
          } elseif (stripos($result, 'error') !== false) {
              throw new Exception('Error detected: ' . $result);
          }
      } ?>
    
  2. Moved the job to be executed into the parent theme folder.

  3. Added the following at the top of the executable file in opening and closing PHP tags:

    /** * @package OceanWP WordPress theme */

    require_once("../../../wp-load.php");

Rest of the code...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.