Good morning all, I need to be able to import a json product feed on a daily basis to update our products and stock. I am starting to write a plugin to make this work, but have a couple of questions if somebody could please advise? (I'm relatively new to Wordpress)

Here is a sample of the json I need to import:

            "Type": "Air Pistol",
            "Mechanism": "CO2",
            "Calibre": "4.5mm BB",
            "Make": "ASG",
            "Model": "SCHOFIELD",
            "Variant": "GREY",
            "Origin": null,
            "Orientation": "Right Handed",
            "Ejection": null,
            "Trigger": "0",
            "BarrelLength": "0.000",
            "BarrelLengthInches": null,
            "BarrelLengthFraction": null,
            "StockLength": "0.000",
            "StockLengthInches": null,
            "StockLengthFraction": null,
            "WeightPounds": null,
            "WeightOunces": null,
            "Choke1": null,
            "Choke2": null,
            "ScopeMake": null,
            "ScopeMag": null,
            "Summary": null,
            "Description": null,
            "Condition": "New",
            "Price": 170,
            "ExpiryDate": "2019-08-10",
            "ImageCount": 4,
            "StockNumber": "180328/003",
            "PairedGun": null,
            "SerialNumber": "17J11774",
            "ProofedLO": null,
            "ProofedRU": null,
            "Cased": null,
            "Chamber": null,
            "Paired": 0,
            "Images": [
                    "Number": 1,
                    "FullPath": "https://images.guntrader.uk/GunImages/18/1803/18032/180329160440003/180329160440003-1.jpg",
                    "ThumbPath": "https://images.guntrader.uk/GunImages/Thumbnails/180329160440003-1-120x120.jpg"
                    "Number": 2,
                    "FullPath": "https://images.guntrader.uk/GunImages/18/1803/18032/180329160440003/180329160440003-2.jpg",
                    "ThumbPath": "https://images.guntrader.uk/GunImages/Thumbnails/180329160440003-2-120x120.jpg"
                    "Number": 3,
                    "FullPath": "https://images.guntrader.uk/GunImages/18/1803/18032/180329160440003/180329160440003-3.jpg",
                    "ThumbPath": "https://images.guntrader.uk/GunImages/Thumbnails/180329160440003-3-120x120.jpg"
                    "Number": 4,
                    "FullPath": "https://images.guntrader.uk/GunImages/18/1803/18032/180329160440003/180329160440003-4.jpg",
                    "ThumbPath": "https://images.guntrader.uk/GunImages/Thumbnails/180329160440003-4-120x120.jpg"
            "ID": "180329160440003",
            "Licence": "No Licence",
            "Created": "2018-03-29 16:04:40",
            "Modified": "2019-07-10 02:00:47"

I have created categories to match the 'Type' key in my data and attributes for all other keys, is this the right way to go about this?

How would I go about mapping these keys to the attributes, assuming that this is the correct way to do this.

Thanks in advance.

1 Answer 1


You could use a combination of update_post_meta(), set_objetc_terms(), and wp_update_post() to update your products. It is a good idea to use taxonomies (product categories and product attributes in WooC) as extensively as possible to store common product data as it makes searching for products faster compared to storing the data in post_meta.

Here's a concept of a product updater that might do the trick. This is naturally untested and requires you to fill in the details.

If there are hundreds or thousands of products that needs to be updated every time, then I'm not sure, if this the most performant way of doing the update. Maybe splitting the update into batches (saved as transients maybe) and executing the batches with a (real) cronjob would help.

foreach ( $products_from_decoded_json as $product_data_array ) {
  product_updater( $product_data_array );

function product_updater( array $product_data ) {

  $meta = array(
    'Price' => '_price',
    // rest of the meta_key mappings
    // where json_key => WP meta_key

  $terms = array(
    'Make' => 'pa_make',
    'Type' => 'product_category',
    // rest of product taxonomy mappings
    // where json_key => WP taxonomy_slug

  $product_id = get_product_id( $product_data );

  update_product_meta( $product_id, $meta, array_filter( $product_data, function($key) {
    return isset( $meta[$key] );

  update_product_terms( $product_id, $terms, array_filter( $product_data, function($key) {
    return isset( $terms[$key] );

  // Check params array params from wp_insert_post docs
  wp_update_post( array(
    'ID' => $product_id,
    'post_content' => $product_data['Description'],
    'post_excerpt' => $product_data['Summary'],
    'post_modified' => $product_data['Modified'],
  ) );


function get_product_id( $data ) {
  // Returned value should match an existing post ID
  // return int $id;

function update_product_meta( int $id, array $meta, array $product_data ) {
  foreach ( $meta as $json_key => $meta_key ) {
    // update_post_meta( $post_id, $meta_key, $meta_value, $prev_value = '' )
    update_post_meta( $id, $meta_key, $product_data[$json_key] );

function update_product_terms( int $id, array $meta, array $product_data ) {
  foreach ( $terms as $json_key => $taxonomy ) {
    // get_term_by( $field, $value, $taxonomy, $output = OBJECT, $filter = 'raw' )
    $term_data = get_term_by( 'name', $product_data[$json_key], $taxonomy );
    if ( $term_data ) {
      // wp_set_object_terms( $object_id, $terms, $taxonomy, $append = false )
      wp_set_object_terms( $id, $term_data->term_id, $taxonomy, false );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.