N
N
Natasha0002022-01-25 17:36:34
WordPress
Natasha000, 2022-01-25 17:36:34

How to run export "in the background" after every new order in woocommerce?

There is a site on WordPress, it has WP All Export installed .
Two functions are added by the plugin to one file:
1) launches the export using the woocommerce_thankyou hook (I also tried woocommerce_new_order)
2) launches after the export using the hook - checks for "single launch" (and .. .I did not add this code so as not to clutter up .... it divides the file into parts and sends it via sftp)
Now I have added to both functions (Code for waiting for "garbage")

<?php


function split_orders() {

$lockFile = __FILE__.'.lock';
$hasFile = file_exists($lockFile);
$lockFp = fopen($lockFile, 'w');

// Если блокировку получить не удалось, значит второй скрипт еще работает
if (!flock($lockFp, LOCK_EX | LOCK_NB)) {
    die('Sorry, one more script is running.');
}

// Если файл блокировки уже существовал, но не был залочен,
// значит предыдущий запуск завершился некорректно
if ($hasFile) {
    echo 'The previous running has been completed with an error.';
}

// Все в порядке, блокировка lock получен

// По окончании работы необходимо снять блокировку и удалить файл

register_shutdown_function(function() use ($lockFp, $lockFile) {
    flock($lockFp, LOCK_UN);
    unlink($lockFile);
});

// Код для ожидания "мусор"
$i=1;
for($i=1; $i<6; $i++) {
    $i;
    sleep(10);
}


}


add_action('pmxe_after_export', 'split_orders', 11 );



function start_export_orders() {

// Код для ожидания "мусор"	
$i=1;
for($i=1; $i<6; $i++) {
    $i;
    sleep(10);
}
  
// -- Change these to match your cron URLs
$trigger_url   = "http://example.com/wp-cron.php?import_key=A.y8AICS&import_id=68&action=trigger";
$execution_url = "http://example.com/wp-cron.php?import_key=A.y8AICS&import_id=68&action=processing";

// -- Request trigger URL
$json = json_decode(file_get_contents($trigger_url));

// Example of possible responses from trigger request: 
//    {"status":200,"message":"#68 Cron job triggered."}
//    {"status":403,"message":"Import #68 already triggered. Request skipped."}

if (!is_object($json) || !in_array($json->status,array(200))) {
  die("Error in triggering export <br>".PHP_EOL);
}


$step = 1;
do {
  

  // -- Adjust this as needed (or allowed) by your server	
  set_time_limit(300);   

  // -- Request execution/processing URL
  $json = json_decode(file_get_contents($execution_url));
  
  // Example of possible responses from execution/processing request: 
  //    {"status":200,"message":"Records Processed 10. Records imported 10 of 30."}
  //    {"status":200,"message":"Records Processed 10. Records imported 20 of 30."}
  //    {"status":200,"message":"Import #68 complete"}
  //    {"status":403,"message":"Import #68 is not triggered. Request skipped."}  
  
} while (is_object($json) && $json->status === 200);

}

add_action('woocommerce_thankyou', 'start_export_orders', 10 );


When placing a new order, the page freezes until the entire script is executed...
Help make it run in the background and not bother the user with "endless page loading" as this whole process can take a long time..

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Kirill, 2022-01-25
@init0

Move this script to cron. And it would be nice to transfer cron itself to the system.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question