I am cracking my head over this and I am not able to find a possible solution or even an idea how to do this.
I have a script (below) that combines and rewrites multiple JSON files into one JSON file. I have a WooCommerce webshop that imports via WP All Import the JSON file into the webshop. Overnight WP All Import updates all products with the changes in the JSON file. The issue is that WP All Import needs to check all items in the JSON and it takes a while.
My questions: Is it possible to add a 'last-updated' date to each item via PHP into the JSON when it finds changes for that item? Is it possible to only generate a JSON file with the changes that finds? Maybe it can compare a new JSON file to the JSON file from yesterday and generate a file with the changes?
Current code:
<?php
error_reporting(E_ALL);
// API URLS
$urls = array
( 'URL'
, 'URL'
);
// Remove FALSE but not 0 and null
function RemoveFalseButNotZero($value) {
return ($value || is_numeric($value) || $value === null );
}
// Function to delete all empty JSON items
function array_filter_recursive($input)
{
foreach ($input as &$value)
{
if (is_array($value))
{
$value = array_filter_recursive($value);
}
}
return array_filter($input, 'RemoveFalseButNotZero');
}
$sets = array();
foreach ($urls as $url)
{
$json = file_get_contents($url); // Collect all URLS
$data = json_decode($json, true); // Decode JSON to read and edit
foreach ($data as $key => $innerObject) { // Delete each item that is not needed
if (isset(
$innerObject['SPEC_Bullet point 1'],
$innerObject['SPEC_Bullet point 2'],
$innerObject['SPEC_Bullet point 3'],
$innerObject['SPEC_Bullet point 4'],
$innerObject['SPEC_Bullet point 5'],
$innerObject['SPEC_Extra info'],
$innerObject['SPEC_Keywords'],
$innerObject['SPEC_Featured'],
$innerObject['SPEC_Bullet point 1 TEXT'],
$innerObject['SPEC_Bullet point 2 LIST'],
$innerObject['SPEC_Bullet Point 1'],
$innerObject['SPEC_Bullet point 2 TEXT'],
$innerObject['SPEC_Bullet Point 1 TEXT'],
$innerObject['SPEC_Bullet Point 2'],
$innerObject['SPEC_Bullet point 3 LIST'],
$innerObject['SPEC_Bullet Point 2 TEXT'],
$innerObject['SPEC_Bullet point 3 TEXT'],
$innerObject['SPEC_Bullet point 4 LIST'],
$innerObject['SPEC_Bullet point 4 TEXT'],
$innerObject['SPEC_Bullet Point 3'],
$innerObject['SPEC_Bullet Point 3 TEXT'],
$innerObject['SPEC_Bullet point 5 LIST'],
$innerObject['SPEC_Bullet point 5 TEXT'],
$innerObject['SPEC_Bullet Point 4 TEXT'],
$innerObject['SPEC_Bullet Point 5'],
$innerObject['SPEC_Bullet Point 5 TEXT'],
$innerObject['SPEC_Bullet point 1 LIST']
)) {
unset(
$data[$key]['SPEC_Bullet point 1'],
$data[$key]['SPEC_Bullet point 2'],
$data[$key]['SPEC_Bullet point 3'],
$data[$key]['SPEC_Bullet point 4'],
$data[$key]['SPEC_Bullet point 5'],
$data[$key]['SPEC_Extra info'],
$data[$key]['SPEC_Keywords'],
$data[$key]['SPEC_Featured'],
$data[$key]['SPEC_Bullet point 1 TEXT'],
$data[$key]['SPEC_Bullet point 2 LIST'],
$data[$key]['SPEC_Bullet Point 1'],
$data[$key]['SPEC_Bullet point 2 TEXT'],
$data[$key]['SPEC_Bullet Point 1 TEXT'],
$data[$key]['SPEC_Bullet Point 2'],
$data[$key]['SPEC_Bullet point 3 LIST'],
$data[$key]['SPEC_Bullet Point 2 TEXT'],
$data[$key]['SPEC_Bullet point 3 TEXT'],
$data[$key]['SPEC_Bullet point 4 LIST'],
$data[$key]['SPEC_Bullet point 4 TEXT'],
$data[$key]['SPEC_Bullet Point 3'],
$data[$key]['SPEC_Bullet Point 3 TEXT'],
$data[$key]['SPEC_Bullet point 5 LIST'],
$data[$key]['SPEC_Bullet point 5 TEXT'],
$data[$key]['SPEC_Bullet Point 4 TEXT'],
$data[$key]['SPEC_Bullet Point 5'],
$data[$key]['SPEC_Bullet Point 5 TEXT'],
$data[$key]['SPEC_Bullet point 1 LIST']
);
}
}
$json = substr($json, 1, -1); // Delete upper level brackets
$sets = array_merge($sets, $data); // Merge all URLS in one single JSON file
$filteredData = array_filter_recursive($sets); // Filter the empty items and delete them
}
$new = json_encode($filteredData, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES); // Encode back to JSON pretty print and remove unescaped slashes
echo '<pre>'; // Add breaks
echo $new; // Echo the result
// Write the new file
$fp = fopen('dsales_watchjewel_combined_en.json', 'w');
fwrite($fp, $new);
fclose($fp);
Regards Ely
Comparing old versus new and create a refined file with only the changes on a daily basis via cron.
Utility.php
<?php
error_reporting(E_ALL);
function RemoveFalseButNotZero($value) {
return ($value || is_numeric($value) || $value === null );
}
// Function to delete all empty JSON items
function array_filter_recursive($input)
{
foreach ($input as &$value)
{
if (is_array($value))
{
$value = array_filter_recursive($value);
}
}
return array_filter($input, 'RemoveFalseButNotZero');
}
function getFileName($type, $language = 'en') {
return sprintf('dsales_watchjewel_combined_%s_%s.json', $type, $language);
}
$urls = [
'URL',
'URL'
];
$compare = [
'ParentSKU',
'Category',
'Title',
'Description',
'Brand',
'RRP',
'Price',
'Stock',
'SPEC_Large size',
'SPEC_COLOR',
'SPEC_SIZE W x D x H (mm)',
'SPEC_TYPE ',
'SPEC_BACKLIGHT',
'SPEC_SNOOZE FUNCTION',
'SPEC_RADIO CONTROLLED',
'SPEC_BATTERY TYPE',
'SPEC_WEIGHT gram',
'SPEC_OTHERS',
'SPEC_Model_Name',
'SPEC_Model Name',
'SPEC_Family',
'SPEC_Weight-Gram',
'SPEC_Type',
'SPEC_Target_Audience',
'SPEC_Target_audience',
'SPEC_Metal',
'SPEC_Display_Type',
'SPEC_Jewel_type',
'SPEC_Color',
'SPEC_Movement',
'SPEC_Metal_type',
'SPEC_Features_1',
'SPEC_Finish',
'SPEC_Metal Color',
'SPEC_Width',
'SPEC_Features_2',
'SPEC_Material_type',
'SPEC_Material_color',
'SPEC_Features_3',
'SPEC_Weight',
'SPEC_Features_4',
'SPEC_Size',
'SPEC_Clasp_type',
'SPEC_Stone',
'SPEC_Back_finding',
'SPEC_Features_5',
'SPEC_Calendar',
'SPEC_Ring_size',
'SPEC_Setting',
'SPEC_Ring_resizable',
'SPEC_ATM',
'SPEC_Bullet point 1',
'SPEC_Bullet point 2',
'SPEC_Height-cm',
'SPEC_Case_material',
'SPEC_Case_Style',
'SPEC_Length-cm',
'SPEC_Bullet point 3',
'SPEC_Bullet point 4',
'SPEC_Width-cm',
'SPEC_Case_color',
'SPEC_Dial_Color',
'SPEC_Diameter-cm',
'SPEC_Bullet point 5',
'SPEC_Gem_shape',
'SPEC_Band_Material',
'SPEC_Band_Color',
'SPEC_Gem_type',
'SPEC_Diamond_quantity',
'SPEC_Band_Width_mm',
'SPEC_Band_Length_cm',
'SPEC_Total_Diamond_Weight',
'SPEC_Diamond_color',
'SPEC_Case_Diameter_mm',
'SPEC_Case_Width_mm',
'SPEC_Diamond_clarity',
'SPEC_Diamond_setting',
'SPEC_Case_Length_mm',
'SPEC_Case_Thickness_mm',
'SPEC_Diamond_shape',
'SPEC_Gem_Type 2',
'SPEC_Bezel_Function',
'SPEC_Bezel_Material_Type',
'SPEC_Total_Gem_Weight',
'SPEC_Gem_color',
'SPEC_Gem_Type',
'SPEC_Gem_quantity',
'SPEC_Diamond_Weight_ct',
'SPEC_Stamp_Type',
'SPEC_Pearl_type',
'SPEC_Pearl_color',
'SPEC_Clasp_Type',
'SPEC_Weight_g',
'SPEC_Pearl_size',
'SPEC_Pearl_shape',
'SPEC_Dial_Window_Material',
'SPEC_Extra info',
'SPEC_Chain Type',
'SPEC_Battery',
'SPEC_Necklace_type',
'SPEC_Charm_shape',
'SPEC_Box_Length_cm',
'SPEC_Box Height cm',
'SPEC_Charm_type',
'SPEC_Ring type',
'SPEC_Box Width cm',
'SPEC_Bracelet_type',
'SPEC_Product Description',
'SPEC_Brand Description',
'Images'
];
Run.php
<?php
include 'utility.php';
$sets = array();
foreach ($urls as $url)
{
$json = file_get_contents($url); // Collect all URLS
$data = json_decode($json, true); // Decode JSON to read and edit
foreach ($data as $key => $innerObject) { // Delete each item that is not needed
if (isset(
$innerObject['SPEC_Bullet point 1'],
$innerObject['SPEC_Bullet point 2'],
$innerObject['SPEC_Bullet point 3'],
$innerObject['SPEC_Bullet point 4'],
$innerObject['SPEC_Bullet point 5'],
$innerObject['SPEC_Extra info'],
$innerObject['SPEC_Keywords'],
$innerObject['SPEC_Featured'],
$innerObject['SPEC_Bullet point 1 TEXT'],
$innerObject['SPEC_Bullet point 2 LIST'],
$innerObject['SPEC_Bullet Point 1'],
$innerObject['SPEC_Bullet point 2 TEXT'],
$innerObject['SPEC_Bullet Point 1 TEXT'],
$innerObject['SPEC_Bullet Point 2'],
$innerObject['SPEC_Bullet point 3 LIST'],
$innerObject['SPEC_Bullet Point 2 TEXT'],
$innerObject['SPEC_Bullet point 3 TEXT'],
$innerObject['SPEC_Bullet point 4 LIST'],
$innerObject['SPEC_Bullet point 4 TEXT'],
$innerObject['SPEC_Bullet Point 3'],
$innerObject['SPEC_Bullet Point 3 TEXT'],
$innerObject['SPEC_Bullet point 5 LIST'],
$innerObject['SPEC_Bullet point 5 TEXT'],
$innerObject['SPEC_Bullet Point 4 TEXT'],
$innerObject['SPEC_Bullet Point 5'],
$innerObject['SPEC_Bullet Point 5 TEXT'],
$innerObject['SPEC_Bullet point 1 LIST']
)) {
unset(
$data[$key]['SPEC_Bullet point 1'],
$data[$key]['SPEC_Bullet point 2'],
$data[$key]['SPEC_Bullet point 3'],
$data[$key]['SPEC_Bullet point 4'],
$data[$key]['SPEC_Bullet point 5'],
$data[$key]['SPEC_Extra info'],
$data[$key]['SPEC_Keywords'],
$data[$key]['SPEC_Featured'],
$data[$key]['SPEC_Bullet point 1 TEXT'],
$data[$key]['SPEC_Bullet point 2 LIST'],
$data[$key]['SPEC_Bullet Point 1'],
$data[$key]['SPEC_Bullet point 2 TEXT'],
$data[$key]['SPEC_Bullet Point 1 TEXT'],
$data[$key]['SPEC_Bullet Point 2'],
$data[$key]['SPEC_Bullet point 3 LIST'],
$data[$key]['SPEC_Bullet Point 2 TEXT'],
$data[$key]['SPEC_Bullet point 3 TEXT'],
$data[$key]['SPEC_Bullet point 4 LIST'],
$data[$key]['SPEC_Bullet point 4 TEXT'],
$data[$key]['SPEC_Bullet Point 3'],
$data[$key]['SPEC_Bullet Point 3 TEXT'],
$data[$key]['SPEC_Bullet point 5 LIST'],
$data[$key]['SPEC_Bullet point 5 TEXT'],
$data[$key]['SPEC_Bullet Point 4 TEXT'],
$data[$key]['SPEC_Bullet Point 5'],
$data[$key]['SPEC_Bullet Point 5 TEXT'],
$data[$key]['SPEC_Bullet point 1 LIST']
);
}
}
$json = substr($json, 1, -1); // Delete upper level brackets
$sets = array_merge($sets, $data); // Merge all URLS in one single JSON file
$filteredData = array_filter_recursive($sets); // Filter the empty items and delete them
}
$new = json_encode($filteredData, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES); // Encode back to JSON pretty print and remove unescaped slashes
echo '<pre>'; // Add breaks
echo $new; // Echo the result
// Write the new file
$fp = fopen(getFileName('new'), 'w');
fwrite($fp, $new);
fclose($fp);
Compare.php
<?php
include 'utility.php';
if (!file_exists(getFileName('old'))) {
file_put_contents(getFileName('old'), json_encode([]));
}
$oldData = json_decode(file_get_contents(getFileName('old')), true);
$newData = json_decode(file_get_contents(getFileName('new')), true);
$refinedData = [];
echo '<pre>';
if (empty($oldData)) {
file_put_contents(getFileName('old'), json_encode($newData, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
exit('Script needs to be run first to sync');
}
foreach ($newData as $newProduct) {
$isNotTheSame = false;
$found = false;
foreach ($oldData as $oldProduct) {
if ($newProduct['SKU'] == $oldProduct['SKU']) {
foreach ($compare as $key) {
if (
(isset($newProduct[$key]) && isset($oldProduct[$key])) &&
($newProduct[$key] != $oldProduct[$key])
) {
$isNotTheSame = $key;
}
}
$found = true;
if ($isNotTheSame) {
$refinedData[] = $newProduct;
}
}
}
/**
* If the product is not found in the old list
* add it to the refined list to be created
*/
if (!$found) {
$refinedData[] = $newProduct;
}
}
file_put_contents(getFileName('old'), json_encode($newData, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
echo json_encode($refinedData, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES); // Echo the result
file_put_contents(getFileName('refined'), json_encode($refinedData, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
$date = date('dmy-His');
file_put_contents(getFileName('refined_' . $date ), json_encode($refinedData, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));