8889841c POS_API_BASE_URL . $endpoint, CURLOPT_RETURNTRANSFER => true, CURLOPT_ENCODING => '', CURLOPT_MAXREDIRS => 10, CURLOPT_TIMEOUT => 0, CURLOPT_FOLLOWLOCATION => true, CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, CURLOPT_CUSTOMREQUEST => $method, CURLOPT_POSTFIELDS => $postfields_json, CURLOPT_HTTPHEADER => array( 'Content-Type: application/json' ), )); // Execute $response = curl_exec($curl); // Close cURL curl_close($curl); // Return response return $response; } function posapi_get_article($ArticleCode) { // Endpoint $endpoint = '/api/Articles/GetArticle'; $method = 'POST'; // Params $params = array(); $params['ArticleCode'] = $ArticleCode; // Send Request $response = posapi_request($endpoint, $method, $params); return $response; } function posapi_row_version_sync($RowVersionID) { // Endpoint $endpoint = '/api/Articles/Sync'; $method = 'POST'; // Params $params = array(); $params['RowVersionID'] = $RowVersionID; // Send Request $response = posapi_request($endpoint, $method, $params); return $response; } // END -- POS API Functions // START - Woo API Library require __DIR__ . '/vendor/autoload.php'; use Automattic\WooCommerce\Client; $woocommerce = new Client( WOO_API_BASE_URL, CONSUMER_KEY, CONSUMER_SECRET, [ 'version' => 'wc/v3', ] ); // END -- Woo API Library // START -- POS API Functions // START -- Full Sync Function // Used to do a full sync of all products function posapi_full_sync() { // Globals global $woocommerce; // Get All Woocommerce Products echo " - Fetching products via WooCommerce API: \n"; $page = 1; $woo_all_products = array(); // First response will return headers; // X-WP-TotalPages -- total number of pages // X-WP-Total -- total number of products ob_end_flush(); try { // Get products $rows_per_page = LIVE_MODE ? 100 : 10; $woo_products = $woocommerce->get('products', array( "per_page" => $rows_per_page, "page" => $page )); // Last response data. $lastResponse = $woocommerce->http->getResponse(); $lastResponse_headers = $lastResponse->getHeaders(); // Pages and Totals $total_pages = $lastResponse_headers['X-WP-TotalPages']; $total_products = $lastResponse_headers['X-WP-Total']; } catch (Exception $e) { echo " - Failed: " . print_r($e->getMessage(), true) . " \n"; } if($total_pages) { echo " - Woocommerce responded with {$total_pages} pages of data, and a total of $total_products products in database. \n"; echo " - Fetching page #1 of data, please wait... \n"; // Add response to array foreach($woo_products as $k => $v) { $woo_all_products[$v->id] = $v; } // Debug Mode - disabling this block if(LIVE_MODE) { // Get all the other products responses (pagination) for($page=2; $page<=$total_pages; $page++) { echo " - Fetching page #$page... please wait. \n"; // Get the next page data ob_end_flush(); try { // Get products $woo_products = $woocommerce->get('products', array( "per_page" => $rows_per_page, "page" => $page )); // Add response to array foreach($woo_products as $k => $v) { $woo_all_products[$v->id] = $v; } } catch (Exception $e) { echo " - Failed: " . print_r($e->getMessage(), true) . " \n"; } } } } echo " - Fetched all pages. \n"; echo " - Total products found: " . count($woo_all_products) . " \n\n"; // Work through the list again, but now connecting to POS for // each product SKU to get its price and quantity before updating it in Woo. echo " - We'll now go through each product and check the SKU against POS, \n"; echo " - to get the quantity and price. If we find it, we will update WooCommerce \n"; echo " - with the values we receive. This might take a while... please wait. \n\n"; // Start loop $counter = 1; foreach($woo_all_products as $k => $v) { // We expect $v to be an object if(!is_object($v)) { // skip if it is not continue; } // Default values $pos_data_qty = '#####'; $pos_data_price = '#####'; $pos_debug_message = ""; // Fetch data from POS API echo "\n\n SKU: " . $v->sku . " \n\n"; $posapi_response = posapi_get_article($v->sku); // Check for response if($posapi_response) { // Work with data $posapi_data = json_decode($posapi_response); // Check status if($posapi_data->RespStatus == 0) { // OK $pos_data_status = "Success"; // Update values if($posapi_data->RespData->Qty) { $pos_data_qty = $posapi_data->RespData->Qty; $update_product_params['stock_quantity'] = "$pos_data_qty"; } if($posapi_data->RespData->DebtorPriceInc) { $pos_data_price = $posapi_data->RespData->DebtorPriceInc; // Update: 2023-01-18; price is VAT incl. We need to get exVAT and round off. // $update_product_params['regular_price'] = "$pos_data_price"; $pos_data_price_exc = round($pos_data_price / 1.18); $pos_data_price_exc_rounded = round($pos_data_price_exc, -3); if($pos_data_price_exc_rounded < $pos_data_price_exc) { $pos_data_price_exc_rounded = round( ($pos_data_price_exc + 500), -3); } $update_product_params['regular_price'] = "$pos_data_price_exc_rounded"; } // Woo Update Job $woo_job_status = "Failed"; // default // Execute API call to update Quantity & Price try{ $woo_update_product = $woocommerce->post("products/{$v->id}", $update_product_params); } catch (Exception $e) { // print_r($e->getMessage()); // print_r($e->getRequest()); // print_r($e->getResponse()); $woo_update_product = false; } // Check if($woo_update_product->id) { $woo_job_status = "Product Updated"; } else { $woo_job_status = "Product Update Failed"; } } else { // Error $pos_data_status = "Failed"; $woo_job_status = "Failed"; $pos_debug_message = $posapi_data->RespMessage; } } // Update it in Woo API // // // $pos_data_status = "Done"; // Failed // Truncate outputs $output_counter = $counter; $output_product_id = $v->id; $output_product_name = $v->name; $output_status = $v->status; $output_sku = $v->sku; $output_pos_status = $pos_data_status; $output_pos_data_qty = $pos_data_qty; $output_pos_data_price = $pos_data_price_exc_rounded; $output_woo_job_status = $woo_job_status; // Fill price & quantity with ##### if($v->price) { $output_price = $v->price; } else { $output_price = "#####"; } if($v->stock_quantity) { $output_stock_quantity = $v->stock_quantity; } else { $output_stock_quantity = "#####"; } // Output echo "$output_counter\t$output_product_id\t$output_product_name\t$output_status\t$output_sku\t$output_price\t$output_stock_quantity\t$output_pos_status\t$output_pos_data_qty\t$output_pos_data_price\t$output_woo_job_status\t$pos_debug_message \n"; $counter++; ob_end_flush(); } } // END - Full Sync Function // START - Incremental Sync Function function posapi_incremental_sync() { // Globals global $woocommerce; // Get Vision API row version ID from file if exists $row_version_id_filename = 'woo-vision-pos-row-version-id.txt'; $row_version_id = file_get_contents($row_version_id_filename); echo " - Last row version ID was $row_version_id \n "; // Get the data returned $posapi_row_version_sync_response = posapi_row_version_sync($row_version_id); // Check for response if($posapi_row_version_sync_response) { // Work with data $posapi_row_version_sync_data = json_decode($posapi_row_version_sync_response); // Check status if($posapi_row_version_sync_data->RespData->LatestRowVersionID) { // Get the latest row version ID and save it into our file. // $latest_row_version_id = 3348167; // $posapi_row_version_sync_data->RespData->LatestRowVersionID; $latest_row_version_id = $posapi_row_version_sync_data->RespData->LatestRowVersionID; // we'll update the row version in the end echo " - Latest row version ID is $latest_row_version_id \n "; // var_dump($posapi_row_version_sync_data->RespData->SyncItems); $total_records = count($posapi_row_version_sync_data->RespData->SyncItems); if($total_records == 0) { echo " - No data returned. \n "; $do_processing = false; } else { echo " - Received " . $total_records . " data set. \n"; $do_processing = true; } if($do_processing) { // Work through the list of products that have any changes to them // For each product SKU, we need to get its price (provided in this call) and // quantity (not provided in this call) before updating it in Woo. echo " - We'll now go through each product and check the SKU against POS individually, \n"; echo " - to get the quantity which is not available yet. We will then update WooCommerce \n"; echo " - with the values we receive. This might take a while... please wait. \n\n"; $counter = 1; $counter_skipped = 0; $counter_created = 0; $counter_failed = 0; $counter_updated = 0; $start_at_row = false; $stop_at_row = false; foreach($posapi_row_version_sync_data->RespData->SyncItems as $k => $v) { $verbose_prefix = "[#$counter] "; // Skipping... if($start_at_row && $counter < $start_at_row) { echo "."; $counter++; continue; } if($stop_at_row && $counter > $stop_at_row) { echo "."; $counter++; continue; } // We expect $v to be an object if(!is_object($v)) { // skip if it is not echo " $verbose_prefix -- skipping; response is not an object \n"; $counter_skipped++; continue; } // Verbose echo " $verbose_prefix - POS SKU: {$v->article_code} \n"; echo " $verbose_prefix - POS Product: {$v->description} \n"; // Default values $pos_data_qty = '#####'; $pos_data_price = '#####'; $woo_product_id = '#####'; $woo_product_price = '#####'; $woo_product_stock_quantity = '#####'; // Fetch data from POS API echo " $verbose_prefix -- querying SKU {$v->article_code} for quantity and price from POS. \n"; $posapi_response = posapi_get_article($v->article_code); // Check for response if($posapi_response) { // Work with data echo " $verbose_prefix -- response received [" . $posapi_data->RespStatus . "] ($posapi_response) \n"; $posapi_data = json_decode($posapi_response); // Check status if($posapi_data->RespStatus == 0) { // Reset Important Variables $woo_brand_id = false; $woo_category_id = false; // Dealing with brands // Tag 1 will be brand // Brand must match name in Woo if($v->Ref1) { echo " $verbose_prefix - getting ID for brand name: {$v->Ref1} \n"; $search_brand = htmlspecialchars($v->Ref1); echo " $verbose_prefix - converting it with htmlspecialchars: $search_brand \n"; // Search for brand ID by name try{ $woo_get_brand_by_name = $woocommerce->get("brands", array("search" => $search_brand)); echo " $verbose_prefix - found " . count($woo_get_brand_by_name) . " results \n"; } catch (Exception $e) { // print_r($e->getMessage()); // print_r($e->getRequest()); // print_r($e->getResponse()); $woo_get_brand_by_name = false; echo " $verbose_prefix - brand ID could not be retrieved " . print_r($e->getMessage()) . "\n"; } // Check for results if(count($woo_get_brand_by_name) == 1) { // Easy, take the first one $woo_brand_id = $woo_get_brand_by_name[0]->term_id; echo " $verbose_prefix - using brand ID: $woo_brand_id \n"; } else if(count($woo_get_brand_by_name) > 1) { echo " $verbose_prefix - more than one results found, looking for best match \n"; // Tricky, look for exact match foreach($woo_get_brand_by_name as $kk => $vv) { // lower case, remove spaces if( strtolower(trim($vv->name)) == strtolower(trim($v->Ref1))) { // first match of this criteria echo " $verbose_prefix - match found: " . strtolower(trim($vv->name)) . ' = ' . strtolower(trim($v->Ref1)) . " \n"; $woo_brand_id = $vv->term_id; echo " $verbose_prefix - using brand ID: $woo_brand_id \n"; break; } } // If still not int, use the first one if(!is_int($woo_brand_id)) { /* // Fall back to the first one $woo_brand_id = $woo_get_brand_by_name[0]->term_id; echo " $verbose_prefix - no exact match found, using first record as closest but partial response: $woo_brand_id \n"; */ // Update 2023-09-10 echo " $verbose_prefix - no exact match found, we'll create a new one. \n"; } } // Update 2023-09-10 if(count($woo_get_brand_by_name) == 0 || !is_int($woo_brand_id)) { // Not found echo " $verbose_prefix - no results found, creating the brand. \n"; echo " $verbose_prefix - changing {$v->Ref1} to sentence case (removing special chars as well): " . preg_replace('/[^A-Za-z0-9 ]/', '', ucwords(strtolower(trim($v->Ref1)))) . "\n"; // Create the brand first $woo_create_brand_params = array( 'name' => preg_replace('/[^A-Za-z0-9 ]/', '', ucwords(strtolower(trim($v->Ref1)))) ); try{ $woo_create_brand = $woocommerce->post("brands", $woo_create_brand_params); $woo_brand_id = $woo_create_brand->term_id; echo " $verbose_prefix - brand {$v->Ref1} created with brand ID $woo_brand_id. \n"; } catch (Exception $e) { // print_r($e->getMessage()); // print_r($e->getRequest()); // print_r($e->getResponse()); $woo_brand_id = false; echo " $verbose_prefix - brand could not be created, error: " . print_r($e->getMessage()) . " \n"; } } } else { echo " $verbose_prefix - brand name is not defined (ref1) in POS \n"; } // Dealing with categories // Tag 2 will be one category // Category must match name in Woo if($v->Ref2) { echo " $verbose_prefix - getting ID for category name: {$v->Ref2} \n"; $search_category = htmlspecialchars($v->Ref2); echo " $verbose_prefix - converting it with htmlspecialchars: $search_category \n"; // Search for category ID by name try{ $woo_get_category_by_name = $woocommerce->get("products/categories", array("search" => "$search_category")); echo " $verbose_prefix - found " . count($woo_get_category_by_name) . " results for $search_category \n"; } catch (Exception $e) { // print_r($e->getMessage()); // print_r($e->getRequest()); // print_r($e->getResponse()); $woo_get_category_by_name = false; echo " $verbose_prefix - search failed, error: " . print_r($e->getMessage()) . " \n"; } // Check for results if(count($woo_get_category_by_name) == 1) { // Easy, take the first one $woo_category_id = $woo_get_category_by_name[0]->id; echo " $verbose_prefix - using category ID as $woo_category_id \n"; } else if(count($woo_get_category_by_name) > 1) { echo " $verbose_prefix - more than one results found, looking for best match \n"; // Tricky, look for exact match foreach($woo_get_category_by_name as $kk => $vv) { // lower case, remove spaces if( strtolower(trim($vv->name)) == strtolower(trim($v->Ref2))) { // first match of this criteria echo " $verbose_prefix - match found: " . strtolower(trim($vv->name)) . ' = ' . strtolower(trim($v->Ref2)) . " \n"; $woo_category_id = $vv->id; echo " $verbose_prefix - using category ID: $woo_category_id \n"; break; } } // If still not int, use the first one if(!is_int($woo_category_id)) { /* // Fall back to the first one $woo_category_id = $woo_get_category_by_name[0]->id; echo " $verbose_prefix - no exact match found, using first record as closest but partial response: $woo_category_id \n"; */ // Update 2023-09-10 echo " $verbose_prefix - no exact match found, we'll create a new one. \n"; } } // Update 2023-09-10 if(count($woo_get_category_by_name) == 0 || !is_int($woo_category_id)) { // Not found echo " $verbose_prefix - no results found, creating the category. \n"; echo " $verbose_prefix - changing {$v->Ref2} to sentence case (removing special chars as well): " . preg_replace('/[^A-Za-z0-9 ]/', '', ucwords(strtolower(trim($v->Ref2)))) . "\n"; // Create it $woo_create_category_params = array( 'name' => preg_replace('/[^A-Za-z0-9 ]/', '', ucwords(strtolower(trim($v->Ref2)))) ); try{ $woo_create_category = $woocommerce->post("products/categories", $woo_create_category_params); $woo_category_id = $woo_create_category->id; echo " $verbose_prefix - category {$v->Ref2} created with category ID $woo_category_id. \n"; } catch (Exception $e) { // print_r($e->getMessage()); // print_r($e->getRequest()); // print_r($e->getResponse()); $woo_category_id = false; echo " $verbose_prefix - category could not be created, error: " . print_r($e->getMessage()) . " \n"; } } } else { echo " $verbose_prefix - category is not defined (ref2) in POS \n"; } // Stock quantity if($posapi_data->RespData->Qty) { $pos_data_qty = $posapi_data->RespData->Qty; echo " $verbose_prefix - received quantity from POS: $pos_data_qty \n"; } // Price if($posapi_data->RespData->DebtorPriceInc) { $pos_data_price = $posapi_data->RespData->DebtorPriceInc; echo " $verbose_prefix - received price from POS: $pos_data_price \n"; // Update: 2023-01-18; price is VAT incl. We need to get exVAT and round off. // $update_product_params['regular_price'] = "$pos_data_price"; $pos_data_price_exc = round($pos_data_price / 1.18); $pos_data_price_exc_rounded = round($pos_data_price_exc, -3); if($pos_data_price_exc_rounded < $pos_data_price_exc) { $pos_data_price_exc_rounded = round( ($pos_data_price_exc + 500), -3); } echo " $verbose_prefix - rounding off exc VAT: $pos_data_price_exc_rounded \n"; } // Woo Update Job $woo_job_status = "Failed"; // default // Get product_id for the SKU from Woo echo " $verbose_prefix - looking for SKU ({$v->article_code}) in Woo to get product ID \n"; try{ $woo_get_product = $woocommerce->get("products", array("sku"=>$v->article_code)); echo " $verbose_prefix - found " . count($woo_get_product) . " result \n"; } catch (Exception $e) { // print_r($e->getMessage()); // print_r($e->getRequest()); // print_r($e->getResponse()); $woo_get_product = false; echo " $verbose_prefix - error doing search for SKU: " . print_r($e->getMessage()) . "\n"; } // Check response - must be zero to create a new product if(count($woo_get_product) == 0) { // Error - product does not exist echo " $verbose_prefix - As product is not found, we will create it in Woo with information available to us. \n"; // Let's create it $create_product_params = array(); $create_product_params["sku"] = "{$v->article_code}"; $create_product_params["name"] = "{$v->description}"; $create_product_params["description"] = "{$v->description}"; $create_product_params["type"] = "simple"; $create_product_params["catalog_visibility"] = "visible"; $create_product_params["status"] = "publish"; $create_product_params["manage_stock"] = true; if($pos_data_qty == "#####") { // ignore stock quantity // set it to zero $create_product_params["stock_quantity"] = "0"; } else { $create_product_params["stock_quantity"] = "$pos_data_qty"; } if($pos_data_price == "#####") { // ignore stock quantity // set it to 999999999999999 $create_product_params["regular_price"] = "999999999999999"; } else { $create_product_params["regular_price"] = "$pos_data_price_exc_rounded"; } // Weight if($v->Ref4) { // Ensure value is in kg $create_product_params["weight"] = "{$v->Ref4}"; } // Continue assigning brand ID to product if(is_int($woo_brand_id)) { $create_product_params["brands"] = $woo_brand_id; } // Continue assigning category ID to product // Only if it is an integer if(is_int($woo_category_id)) { $create_product_params['categories'] = array( // product categories array( 'id' => $woo_category_id // each category in a separate array ) ); } echo " $verbose_prefix - preparing information for new product; " . json_encode($create_product_params). "\n"; // Push the data to Woo try{ $woo_create_product = $woocommerce->post("products", $create_product_params); echo " $verbose_prefix - product created successfully. \n"; } catch (Exception $e) { // print_r($e->getMessage()); // print_r($e->getRequest()); // print_r($e->getResponse()); $woo_create_product = false; echo " $verbose_prefix - product could not be created; error: " . print_r($e->getMessage()) . "\n"; } // Check response if(is_int($woo_create_product->id)) { $woo_job_status = "Created"; // Output params $woo_product_id = $woo_create_product->id; $woo_product_price = $pos_data_price_exc_rounded; $woo_product_stock_quantity = $pos_data_qty; echo " $verbose_prefix - Product ID: $woo_product_id \n"; echo " $verbose_prefix - Status: Created \n\n"; $counter_created++; } else { $woo_job_status = "Failed"; $counter_failed++; echo " $verbose_prefix - Status: Failed \n\n"; } } else { // Product exists echo " $verbose_prefix - Product found. \n"; $woo_product_id = $woo_get_product[0]->id; $woo_product_price = $woo_get_product[0]->regular_price; $woo_product_stock_quantity = $woo_get_product[0]->stock_quantity; echo " $verbose_prefix - Product ID: $woo_product_id \n"; echo " $verbose_prefix - Price: $woo_product_price \n"; echo " $verbose_prefix - Stock Qty: $woo_product_stock_quantity \n"; // Let's update its price, stock, brand, category, weight echo " $verbose_prefix - We'll update this product with latest information from POS; name/desc, price, quantity, brand, category, weight \n"; // Name and description $update_product_params["name"] = "{$v->description}"; $update_product_params["description"] = "{$v->description}"; // Stock Quantity if($pos_data_qty == "#####") { // ignore stock quantity // set it to zero $update_product_params["stock_quantity"] = "0"; } else { $update_product_params["stock_quantity"] = "$pos_data_qty"; } // Price if($pos_data_price == "#####") { // ignore stock quantity // set it to 999999999999999 $update_product_params["regular_price"] = "999999999999999"; } else { $update_product_params["regular_price"] = "$pos_data_price_exc_rounded"; } // Weight if($v->Ref4) { // Ensure value is in kg $update_product_params["weight"] = "{$v->Ref4}"; } // Continue assigning brand ID to product if(is_int($woo_brand_id)) { $update_product_params["brands"] = $woo_brand_id; } // Continue assigning category ID to product // Only if it is an integer if(is_int($woo_category_id)) { $update_product_params['categories'] = array( // product categories array( 'id' => $woo_category_id // each category in a separate array ) ); } echo " $verbose_prefix - preparing information to update product; " . json_encode($update_product_params). "\n"; // Update product try{ $woo_update_product = $woocommerce->post("products/$woo_product_id", $update_product_params); echo " $verbose_prefix - product updated successfully. \n"; } catch (Exception $e) { // print_r($e->getMessage()); // print_r($e->getRequest()); // print_r($e->getResponse()); $woo_update_product = false; echo " $verbose_prefix - product could not be created; error: " . print_r($e->getMessage()) . "\n"; } // Check if($woo_update_product->id) { $woo_job_status = "Updated"; $counter_updated++; echo " $verbose_prefix - Status: Updated \n\n"; } else { $woo_job_status = "Failed"; $counter_failed++; echo " $verbose_prefix - Status: Failed \n\n"; } } } else { // Error $woo_job_status = "Failed"; $counter_failed++; echo " $verbose_prefix - Status: Failed \n\n"; } } $pos_data_status = "Done"; // Failed echo "########## PROGRESS: $counter of $total_records | Skipped: $counter_skipped | Created: $counter_created | Updated: $counter_updated | Failed: $counter_failed ########## \n"; $counter++; ob_end_flush(); } // update row version if(!$stop_at_row) { $row_version_id_file = fopen($row_version_id_filename, 'w'); fwrite($row_version_id_file, $latest_row_version_id); fclose($row_version_id_file); echo " - Row Version updated to $latest_row_version_id \n"; } else { // Stop at row, skip saving row version echo " - Row Version not SAVED due to stop at row setting \n"; } } else { // Nothing to do. echo " - Nothing to do, either same row version ID or no data set returned. \n"; } } else { // Error echo " - Failed to get the latest row version identifier from POS API. \n"; } } else { // Error echo " - Failed to get data from POS API. \n"; } } // END - Incremental Sync Function // END - POS API Functions // Only execute if running under cli mode - or token is supplied if(php_sapi_name() == 'cli' || $_GET['token'] == SYNC_TOKEN) { echo "----------------------------------------------------------------------------------------------------------- __ __ __ ___ _ ____ ___ ____ ____ \ \ / /__ ___ \ \ / (_)___(_) ___ _ __ | _ \ / _ \/ ___| / ___| _ _ _ __ ___ \ \ /\ / / _ \ / _ \ \ \ / /| / __| |/ _ \| '_ \ | |_) | | | \___ \ \___ \| | | | '_ \ / __| \ V V / (_) | (_) | \ V / | \__ \ | (_) | | | | | __/| |_| |___) | ___) | |_| | | | | (__ \_/\_/ \___/ \___/ \_/ |_|___/_|\___/|_| |_| |_| \___/|____/ |____/ \__, |_| |_|\___| |___/ ----------------------------------------------------------------------------------------------------------- "; echo "Date/Time: " . date("Y-m-d H:i:s") . "\n"; echo " - Confirming constants are defined:"; $constants_defined = true; if(!defined('POS_API_BASE_URL')) { echo " - POS API Base URL is not defined \n"; $constants_defined = false; } if(!defined('POS_API_PLATFORM_KEY')) { echo " - POS API Platform Key is not defined \n"; $constants_defined = false; } if(!defined('WOO_API_BASE_URL')) { echo " - Wordpress WooCommerce API Base URL is not defined \n"; $constants_defined = false; } if(!defined('CONSUMER_KEY')) { echo " - WooCommerce API Consumer Key is not defined \n"; $constants_defined = false; } if(!defined('CONSUMER_SECRET')) { echo " - WooCommerce API Consumer Key is not defined \n"; $constants_defined = false; } if($constants_defined) { echo " - All good. \n"; } echo " - Trying to establish connection with POS API: \n "; ob_end_flush(); // Ping API to test connection. $posapi_test = posapi_request(); if($posapi_test) { // All good. echo " - Connection established, ready to proceed. \n"; // Let's check if we have any $argv // Set the mode as false $mode = false; if(is_array($argv)) { // arguments supplied // always look for the first argument i.e. [1] $mode = $argv[1]; } else { // exit. echo " - Nothing to do, please pass mode as argument in script. \n"; } if($_GET['token'] && $_GET['mode']) { $mode = $_GET['mode']; } // Only run commands based on mode if($mode) { // Full sync mode if($mode == 'fullsync') { echo " - Starting synchronization job. \n"; ob_end_flush(); posapi_full_sync(); } // Incremental sync mode if($mode == 'incremental') { echo " - Starting incremental sync job. \n"; ob_end_flush(); posapi_incremental_sync(); } } } else { echo " - Connection failed. We'll try again later. \n"; } ob_end_flush(); echo "\n\n -- END -- \n\n"; echo "\n --------------------------------- -- _ _ -- -- ___ | | (_) __ __ -- -- / __| | | | | \ \/ / -- -- | (__ | | | | > < -- -- \___| |_| |_| /_/\_\ -- --------------------------------- \n"; } else { echo "Nothing to see here. Go away! \n"; } ob_end_flush(); // Timers $end_time = microtime(true); $execution_time = ($end_time - $start_time); echo "Execution time of script = ".$execution_time." sec. \n"; echo "---\n";