title: WooCommerce Objects not available when calling them from a WordPress cron
body: <p>I have a "strange" problem with calling WooCommerce objects from a wp_cron job.</p>
  
  <p>I created the next function which gets called at an interval of 10 minutes to fetch the current stock position from our warehouse and update the available stock in WooCommerce.</p>
  
  <pre><code>// Update the stock of our products every 10 minutes
  function cron_yp_update_product_stock_b1f6051b() {
      $oConvexStock = YP_get_stock_per_SKU();
  
      $oQuery = new WC_Product_Query( array( "return" =&gt; "objects", "virtual" =&gt; false ) );
      $oResult = $oQuery-&gt;get_products();
      if ( is_array( $oResult ) ) {
          foreach( $oResult as $oProduct ) {
              if ( isset( $oConvexStock[$oProduct-&gt;get_sku()] ) ) {
                  wc_update_product_stock($oProduct, $oConvexStock[$oProduct-&gt;get_sku()], "set" );
              }
          }
      }
  }
  </code></pre>
  
  <p>What it does is retrieve an array in which the SKU codes are the keys and the value is the current stock available in the warehouse. It then queries WooCommerce Products for all non virtual products and compares their SKUs with the once in the warehouse array. If there is a match it will update the current stock position.</p>
  
  <p>Nothing really fancy or advanced. It works fine when I trigger the cronjob from the admin panel (using a cron manager). But when the cronjob triggers autonomous the <code>WC_Product_Query</code> object doesn't populate or something. I can <code>print_r</code> the <code>$oQuery</code> object and it shown an empty object, but when I call the <code>get_products()</code> method it crashes. </p>
  
  <p>So it looks like the WooCommerce class doesn't get initiated or something when the cron is triggered autonomous, but it does/is when calling it via the admin panel.</p>
  
  <p>I tried googling or asking at WordPress.org but to no avail (either no answer of bad search skills).</p>
  
  <p>Before I start tearing apart WordPress and WooCommerce and burning massive amounts of hours does anybody know why this happens and how to fix this?</p>
  
  <p>With regards in advance,
  Michel</p>
  
  <p>UPDATE 1.
  I have been working all day to figure out what is going wrong. I eventually found it. But I still have no clue as to why this works in admin panel and not via the cron. </p>
  
  <p>The problem is in the WP_Query class.</p>
  
  <p>There is a variable called query and a method called query. The query function looks like this :</p>
  
  <pre><code>public function query( $query ) {
      $this-&gt;init();
      $this-&gt;query = $this-&gt;query_vars = wp_parse_args( $query );
      return $this-&gt;get_posts();
  }
  </code></pre>
  
  <p>As you can see, inside this function they are updating $this->query but since PHP isn't typed it is anybodies guess if it will update the variable or the method. Even trickier is that wp_parse_args returns a reference to an array (pointer) when passing an array into it. So PHP is not able to tell if you means a variable or if you are updating a function point. </p>
  
  <p>Bottomline is that for some reason half of the time the variable is updated and half of the time the funtion pointer, which ends the whole processing of the PHP scripts all together. </p>
  
  <p>Why they would ever name a function and a variable the same I have no clue, but now I am stuck because I tried to rename all occurences of $this->query to something else but that gave more problems than it solved. </p>
  
  <p>By the way my PHP version is 7.2.11</p>
  
  <p>If anybody has any idea how to tell PHP which of the two it actually should update, that would help a lot.</p>