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 AutomatticWooCommerceHttpClientHttpClientException: 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): AutomatticWooCommerceHttpClientHttpClient->lookForErrors(Object(stdClass)) #1 /Applications/XAMPP/xamppfiles/htdocs/portland_integrasjon/wc/inc/HttpClient/HttpClient.php(422): AutomatticWooCommerceHttpClientHttpClient->processResponse() #2 /Applications/XAMPP/xamppfiles/htdocs/portland_integrasjon/wc/inc/Client.php(82): AutomatticWooCommerceHttpClientHttpClient->request('products', 'GET', Array, Array) #3 /Applications/XAMPP/xamppfiles/htdocs/portland_integrasjon/wc/wc-procucts-extract.php(8): AutomatticWooCommerceClient->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?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0