Created
September 5, 2023 14:34
-
-
Save andykillen/1288e6f2fd81b1f611b4351c51215cbb to your computer and use it in GitHub Desktop.
Faster updates
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
global $wpdb; | |
$query = $wpdb->prepare("SELECT post_id, table1.meta_value as sku, table2.meta_value as price ". | |
"FROM {$wpdb->postmeta} as table1 ". | |
"JOIN {$wpdb->postmeta} as table2 USING (post_id) ". | |
"WHERE ". | |
"table1.meta_key = '_sku' AND ". | |
"table2.meta_key = '_price' ". | |
"GROUP BY post_id". | |
";"); | |
$results = $wpdb->get_results($query); | |
$file = file_get_contents("feed.json"); | |
$json_array = json_decode($file, true); | |
$working_array = []; | |
foreach ( $json_array as $product_data ) { | |
$working_array[$product_data['sku']] = [ | |
'price' => $product_data['rrp'] | |
]; | |
} | |
foreach($results as $product) { | |
if(isset($working_array[$product->sku]) && $working_array[$product->sku]['price'] != $product->price) { | |
update_post_meta( $product->post_id, '_price', $working_array[$product->sku]['price'], $product->price ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment