I'm using a a php function to update quantites, prices etcc on my Wordpress website daily.
here's the function :
<?php
use Automattic\WooCommerce\Client;
function UpdateStock()
{
require __DIR__ . '/vendor/autoload.php';
set_time_limit(10000); //temps limite d'éxecution du code
$filename = "path to csv file on a ftp"; //chemin vers le fichier csv du serveur ftp
$handle = fopen($filename, "r");
while (!feof($handle)) {
$ligne[] = fgetcsv($handle, ',');
}
fclose($handle);
$woocommerce = new Client(
// Your store URL
// Your consumer key
// Your consumer secret
[
'wp_api' => true, // Enable the WP REST API integration
'version' => 'wc/v3', // WooCommerce WP REST API version
'query_string_auth' => true,
'verify_ssl' => false
]
);
for ($item = 1; $item < count($ligne); $item++) {
$cell = $ligne[$item];
if ($cell[13] == "0") { //change la valeur 0 en outofstock
$statue = "outofstock";
} elseif ($cell[13] == "1") {
$statue = "instock";
} elseif ($cell[13] == "backorder") {
$statue = "onbackorder";
} else {
$statue = "outofstock";
}
if ($cell[1] == "simple") { // vérifie si le produit est un produit simple
$data = [ //récupère les données descriptions, stoc_status, short description
"description" => $cell[7],
"name" => $cell[3],
"stock_status" => $statue,
"Short description" => $cell[8],
"regular_price" => $cell[25],
"sale_price" => $cell[24]
];
try {
$woocommerce->put('products/' . $cell[0], $data); // ajoute les données récupérées au produits
} catch (Exception $e) {//si les infos n'ont pas été importés, affiche le message d'erreur
echo 'une erreur est survenue à l article ' . $cell[0] . '</br>';
}
} else {
$data = [
"description" => $cell[7],
"name" => $cell[3],
"stock_status" => $statue,
"regular_price" => $cell[25],
"sale_price" => $cell[24]
];
$path = 'products/' . $cell[43]. '/variations/'.$cell[0];
echo $path;
try {
$woocommerce->put($path, $data);
} catch (Exception $e) {
echo 'une erreur est survenue à l article ' . $cell[0] . '</br>';
}
}
}
}
?>
I'm using PHP Cron status to check if the function is executed but it always end up in a 5 min execution timeout and an 'Incomplete' status on PHP Cron Status. Also, I've set max memory allocated to 2048Mb because I had errors logs about not having enough memory.
Is there any way to get this working or to have more details on errors ? Thanks