Developer information on the logic we use to send data to Shopify.

The Consonance team set up a nightly scheduled task to push data to Shopify. This article outlines that process.

Open connection

Consonance authenticates with the client’s Shopify store. Learn how to set up the API keys on both the Consonance and Shopify side here.

Add products

For each product that is set to be published to Shopify, Consonance sends a call to create a product on Shopify, unless an API call to the shop finds a ShopifyAPI::Product where the handle matches the Consonance product’s id.

Consonance creates a notice to update the user, which reads: 1 of 9: Consonance has hidden products_to_hide_count products on Shopify. Now updating products_to_update.count products…

Update products

For each product that is set to be published to Shopify, Consonance sends a call to check if there is a matching remote product, by sending a call to find a ShopifyAPI::Product where the handle matches the product’s Consonance id.

If one is found, then two calls are made. The first updates the product, and the second updates its first found variant – since there should only be one variant per product.

Product update

First, Consonance will send an instruction to Shopify to destroy any metafields already associated with the product. This is our preferred method, rather than storing metafield IDs locally.

Next, Consonance sends a call to update the attributes (but not any child variant attributes) of the Shopify product.

Why not update the variant at the same time? If you update a variant through the product association (using the Shopify_api Ruby gem), the variant is destroyed and another created, rather than updating. This changes the variant id, which breaks integrations that rely on it.

If the update call fails, a notice is written to Consonance’s log, capturing the error message.

Variant update

Consonance checks to see if its variant’s barcode or price is different to the Shopify variant’s barcode or price. If so, it updates the Shopify variant. If the update call fails, a notice is written to Consonance’s log, capturing the error message.

Consonance creates a notice to update the user, which reads: 2 of 9: Consonance has added products_to_add_count products. Now checking products_to_hide_count products hidden…

Hide products

This sets published_at to nil on any Shopify products which have been set to no in Consonance.

Consonance creates a notice to update the user, which reads: 3 of 9: Consonance has updated products_to_update_count products on Shopify. Now reviewing product_images_to_add_count images…

Add or update product images

Consonance has to figure out which images have changed, but we don’t store Shopify image IDs, and we can’t compare URLs because Shopify writes uploaded images to its own CDN. So we use the following logic.

  • Out of all the requiredproductsalreadyonshop, choose those where
  • the image was updated more recently in Consonance than on Shopify, or
  • there is a different number of images in Consonance than on Shopify.

Consonance creates a notice to update the user, which reads: 4 of 9: Consonance has updated product_images_to_add_count products on Shopify. Now adding collections_to_add_count collections…

Add collections

See which collections Consonance creates to send to Shopify in Shopify and Consonance: Data Reference.

For each of those collections, Consonance checks to see whether it exists on Shopify. If it does not, Consonance sends a call to create it.

Consonance creates a notice to update the user, which reads: 5 of 9: Consonance has added collections_to_add_count collections to Shopify. Now checking collections_to_hide_count collections hidden…

Hide collections

For all previously created collections which, because the products have been unpublished, have no products in them, Consonance sends a call to set published to nil.

Consonance creates a notice to update the user, which reads: 6 of 9: Consonance has hidden collections_to_hide_count collections on Shopify. Now updating collections_to_update_count collections…

Update collections

Where the Consonance collection and the Shopify collection (matching on handle, excluding collects) differs, Consonance sends an update message for collection attributes – not the contents of the collection (the collects).

If a collection has an image it will always mismatch because Shopify uploads to its cdn, which won’t match the Consonance URL.

Consonance creates a notice to update the user, which reads: 7 of 9: Consonance has updated collections_to_update_count collections on Shopify. Now removing #{collectstoremove_count} products from collections…

Remove collects

Consonance sends a destroy notification to any collects found matching the ID where they should be removed because the products have been unpublished. This only destroys the join table record, not the collection or the product within the collection.

Consonance creates a notice to update the user, which reads: 8 of 9: Consonance has removed collects_to_remove_count products from collections on Shopify. Now adding #{collectstoadd_count} products to collections…

Add collects

Consonance sends a create message to create a collect if it can’t find one based on product id and collection id already.

Consonance creates a notice to update the user, which reads: 9 of 9: Consonance has added collects_to_add_count products to collections on Shopify. Sync complete!