60, // in seconds
'display' => __( 'Every Minute' ),
);
return $schedules;
}
// Unless an event is already scheduled, create one.
add_action( 'init', 'aitrillion_data_sync_cron' );
function aitrillion_data_sync_cron() {
if ( ! wp_next_scheduled( 'aitrillion_data_sync_schedule' )) {
// wp_schedule_event( time(), 'every_minute', 'aitrillion_data_sync_schedule' );
wp_schedule_single_event( time() + 60, 'aitrillion_data_sync_schedule' );
}
else{
$host = parse_url( get_site_url(), PHP_URL_HOST );
// added code for thornado when cron is not working
if($host == "thornado.com.au"){
$nextTime = get_option( 'aitrillion_data_sync_schedule_time' );
if(!$nextTime || $nextTime < time()){
if($nextTime){
aitrillion_api_log('aitrillion_data_sync_cron called '.$nextTime.' < '.time()." ".PHP_EOL);
}
update_option( 'aitrillion_data_sync_schedule_time', time() + 60 * 5 );
aitrillion_data_sync_action();
}
// sync_new_orders();
}
}
}
// call sync function on cron action
add_action( 'aitrillion_data_sync_schedule', 'aitrillion_data_sync_action' );
/**
* cron data sync function, execute on each cron call
*
*/
function aitrillion_data_sync_action() {
sync_new_customers();
sync_updated_customers();
sync_deleted_customers();
sync_new_categories();
sync_updated_categories();
sync_deleted_categories();
sync_new_products();
sync_updated_products();
sync_deleted_products();
sync_new_orders();
sync_updated_orders();
sync_deleted_orders();
sync_shop_update();
}
/**
* sync new customers
*
*/
function sync_new_customers(){
// get ids of new customer registered since last cron call
$users = get_option( '_aitrillion_created_users' );
aitrillion_api_log('new user sync log '.print_r($users, true).PHP_EOL);
// variable to store failed sync users id
$failed_sync_users = array();
if(empty($users)){
aitrillion_api_log('getting users which are not synced '.PHP_EOL);
// wp_cache_flush();
aitrillion_api_log('sync_new_customers line '.__LINE__.PHP_EOL);
$usersNotSynced = get_users(array(
'meta_key' => '_aitrillion_user_sync',
'meta_value' => 'false'
));
aitrillion_api_log('sync_new_customers line '.__LINE__.PHP_EOL);
if(!empty($usersNotSynced) && is_array($usersNotSynced)){
aitrillion_api_log('not synced user count '.__LINE__." ".count($usersNotSynced).PHP_EOL);
$users = array();
$i = 0;
foreach($usersNotSynced as $user){
if(!empty($user) && is_object($user)){
$users[] = $user->id;
//aitrillion_api_log('not synced user id '.__LINE__." ".$user->id."-".get_post_meta( $user->id, '_aitrillion_user_sync', 'true' ).PHP_EOL);
$i++;
if($i==50){
break;
}
}
else{
//aitrillion_api_log('in else not synced user id '.__LINE__." ".$user."-".get_post_meta( $user, '_aitrillion_user_sync', 'true' ).PHP_EOL);
if(is_numeric($user)){
$users[] = $user;
}
}
}
}
}
if(!empty($users)){
// remove duplicate ids
$users = array_unique($users);
$synced_users = array();
foreach($users as $user_id){
aitrillion_api_log('user id '.$user_id.PHP_EOL);
$c = array();
// get customer data from common function
$c = aitrilltion_get_customer( $user_id );
if(empty($c)){
update_user_meta($user_id, '_aitrillion_user_sync', 'true');
removeValueFromSession($user_id,'_aitrillion_created_users');
continue;
}
$json_payload = json_encode($c);
$_aitrillion_api_key = get_option( '_aitrillion_api_key' );
$_aitrillion_api_password = get_option( '_aitrillion_api_password' );
$bearer = base64_encode( $_aitrillion_api_key.':'.$_aitrillion_api_password );
$url = AITRILLION_END_POINT.'customers/create';
$response = wp_remote_post( $url, array(
'headers' => array(
'Authorization' => 'Basic ' . base64_encode( $_aitrillion_api_key.':'.$_aitrillion_api_password )
),
'body' => $json_payload
));
$r = json_decode($response['body']);
// if sync failed, store failed user into separate variable
if(!isset($r->status) && $r->status != 'success'){
$failed_sync_users_data[$user_id][] = array('user_id' => $user_id, 'error' => $r->message, 'date' => date('Y-m-d H:i:s'));
$failed_sync_users[] = $user_id;
}else{
// flag this user as synced successfully
update_user_meta($user_id, '_aitrillion_user_sync', 'true');
$synced_users[] = $user_id;
removeValueFromSession($user_id,'_aitrillion_created_users');
}
aitrillion_api_log('New customers API Response for user id: '.$user_id.PHP_EOL.print_r($r, true));
}
if(!empty($failed_sync_users)){
// if there are failed sync users, add them into next cron queue
update_option('_aitrillion_created_users', $failed_sync_users, false);
update_option('_aitrillion_failed_sync_users', $failed_sync_users_data, false);
}else{
// all user synced successfully, clear queue
delete_option('_aitrillion_created_users');
}
}
}
/**
* sync modified customers
*
*/
function sync_updated_customers(){
// get ids of modified customers since last cron call
$users = get_option( '_aitrillion_updated_users' );
aitrillion_api_log('udpated users sync log '.print_r($users, true).PHP_EOL);
$failed_sync_users = array();
if(!empty($users)){
// remove duplicate ids
$users = array_unique($users);
$synced_users = array();
foreach($users as $user_id){
$c = array();
// get customer data from common function
$c = aitrilltion_get_customer( $user_id );
if(empty($c)){
update_user_meta($user_id, '_aitrillion_user_sync', 'true');
removeValueFromSession($user_id,'_aitrillion_updated_users');
continue;
}
//aitrillion_api_log('customer '.print_r($c, true).PHP_EOL);
$json_payload = json_encode($c);
$_aitrillion_api_key = get_option( '_aitrillion_api_key' );
$_aitrillion_api_password = get_option( '_aitrillion_api_password' );
$bearer = base64_encode( $_aitrillion_api_key.':'.$_aitrillion_api_password );
$_aitrillion_api_key = get_option( '_aitrillion_api_key' );
$_aitrillion_api_password = get_option( '_aitrillion_api_password' );
$url = AITRILLION_END_POINT.'customers/update';
$response = wp_remote_post( $url, array(
'headers' => array(
'Authorization' => 'Basic ' . base64_encode( $_aitrillion_api_key.':'.$_aitrillion_api_password )
),
'body' => $json_payload
));
$r = json_decode($response['body']);
// if sync failed, store failed user into separate variable
if(!isset($r->status) && $r->status != 'success'){
$failed_sync_users_data[$user_id][] = array('user_id' => $user_id, 'error' => $r->message, 'date' => date('Y-m-d H:i:s'));
$failed_sync_users[] = $user_id;
}else{
// flag this user as synced successfully
update_user_meta($user_id, '_aitrillion_user_sync', 'true');
$synced_users[] = $user_id;
removeValueFromSession($user_id,'_aitrillion_updated_users');
}
aitrillion_api_log('Update customers API Response for user id: '.$user_id.PHP_EOL.print_r($r, true));
}
}
if(!empty($failed_sync_users)){
// if there are failed sync users, add them into next cron queue
update_option('_aitrillion_updated_users', $failed_sync_users, false);
update_option('_aitrillion_failed_sync_users', $failed_sync_users_data, false);
}else{
// all user synced successfully, clear queue
delete_option('_aitrillion_updated_users');
}
}
/**
* sync deleted customers
*
*/
function sync_deleted_customers(){
// get ids of deleted customers since last cron call
$deleted_users = get_option( '_aitrillion_deleted_users' );
aitrillion_api_log('deleted users sync log: '.print_r($deleted_users, true).PHP_EOL);
$failed_sync_users = array();
if(!empty($deleted_users)){
// remove duplicate ids
$deleted_users = array_unique($deleted_users);
foreach($deleted_users as $k => $user_id){
//aitrillion_api_log('user delete id: '.$user_id.PHP_EOL);
$json_payload = json_encode(array('id' => $user_id));
$_aitrillion_api_key = get_option( '_aitrillion_api_key' );
$_aitrillion_api_password = get_option( '_aitrillion_api_password' );
$bearer = base64_encode( $_aitrillion_api_key.':'.$_aitrillion_api_password );
$_aitrillion_api_key = get_option( '_aitrillion_api_key' );
$_aitrillion_api_password = get_option( '_aitrillion_api_password' );
$url = AITRILLION_END_POINT.'customers/delete';
$response = wp_remote_post( $url, array(
'headers' => array(
'Authorization' => 'Basic ' . base64_encode( $_aitrillion_api_key.':'.$_aitrillion_api_password )
),
'body' => $json_payload
));
$r = json_decode($response['body']);
// if sync failed, store failed user into separate variable
if(!isset($r->status) && $r->status != 'success'){
$failed_sync_users_data[$user_id][] = array('user_id' => $user_id, 'error' => $r->message, 'date' => date('Y-m-d H:i:s'));
$failed_sync_users[] = $user_id;
}
else{
removeValueFromSession($user_id,'_aitrillion_deleted_users');
}
aitrillion_api_log('Delete customer API Response for user id: '.$user_id.PHP_EOL.print_r($r, true));
}
if(!empty($failed_sync_users)){
// if there are failed sync users, add them into next cron queue
update_option('_aitrillion_deleted_users', $failed_sync_users, false);
update_option('_aitrillion_failed_sync_users', $failed_sync_users_data, false);
}else{
// all user synced successfully, clear queue
delete_option('_aitrillion_deleted_users');
}
}
}
/**
* sync new products
*
*/
function sync_new_products(){
// get ids of new products created since last cron call
$products = get_option( '_aitrillion_created_products' );
aitrillion_api_log('new product sync log '.print_r($products, true).PHP_EOL);
// variable to store failed sync product id
$failed_sync_products = array();
if(empty($products)){
wp_cache_flush();
$products = array();
$params = array(
'post_type' => 'product',
'posts_per_page' => -1, // Get all matching products
'fields' => 'ids', // Optimize: return only IDs
'meta_query' => array(
array(
'key' => '_aitrillion_product_sync',
'value' => 'false',
'compare' => '='
)
)
);
$wc_query = new WP_Query($params);
// Get the IDs directly
$product_ids = $wc_query->posts;
$i = 0;
foreach ( $product_ids as $product_id ) {
$products[] = $product_id;
$i++;
if($i==300){
break;
}
}
if(count($products) > 0){
aitrillion_api_log('syncing unsynced products '.PHP_EOL);
}
}
if(!empty($products)){
// remove duplicate ids
$products = array_unique($products);
foreach($products as $product_id){
$p = array();
$product = wc_get_product( $product_id );
$p = aitrillion_get_product( $product );
if(empty($p)){
update_post_meta($product_id, '_aitrillion_product_sync', 'true');
removeValueFromSession($product_id,'_aitrillion_created_products');
continue;
}
//aitrillion_api_log('Product: '.$p.PHP_EOL);
$json_payload = json_encode($p);
$_aitrillion_api_key = get_option( '_aitrillion_api_key' );
$_aitrillion_api_password = get_option( '_aitrillion_api_password' );
$bearer = base64_encode( $_aitrillion_api_key.':'.$_aitrillion_api_password );
$_aitrillion_api_key = get_option( '_aitrillion_api_key' );
$_aitrillion_api_password = get_option( '_aitrillion_api_password' );
$url = AITRILLION_END_POINT.'products/create';
$response = wp_remote_post( $url, array(
'headers' => array(
'Authorization' => 'Basic ' . base64_encode( $_aitrillion_api_key.':'.$_aitrillion_api_password )
),
'body' => $json_payload
));
$r = json_decode($response['body']);
// if sync failed, store failed product into separate variable
if(!isset($r->status) && $r->status != 'success'){
$failed_sync_products_data[$product_id][] = array('product_id' => $product_id, 'error' => $r->message, 'date' => date('Y-m-d H:i:s'));
$failed_sync_products[] = $product_id;
}else{
// flag this product as synced successfully
update_post_meta($product_id, '_aitrillion_product_sync', 'true');
$synced_products[] = $product_id;
removeValueFromSession($product_id,'_aitrillion_created_products');
}
aitrillion_api_log('Add product API Response for product id: '.$product_id.PHP_EOL.print_r($r, true));
}
if(!empty($failed_sync_products)){
// if there are failed sync products, add them into next cron queue
update_option('_aitrillion_created_products', $failed_sync_products);
update_option('_aitrillion_failed_sync_products', $failed_sync_products_data);
}else{
// all products synced successfully, clear queue
delete_option('_aitrillion_created_products');
}
}
}
/**
* sync modified products
*
*/
function sync_updated_products(){
// get ids of modified products created since last cron call
$products = get_option( '_aitrillion_updated_products' );
aitrillion_api_log('updated product sync log '.PHP_EOL);
// variable to store failed sync product id
$failed_sync_products = array();
if(!empty($products)){
// remove duplicate ids
$products = array_unique($products);
aitrillion_api_log('unique products: '.print_r($products, true).PHP_EOL);
foreach($products as $product_id){
aitrillion_api_log('Product id: '.$product_id.PHP_EOL);
$p = array();
$product = wc_get_product( $product_id );
if(empty($product)){
aitrillion_api_log('Product not found id: '.$product_id.", line - ".__LINE__.PHP_EOL);
update_post_meta($product_id, '_aitrillion_product_sync', 'true');
removeValueFromSession($product_id,'_aitrillion_updated_products');
continue;
}
$product_status = $product->get_status();
if($product_status != 'publish'){
continue;
}
$p = aitrillion_get_product( $product );
if(empty($p)){
aitrillion_api_log('Product not found id: '.$product_id.", line - ".__LINE__.PHP_EOL);
update_post_meta($product_id, '_aitrillion_product_sync', 'true');
removeValueFromSession($product_id,'_aitrillion_updated_products');
continue;
}
$json_payload = json_encode($p);
$_aitrillion_api_key = get_option( '_aitrillion_api_key' );
$_aitrillion_api_password = get_option( '_aitrillion_api_password' );
$bearer = base64_encode( $_aitrillion_api_key.':'.$_aitrillion_api_password );
$_aitrillion_api_key = get_option( '_aitrillion_api_key' );
$_aitrillion_api_password = get_option( '_aitrillion_api_password' );
$url = AITRILLION_END_POINT.'products/update';
$response = wp_remote_post( $url, array(
'headers' => array(
'Authorization' => 'Basic ' . base64_encode( $_aitrillion_api_key.':'.$_aitrillion_api_password )
),
'body' => $json_payload
));
$r = json_decode($response['body']);
// if sync failed, store failed product into separate variable
if(!isset($r->status) && $r->status != 'success'){
$failed_sync_products_data[$product_id][] = array('product_id' => $product_id, 'error' => $r->message, 'date' => date('Y-m-d H:i:s'));
$failed_sync_products[] = $product_id;
}else{
// flag this product as synced successfully
update_post_meta($product_id, '_aitrillion_product_sync', 'true');
$synced_products[] = $product_id;
removeValueFromSession($product_id,'_aitrillion_updated_products');
}
aitrillion_api_log('Update product API Response for product id: '.$product_id.PHP_EOL.print_r($r, true));
}
if(!empty($failed_sync_products)){
// if there are failed sync products, add them into next cron queue
update_option('_aitrillion_updated_products', $failed_sync_products);
update_option('_aitrillion_failed_sync_products', $failed_sync_products_data);
}else{
// all products synced successfully, clear queue
delete_option('_aitrillion_updated_products');
}
}
}
/**
* sync deleted products
*
*/
function sync_deleted_products(){
// get ids of deleted products since last cron call
$deleted_products = get_option( '_aitrillion_deleted_products' );
aitrillion_api_log('deleted product sync log: '.PHP_EOL);
// variable to store failed sync product id
$failed_sync_products = array();
if(!empty($deleted_products)){
// remove duplicate ids
$deleted_products = array_unique($deleted_products);
aitrillion_api_log('unique deleted product ids : '.print_r($deleted_products, true).PHP_EOL);
foreach($deleted_products as $k => $post_id){
if( get_post_type( $post_id ) != 'product' ){
removeValueFromSession($post_id,'_aitrillion_deleted_products');
aitrillion_api_log('post type is not product type : '.$post_id.PHP_EOL);
continue;
}
$json_payload = json_encode(array('id' => $post_id));
$_aitrillion_api_key = get_option( '_aitrillion_api_key' );
$_aitrillion_api_password = get_option( '_aitrillion_api_password' );
$bearer = base64_encode( $_aitrillion_api_key.':'.$_aitrillion_api_password );
$_aitrillion_api_key = get_option( '_aitrillion_api_key' );
$_aitrillion_api_password = get_option( '_aitrillion_api_password' );
$url = AITRILLION_END_POINT.'products/delete';
$response = wp_remote_post( $url, array(
'headers' => array(
'Authorization' => 'Basic ' . base64_encode( $_aitrillion_api_key.':'.$_aitrillion_api_password )
),
'body' => $json_payload
));
$r = json_decode($response['body']);
// if sync failed, store failed product into separate variable
if(!isset($r->status) && $r->status != 'success'){
$failed_sync_products_data[$product_id][] = array('product_id' => $post_id, 'error' => $r->message, 'date' => date('Y-m-d H:i:s'));
$failed_sync_products[] = $post_id;
}
else{
removeValueFromSession($product_id,'_aitrillion_deleted_products');
}
aitrillion_api_log('Product Delete: product id: '.$post_id.PHP_EOL.print_r($r, true));
}
if(!empty($failed_sync_products)){
// if there are failed sync products, add them into next cron queue
update_option('_aitrillion_deleted_products', $failed_sync_products, false);
update_option('_aitrillion_failed_sync_products', $failed_sync_products_data, false);
}else{
// all user synced successfully, clear queue
delete_option('_aitrillion_deleted_products');
}
aitrillion_api_log('after product delete, delete option : '.PHP_EOL);
}
}
/**
* sync new categories
*
*/
function sync_new_categories(){
// get ids of new categories since last cron call
$categories = get_option( '_aitrillion_created_categories' );
aitrillion_api_log('new category sync log '.print_r($categories, true).PHP_EOL);
$failed_sync_categories = array();
if(!empty($categories)){
// remove duplicate ids
$categories = array_unique($categories);
$synced_categories = array();
foreach($categories as $category_id){
//echo '
category_id: '.$category_id;
$c = array();
$taxonomy = 'product_cat';
$orderby = 'name';
$show_count = 0; // 1 for yes, 0 for no
$pad_counts = 0; // 1 for yes, 0 for no
$hierarchical = 1; // 1 for yes, 0 for no
$title = '';
$empty = 0;
$args = array(
'taxonomy' => $taxonomy,
'orderby' => $orderby,
'show_count' => $show_count,
'pad_counts' => $pad_counts,
'hierarchical' => $hierarchical,
'title_li' => $title,
'hide_empty' => $empty,
'term_taxonomy_id' => $category_id
);
$category = get_categories( $args );
//echo '
'; print_r($category[0]); echo ''; if(!$category[0]){ update_term_meta($category_id, '_aitrillion_category_sync', 'true'); removeValueFromSession($category_id,'_aitrillion_created_categories'); continue; } $c = aitrilltion_get_category( $category[0] ); if(empty($c)){ update_term_meta($category_id, '_aitrillion_category_sync', 'true'); removeValueFromSession($category_id,'_aitrillion_created_categories'); continue; } //aitrillion_api_log('category '.print_r($c, true).PHP_EOL); $json_payload = json_encode($c); $_aitrillion_api_key = get_option( '_aitrillion_api_key' ); $_aitrillion_api_password = get_option( '_aitrillion_api_password' ); $bearer = base64_encode( $_aitrillion_api_key.':'.$_aitrillion_api_password ); $url = AITRILLION_END_POINT.'collections/create'; $response = wp_remote_post( $url, array( 'headers' => array( 'Authorization' => 'Basic ' . base64_encode( $_aitrillion_api_key.':'.$_aitrillion_api_password ) ), 'body' => $json_payload )); $r = json_decode($response['body']); if(!isset($r->status) && $r->status != 'success'){ // if sync failed, store failed categories into separate variable $failed_sync_category_data[$category_id][] = array('category_id' => $category_id, 'error' => $r->message, 'date' => date('Y-m-d H:i:s')); $failed_sync_categories[] = $category_id; }else{ // flag this category as synced successfully update_term_meta($category_id, '_aitrillion_category_sync', 'true'); removeValueFromSession($category_id,'_aitrillion_created_categories'); $synced_categories[] = $category_id; } aitrillion_api_log('Add category API Response for category id: '.$category_id.PHP_EOL.print_r($r, true)); } if(!empty($failed_sync_categories)){ // if there are failed sync category, add them into next cron queue update_option('_aitrillion_created_categories', $failed_sync_categories, false); update_option('_aitrillion_failed_sync_categories', $failed_sync_category_data, false); }else{ // all categories synced successfully, clear queue delete_option('_aitrillion_created_categories'); } } } /** * sync modified categories * */ function sync_updated_categories(){ // get ids of modified categories since last cron call $categories = get_option( '_aitrillion_updated_categories' ); aitrillion_api_log('update category sync log '.print_r($categories, true).PHP_EOL); $failed_sync_categories = array(); if(!empty($categories)){ // remove duplicate ids $categories = array_unique($categories); $synced_categories = array(); foreach($categories as $category_id){ //echo '
'; print_r($category[0]); echo ''; if(!$category[0]){ update_term_meta($category_id, '_aitrillion_category_sync', 'true'); removeValueFromSession($category_id,'_aitrillion_updated_categories'); continue; } $c = aitrilltion_get_category( $category[0] ); if(empty($c)){ update_term_meta($category_id, '_aitrillion_category_sync', 'true'); removeValueFromSession($category_id,'_aitrillion_updated_categories'); continue; } //aitrillion_api_log('category '.print_r($c, true).PHP_EOL); $json_payload = json_encode($c); $_aitrillion_api_key = get_option( '_aitrillion_api_key' ); $_aitrillion_api_password = get_option( '_aitrillion_api_password' ); $bearer = base64_encode( $_aitrillion_api_key.':'.$_aitrillion_api_password ); $url = AITRILLION_END_POINT.'collections/update'; $response = wp_remote_post( $url, array( 'headers' => array( 'Authorization' => 'Basic ' . base64_encode( $_aitrillion_api_key.':'.$_aitrillion_api_password ) ), 'body' => $json_payload )); $r = json_decode($response['body']); if(!isset($r->status) && $r->status != 'success'){ // if sync failed, store failed categories into separate variable $failed_sync_category_data[$category_id][] = array('category_id' => $category_id, 'error' => $r->message, 'date' => date('Y-m-d H:i:s')); $failed_sync_categories[] = $category_id; }else{ // flag this category as synced successfully update_term_meta($category_id, '_aitrillion_category_sync', 'true'); removeValueFromSession($category_id,'_aitrillion_updated_categories'); $synced_categories[] = $category_id; } aitrillion_api_log('Update category API Response for category id: '.$category_id.PHP_EOL.print_r($r, true)); } if(!empty($failed_sync_categories)){ // if there are failed sync category, add them into next cron queue update_option('_aitrillion_updated_categories', $failed_sync_categories); update_option('_aitrillion_failed_sync_categories', $failed_sync_category_data); }else{ // all categories synced successfully, clear queue delete_option('_aitrillion_updated_categories'); } } } /** * sync deleted categories * */ function sync_deleted_categories(){ // get ids of deleted categories since last cron call $deleted_categories = get_option( '_aitrillion_deleted_categories' ); aitrillion_api_log('deleted categories sync log: '.print_r($deleted_categories, true).PHP_EOL); $failed_sync_categories = array(); if(!empty($deleted_categories)){ $deleted_categories = array_unique($deleted_categories); foreach($deleted_categories as $k => $category_id){ //aitrillion_api_log('user delete id: '.$user_id.PHP_EOL); $json_payload = json_encode(array('id' => $category_id)); $_aitrillion_api_key = get_option( '_aitrillion_api_key' ); $_aitrillion_api_password = get_option( '_aitrillion_api_password' ); $bearer = base64_encode( $_aitrillion_api_key.':'.$_aitrillion_api_password ); $_aitrillion_api_key = get_option( '_aitrillion_api_key' ); $_aitrillion_api_password = get_option( '_aitrillion_api_password' ); $url = AITRILLION_END_POINT.'collections/delete'; $response = wp_remote_post( $url, array( 'headers' => array( 'Authorization' => 'Basic ' . base64_encode( $_aitrillion_api_key.':'.$_aitrillion_api_password ) ), 'body' => $json_payload )); $r = json_decode($response['body']); if(!isset($r->status) && $r->status != 'success'){ // if sync failed, store failed categories into separate variable $failed_sync_category_data[$category_id][] = array('category_id' => $category_id, 'error' => $r->message, 'date' => date('Y-m-d H:i:s')); $failed_sync_categories[] = $category_id; } else{ removeValueFromSession($category_id,'_aitrillion_deleted_categories'); } aitrillion_api_log('API Response for category id: '.$category_id.PHP_EOL.print_r($r, true)); } if(!empty($failed_sync_categories)){ // if there are failed sync category, add them into next cron queue update_option('_aitrillion_deleted_categories', $failed_sync_categories, false); update_option('_aitrillion_failed_sync_categories', $failed_sync_category_data, false); }else{ // all user synced successfully, clear queue delete_option('_aitrillion_deleted_categories'); } } } /** * sync new orders * */ function sync_new_orders(){ // get ids of new order created since last cron call $orders = get_option( '_aitrillion_created_orders' ); aitrillion_api_log('new order sync log '.PHP_EOL); // variable to store failed sync order id $failed_sync_order = array(); if(!$orders){ wp_cache_flush(); $ordersNotSynced = wc_get_orders( array( 'limit' => -1, // Query all orders 'meta_key' => '_aitrillion_order_sync', 'meta_value' => 'false', 'meta_compare' => '=', // The comparison argument )); if($ordersNotSynced && is_array($ordersNotSynced)){ aitrillion_api_log('not synced order count '.__LINE__." ".count($ordersNotSynced).PHP_EOL); $orders = array(); $i = 0; foreach($ordersNotSynced as $order){ if(is_object($order)){ $orders[] = $order->id; aitrillion_api_log('not synced order id '.__LINE__." ".$order->id."-".get_post_meta( $order->id, '_aitrillion_order_sync', true ).PHP_EOL); $i++; if($i==300){ break; } } } } } if(!empty($orders)){ // remove duplicate ids $orders = array_unique($orders); aitrillion_api_log('unique order ids count - '.count($orders).", ids - ".print_r($orders, true).PHP_EOL); foreach($orders as $order_id){ $order = wc_get_order( $order_id ); if(empty($order) || !is_object($order)){ aitrillion_api_log('invalid order - '.$order_id.PHP_EOL); delete_post_meta($order_id, '_aitrillion_order_sync'); update_post_meta($order_id, '_aitrillion_order_sync', 'true'); removeValueFromSession($order_id,'_aitrillion_created_orders'); continue; } $o = array(); $o = aitrillion_get_order($order); if(empty($o)){ aitrillion_api_log('invalid order - '.$order_id.PHP_EOL); delete_post_meta($order_id, '_aitrillion_order_sync'); update_post_meta($order_id, '_aitrillion_order_sync', 'true'); removeValueFromSession($order_id,'_aitrillion_created_orders'); continue; } $json_payload = json_encode($o); $_aitrillion_api_key = get_option( '_aitrillion_api_key' ); $_aitrillion_api_password = get_option( '_aitrillion_api_password' ); $bearer = base64_encode( $_aitrillion_api_key.':'.$_aitrillion_api_password ); $_aitrillion_api_key = get_option( '_aitrillion_api_key' ); $_aitrillion_api_password = get_option( '_aitrillion_api_password' ); $url = AITRILLION_END_POINT.'orders/create'; $response = wp_remote_post( $url, array( 'headers' => array( 'Authorization' => 'Basic ' . base64_encode( $_aitrillion_api_key.':'.$_aitrillion_api_password ) ), 'body' => $json_payload )); $r = json_decode($response['body']); // if sync failed, store failed order into separate variable if(!isset($r->status) && $r->status != 'success'){ $failed_sync_orders_data[$order_id][] = array('order_id' => $order_id, 'error' => $r->message, 'date' => date('Y-m-d H:i:s')); $failed_sync_order[] = $order_id; }else{ // flag this order as synced successfully delete_post_meta($order_id, '_aitrillion_order_sync'); update_post_meta($order_id, '_aitrillion_order_sync', 'true'); $synced_orders[] = $order_id; removeValueFromSession($order_id,'_aitrillion_created_orders'); } aitrillion_api_log('API Response for order id: '.$order_id.PHP_EOL.print_r($r, true)); } if(!empty($failed_sync_order)){ // if there are failed sync orders, add them into next cron queue update_option('_aitrillion_created_orders', $failed_sync_order, false); update_option('_aitrillion_failed_sync_orders', $failed_sync_orders_data, false); }else{ // all order synced successfully, clear queue delete_option('_aitrillion_created_orders'); delete_post_meta($order_id, '_aitrillion_order_sync'); update_post_meta($order_id, '_aitrillion_order_sync', 'true'); } } } /** * sync modified orders * */ function sync_updated_orders(){ // get ids of modified orders created since last cron call $orders = get_option( '_aitrillion_updated_orders' ); aitrillion_api_log('updated order sync log '.PHP_EOL); // variable to store failed sync order id $failed_sync_order = array(); if(!empty($orders)){ // remove duplicate ids $orders = array_unique($orders); aitrillion_api_log('updated unique order ids log count - '.count($orders).', orders - '.print_r($orders, true).PHP_EOL); foreach($orders as $order_id){ $order = wc_get_order( $order_id ); if(empty($order) || !is_object($order)){ update_post_meta($order_id, '_aitrillion_order_sync', 'true'); removeValueFromSession($order_id,'_aitrillion_updated_orders'); continue; } $o = aitrillion_get_order($order); if(empty($o)){ update_post_meta($order_id, '_aitrillion_order_sync', 'true'); removeValueFromSession($order_id,'_aitrillion_updated_orders'); continue; } $json_payload = json_encode($o); $_aitrillion_api_key = get_option( '_aitrillion_api_key' ); $_aitrillion_api_password = get_option( '_aitrillion_api_password' ); $bearer = base64_encode( $_aitrillion_api_key.':'.$_aitrillion_api_password ); $_aitrillion_api_key = get_option( '_aitrillion_api_key' ); $_aitrillion_api_password = get_option( '_aitrillion_api_password' ); $url = AITRILLION_END_POINT.'orders/update'; $response = wp_remote_post( $url, array( 'headers' => array( 'Authorization' => 'Basic ' . base64_encode( $_aitrillion_api_key.':'.$_aitrillion_api_password ) ), 'body' => $json_payload )); $r = json_decode($response['body']); // if sync failed, store failed order into separate variable if(!isset($r->status) && $r->status != 'success'){ $failed_sync_orders_data[$order_id][] = array('order_id' => $order_id, 'error' => $r->message, 'date' => date('Y-m-d H:i:s')); $failed_sync_order[] = $order_id; }else{ // flag this order as synced successfully update_post_meta($order_id, '_aitrillion_order_sync', 'true'); $synced_orders[] = $order_id; removeValueFromSession($order_id,'_aitrillion_updated_orders'); } aitrillion_api_log('order updated: order id '.$order_id.PHP_EOL.print_r($r, true)); } if(!empty($failed_sync_order)){ // if there are failed sync orders, add them into next cron queue update_option('_aitrillion_updated_orders', $failed_sync_order, false); update_option('_aitrillion_failed_sync_orders', $failed_sync_orders_data, false); }else{ // all order synced successfully, clear queue delete_option('_aitrillion_updated_orders'); } } } function removeValueFromSession($id, $sessionName){ $valueArray = array(); $valueArray[] = $id; $existingArray = get_option($sessionName); if(!empty($existingArray)){ $existingArray = array_unique($existingArray); } else{ $existingArray = array(); } $arrDiff = array_diff($existingArray, $valueArray); update_option($sessionName, $arrDiff, false); } /** * sync deleted orders * */ function sync_deleted_orders(){ // get ids of deleted orders created since last cron call $deleted_orders = get_option( '_aitrillion_deleted_orders' ); aitrillion_api_log('deleted order sync log: '.print_r($deleted_orders, true).PHP_EOL); if(!empty($deleted_orders)){ // remove duplicate ids $deleted_orders = array_unique($deleted_orders); foreach($deleted_orders as $k => $post_id){ if( get_post_type( $post_id ) != 'shop_order' ){ removeValueFromSession($post_id,'_aitrillion_deleted_orders'); aitrillion_api_log('post type is not order type : '.$post_id.PHP_EOL); continue; } $json_payload = json_encode(array('id' => $post_id)); $_aitrillion_api_key = get_option( '_aitrillion_api_key' ); $_aitrillion_api_password = get_option( '_aitrillion_api_password' ); $bearer = base64_encode( $_aitrillion_api_key.':'.$_aitrillion_api_password ); $_aitrillion_api_key = get_option( '_aitrillion_api_key' ); $_aitrillion_api_password = get_option( '_aitrillion_api_password' ); $url = AITRILLION_END_POINT.'orders/delete'; $response = wp_remote_post( $url, array( 'headers' => array( 'Authorization' => 'Basic ' . base64_encode( $_aitrillion_api_key.':'.$_aitrillion_api_password ) ), 'body' => $json_payload )); $r = json_decode($response['body']); // if sync failed, store failed order into separate variable if(!isset($r->status) && $r->status != 'success'){ $failed_sync_orders_data[$order_id][] = array('order_id' => $post_id, 'error' => $r->message, 'date' => date('Y-m-d H:i:s')); $failed_sync_order[] = $post_id; }else{ removeValueFromSession($post_id,'_aitrillion_deleted_orders'); } aitrillion_api_log('Order Delete: Order id: '.$post_id.PHP_EOL.print_r($r, true)); } if(!empty($failed_sync_order)){ // if there are failed sync orders, add them into next cron queue update_option('_aitrillion_deleted_orders', $failed_sync_order, false); update_option('_aitrillion_failed_sync_order', $failed_sync_orders_data, false); }else{ // all user synced successfully, clear queue delete_option('_aitrillion_deleted_orders'); } } } /** * sync shop detail * */ function sync_shop_update(){ // get flag if shop updated since last cron call $shop_update = get_option( '_aitrillion_shop_updated' ); if($shop_update){ $return['shop_name'] = DOMAIN; $return['shop_type'] = 'woocommerce'; $return['shop_owner'] = ''; $return['status'] = 1; $store_city = get_option( 'woocommerce_store_city' ); $store_postcode = get_option( 'woocommerce_store_postcode' ); // The country/state $store_raw_country = get_option( 'woocommerce_default_country' ); // Split the country/state $split_country = explode( ":", $store_raw_country ); // Country and state separated: $store_country = $split_country[0]; $store_state = $split_country[1]; $return['address1'] = get_option( 'woocommerce_store_address' ); $return['address2'] = get_option( 'woocommerce_store_address_2' ); $return['country'] = $store_country; $return['city'] = $store_city; $return['zip'] = $store_postcode; $return['phone'] = ''; $return['store_name'] = get_bloginfo('name'); $return['email'] = get_bloginfo( 'admin_email' ); $return['shop_currency'] = get_woocommerce_currency(); $return['money_format'] = html_entity_decode(get_woocommerce_currency_symbol()).'{{amount}}'; $json_payload = json_encode($return); $_aitrillion_api_key = get_option( '_aitrillion_api_key' ); $_aitrillion_api_password = get_option( '_aitrillion_api_password' ); $bearer = base64_encode( $_aitrillion_api_key.':'.$_aitrillion_api_password ); $url = AITRILLION_END_POINT.'shops/update'; $response = wp_remote_post( $url, array( 'headers' => array( 'Authorization' => 'Basic ' . base64_encode( $_aitrillion_api_key.':'.$_aitrillion_api_password ) ), 'body' => $json_payload )); $r = json_decode($response['body']); aitrillion_api_log('Store updated: '.PHP_EOL.print_r($return, true).PHP_EOL.print_r($r, true)); update_option('_aitrillion_shop_updated', false); } } ?>