0

I have made a script that extracts products from WooCommerce and a script that update stock in WooCommerce. The shop have about 750 parent products and total number of variants under these parents are about 4500. I use Automattic's PHP SDK for WooCommerce.

When i run my "extract products" function to save all product information in an external database, it will run fine. But if i re-run the script 15 minutes later, i get this message:

Fatal error: Uncaught Automattic\WooCommerce\HttpClient\HttpClientException: Error: <p>En kritisk feil har inntruffet på ditt nettsted.</p><p><a href="https://wordpress.org/support/article/debugging-in-wordpress/">Lær mer om feilsøking i WordPress.</a></p> [internal_server_error] in /Applications/XAMPP/xamppfiles/htdocs/portland_integrasjon/wc/inc/HttpClient/HttpClient.php:350 Stack trace: #0 /Applications/XAMPP/xamppfiles/htdocs/portland_integrasjon/wc/inc/HttpClient/HttpClient.php(386): Automattic\WooCommerce\HttpClient\HttpClient->lookForErrors(Object(stdClass)) #1 /Applications/XAMPP/xamppfiles/htdocs/portland_integrasjon/wc/inc/HttpClient/HttpClient.php(422): Automattic\WooCommerce\HttpClient\HttpClient->processResponse() #2 /Applications/XAMPP/xamppfiles/htdocs/portland_integrasjon/wc/inc/Client.php(82): Automattic\WooCommerce\HttpClient\HttpClient->request('products', 'GET', Array, Array) #3 /Applications/XAMPP/xamppfiles/htdocs/portland_integrasjon/wc/wc-procucts-extract.php(8): Automattic\WooCommerce\Client->g in /Applications/XAMPP/xamppfiles/htdocs/portland_integrasjon/wc/inc/HttpClient/HttpClient.php on line 350

Error message is in Norwegian, but in English it says: A critical error have occured on you website.

The API call i use for "extract products" are:

 $products = $woocommerce->get('products', $parameters=['per_page' => 99999]);
 $productVariation = $woocommerce->get('products/'.$productId.'/variations/'.$child);

If i wait a few more minutes and run another API hit for WooCommerce, lets say "update stock", and then re-run "extract products", everything works fine.

So my problem is: I want to be able to re-run "extract-products" function several times without having to wait or run another API hit first.

Any sugestions?

  • You might want to ask the author of 'Automattic's PHP SDK for WooCommerce' or look in their support for HttpClient or http errors. If it's happening intermittently it could be due to a networking issue where the code is running. E.g. poor network connection, firewall? – mozboz Jul 26 at 9:44
0

So, after fiddling with this all week, i finally found the solution.

In my original script, i asked for 99999 products with this line:

$products = $woocommerce->get('products', $parameters=['per_page' => 99999]); 

I also had this function in functions.php in WP:

function maximum_api_filter($query_params) {
    $query_params['per_page']["maximum"]= 99999;
    return $query_params;
}

add_filter('rest_product_collection_params', 'maximum_api_filter');

The new changes i made to the script was that i changed "per_page" to 100, and then looped through it x number of times and finally merged the arrays.

The new solution works fine, and the fatal error i was getting before was maybe caused by Wordpress trying to serve me several hundreds of products at one go witch may have overloaded or triggered something on the server. Dont know this for sure, but new solution works fine.

| improve this answer | |
  • Nice. There's also a WP convention of setting per_page to '-1' to get everything. Not necessarily relevant here but probably what you wanted instead of 99999 – mozboz Jul 27 at 9:19
  • @mozboz: thank you for -1 tips. Will keep that in mind for next time. :) – phingoc Jul 27 at 14:09

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.