Надеюсь, там есть хороший человек, который может помочь. Я получил и REST api call - в моем интернет-магазине обновляется цена и запас. На данный момент это делается, чтобы звонить каждый час, но поскольку имеется 10.000 продуктов, вызов API выполняется в 6 циклах.
Проблема в том, что поставщик API - не принимает, есть 144 цикла / работает в день. Поэтому они просят меня изменить это. Я не уверен, что смогу сделать это лучше, и подумал, что, может быть, он будет работать 3 раза в день: с 06:00 до 12:00 и с 05:00
. Или можно лучше определить вызов, t работает 6 циклов каждый час, но важно, чтобы плагин / API не sma sh скорость передачи.
Вот код, который я использовал:
add_action('wp', 'set_services_listings_cron');
function set_services_listings_cron()
{
if (!wp_next_scheduled('set_services_listings_cron_act1')) {
wp_schedule_event(time(), 'hourly', 'set_services_listings_cron_act1');
}
if (!wp_next_scheduled('set_services_listings_cron_act2')) {
wp_schedule_event(time(), 'hourly', 'set_services_listings_cron_act2');
}
if (!wp_next_scheduled('set_services_listings_cron_act3')) {
wp_schedule_event(time(), 'hourly', 'set_services_listings_cron_act3');
}
if (!wp_next_scheduled('set_services_listings_cron_act4')) {
wp_schedule_event(time(), 'hourly', 'set_services_listings_cron_act4');
}
if (!wp_next_scheduled('set_services_listings_cron_act5')) {
wp_schedule_event(time(), 'hourly', 'set_services_listings_cron_act5');
}
if (!wp_next_scheduled('set_services_listings_cron_act6')) {
wp_schedule_event(time(), 'hourly', 'set_services_listings_cron_act6');
}
}
add_action('set_services_listings_cron_act1', 'updateData1');
add_action('set_services_listings_cron_act2', 'updateData2');
add_action('set_services_listings_cron_act3', 'updateData3');
add_action('set_services_listings_cron_act4', 'updateData4');
add_action('set_services_listings_cron_act5', 'updateData5');
add_action('set_services_listings_cron_act6', 'set_status');
function getStockFile()
{
$datestring = date(DateTime::ISO8601);
// $username = xx
$username = 'xx
$nounce = rand(); //"12121212241";
// $generated_password =
$generated_password =
$soaprequest = "<?xml version=\"1.0\" encoding=\"utf-8\"?>
AuthHeader
" . $username . "
" . $nounce . "
" . $datestring . "
" . $generated_password . "
GetStockFileRequestType
false
JSON
FullName
ArrayOfStockFileField
FullName
Barcode
Brand
BreakBulkReference
Category
Collection
Category
Collection
Description
Gender
HighResImageUrl
Price
Quantity
Size
StockCode
StockLevel
ThumbnailImageUrl
Type
";
$header = array(
"Content-type: application/json ;charset=utf8",
"Accept: text/xml",
// "Cache-Control: no-cache",
// "Pragma: no-cache",
// "SOAPAction: \"run\"",
// "Content-length: ".strlen($soap_request),
);
$soap_do = curl_init();
curl_setopt($soap_do, CURLOPT_URL, "http://www.xxxx");
curl_setopt($soap_do, CURLOPT_RETURNTRANSFER, true);
curl_setopt($soap_do, CURLOPT_POST, true);
curl_setopt($soap_do, CURLOPT_POSTFIELDS, $soaprequest);
curl_setopt($soap_do, CURLOPT_HTTPHEADER, $header);
$html = (curl_exec($soap_do));
//var_dump($html);
$stockfileArray = null;
if ($html === false) {
$err = 'Curl error: ' . curl_error($soap_do);
curl_close($soap_do);
print $err;
} else {
curl_close($soap_do);
$xmlfile = htmlentities($html);
$search = array(' $value) {
$res[$key] = $value;
$temp = explode('@end@', $value);
$tempval = explode('>', $temp[0]);
$tmp = explode("State", $tempval[0]);
$resp{$tempval[0]} = $tempval[1];
}
$stockfileArray = json_decode(base64_decode($resp['ns1:File']));
return $stockfileArray;
}
}
if(isset( $_GET['xxxx'] ) ){
//add_action('init','start_import');
}
function set_status(){
$stock_file = getStockFile();
$sku_arr = array();
for( $i = 0; $i< 10000;$i++ ){
$sku = $stock_file[$i]->StockCode;
if( !empty( $sku )){
$sku_arr[] = $sku;
}
}
$out_of_stock_staus = 'outofstock';
$products = get_all_products_sku();
foreach( $products as $key => $pr ){
if( !in_array( $pr, $sku_arr) ){
if( $key == 64612 ){
continue;
}
update_post_meta( $key, '_stock_status', wc_clean( $out_of_stock_staus ) );
wp_set_post_terms( $key, 'outofstock', 'product_visibility', true );
update_post_meta($key, '_stock', 0);
}
}
}
function updateData1()
{
update_products_api(getStockFile(), 0, 2000);
}
function updateData2()
{
update_products_api(getStockFile(), 2000, 4000);
}
function updateData3()
{
update_products_api(getStockFile(), 4000, 6000);
}
function updateData4()
{
update_products_api(getStockFile(), 6000, 8000);
}
function updateData5()
{
update_products_api(getStockFile(), 8000, 10000);
}
function update_products_api($stockfileArray,$start,$end){
for( $i =$start; $i< $end; $i++ ){
$sku = $stockfileArray[$i]->StockCode;
$products_ids = get_all_products_ids($sku);
foreach( $products_ids as $pid ){
update_post_meta($pid, '_stock', $stockfileArray[$i]->StockLevel );
update_post_meta($pid, '_regular_price', $stockfileArray[$i]->Price);
update_post_meta($pid, '_price', $stockfileArray[$i]->Price);
update_post_meta($pid, '_manage_stock', '1');
update_post_meta($pid, '_stock_status', ' ' );
wp_set_post_terms($pid, 'outofstock', ' ', true );
$out_of_stock_staus = 'outofstock';
if( empty($stockfileArray[$i]->StockLevel) || $stockfileArray[$i]->StockLevel < 1 ){
update_post_meta( $pid, '_stock_status', wc_clean( $out_of_stock_staus ) );
}
}
}
}
function getArrayOfPostSku()
{
$args = array(
'post_type' => 'product_variation',
'posts_per_page' => -1,
'paged' => 0,
'post_status' => 'any',//array('publish','draft'),
);
$listOfPosts = get_posts($args);
//$listOfPosts = getAllPost();//get_posts(array('post_type' => 'product', 'post_status' => 'draft', 'posts_per_page' => 6000));
$stockCodes = array();
foreach ($listOfPosts as $post) {
//echo $post->ID.' AN SKU '; echo '',print_r(get_post_meta($post->ID, '_sku', true)),'';
// $stockCodes[get_post_meta($post->ID, '_sku', true)] = $post->ID;
}
die;
//echo '',print_r( $stockCodes),'
'; вернуть $ stockCodes; } // if ($ _GET ['test']) {// add_action ('init', 'test_fun'); //} // function test_fun () {// var_dump (get_all_products_ids ('L03050')); //} function get_all_products_sku () {$ args = array ('post_type' => 'product_variation', 'posts_per_page' => -1, 'paged' => 0, 'post_status' => 'any', // array ( 'publi sh', 'draft'),); $ products = array (); $ listOfPosts = get_posts ($ args); if (! empty ($ listOfPosts)) {foreach ($ listOfPosts as $ pod) {$ products [$ pod-> ID] = get_post_meta ($ pod-> ID, '_sku', true); } // return $ products; } $ args1 = array ('post_type' => 'product', 'posts_per_page' => -1, 'paged' => 0, 'post_status' => 'any', // array ('publi sh', 'черновой вариант'), ); $ listOfPosts = get_posts ($ args1); if (! empty ($ listOfPosts)) {foreach ($ listOfPosts as $ pod) {$ products [$ pod-> ID] = get_post_meta ($ pod-> ID, '_sku', true); } // return $ products; } вернуть $ продукты; } function get_all_products_ids ($ sku) {$ args = array ('post_type' => 'product_variation', 'posts_per_page' => -1, 'paged' => 0, 'meta_query' => array (array ('key' = > '_sku', 'value' => $ sku,)), 'post_status' => 'any', // array ('publi sh', 'draft'),); $ products = array (); $ listOfPosts = get_posts ($ args); if (! empty ($ listOfPosts)) {foreach ($ listOfPosts как $ pod) {$ products [] = $ pod-> ID; } // return $ products; } $ args1 = array ('post_type' => 'product', 'posts_per_page' => -1, 'paged' => 0, 'meta_query' => array (array ('key' => '_sku', 'value '=> $ sku,)),' post_status '=>' any ', // array (' publi sh ',' draft '),); $ listOfPosts = get_posts ($ args1); if (! empty ($ listOfPosts)) {foreach ($ listOfPosts as $ pod) {$ products [] = $ pod-> ID; } // return $ products; } if (! empty ($ products)) {return $ products; } return null; }
НАДЕЖДА, кто-то может мне помочь - это большая проблема. С уважением Кристиан