Skip to content

Instantly share code, notes, and snippets.

@sharoonthomas
Created January 5, 2018 16:43
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sharoonthomas/ada536f3f14c7d16ac8d3d3876870f3d to your computer and use it in GitHub Desktop.
Save sharoonthomas/ada536f3f14c7d16ac8d3d3876870f3d to your computer and use it in GitHub Desktop.
Update supplier inventory from third party shopify store

Supplier Inventory Feed

Usage

  • Your supplier uses shopify.
  • They are willing to expose inventory over Shopify API giving you a private app API key and Password.
  • You are using fulfil and has product suppliers defined.

How this works

Read more on Fulfil support site:

Installing dependencies

pip install ShopifyAPI fulfil-client

The dependencies are also included in a requirements.txt file

pip install -r requirements.txt

Running the script

The first step is to set the required credentials as environment variables.

Setup environment variables

The following environment variables are required

SHOP_NAME

The shopify store name. Usually the first part of the .myshopify.com URL you use to access the store admin.

SHOPIFY_API_KEY

The API Key from a Shopify Private app

SHOPIFY_PASSWORD

Password from the private app

FULFIL_SUBDOMAIN

The subdomain of your fulfil instance.

FULFIL_API_KEY

The API key of your fulfil instance. The user must have permission to read and write product suppliers.

SUPPLIER_CODE

The code of the supplier (who own the shopify store). The code can be found on the top right card on the supplier contact. Usually a number.

You can usually source a script with all the variables

export SHOPIFY_API_KEY=
export SHOP_NAME=
export SHOPIFY_PASSWORD=
export FULFIL_SUBDOMAIN=
export FULFIL_API_KEY=
export SUPPLIER_CODE=

Run the script

python sync_inventory.py

This will read the environment variables, connect to shopify, match with product supplier records in fulfil and update them with the inventory available.

Hosting ideas

Hosting this on Python Anywhere

Sign up for Python Anywhere

You need a paid account because free accounts have restrictions on making api calls.

Navigate to files and edit the .profile (or .bashrc) and include the environment variables from above.

Setting up the environment

Click on "Open bash console here" and follow the steps below:

Create a virtualenv.

mkvirtualenv shopify

Install dependencies

pip install ShopifyAPI fulfil-client

Now run the script python sync_inventory.py

If this runs, you can now setup a scheduled task to do this automatically.

Setting up a schdeuled task

Copy the path to pythin on the virtualenv. To get this just run which python

My result was /home/sharoonthomas/.virtualenvs/shopify/bin/python

  • Navigate to Tasks from the dashboard.
  • Paste the command

Example:

/home/sharoonthomas/.virtualenvs/shopify/bin/python /home/sharoonthomas/sync_inventory.py

ShopifyAPI
fulfil-client
import os
from collections import OrderedDict
import shopify
from fulfil_client import Client
# Setup shopify to access the supplier's inventory.
# The shop name can be found from the admin url used for logging in
# This assumes the use of a private app
# See: https://help.shopify.com/manual/apps/private-apps#generate-credentials-from-the-shopify-admin
shop_url = "https://%s:%s@%s.myshopify.com/admin" % (
os.environ['SHOPIFY_API_KEY'],
os.environ['SHOPIFY_PASSWORD'],
os.environ['SHOP_NAME'],
)
shopify.ShopifyResource.set_site(shop_url)
# Setup a connection to Fulfil
fulfil = Client(
os.environ['FULFIL_SUBDOMAIN'],
os.environ['FULFIL_API_KEY'],
)
ProductSupplier = fulfil.model('purchase.product_supplier')
supplier_code = os.environ['SUPPLIER_CODE']
def fetch_inventory_and_update():
all_supplied_items = ProductSupplier.search_read_all(
# The search filter
[
('party.code', '=', supplier_code)
],
None, # Sort Order
['id', 'code', 'quantity_available']
)
# Make a lookup dictionary for easy access
supplier_items = {}
for item in all_supplied_items:
if not item['code']:
# Skip if there is no supplier sku
continue
supplier_items[item['code'].upper()] = item
print("Found %d items in fulfil by supplier" % len(supplier_items))
# A dictionary to store changes for a bulk update later.
supplier_items_to_update = OrderedDict()
# Get all products from the shopify store
for product in shopify.Product.find():
# browse through each variant
for variant in product.variants:
sku = variant.sku.upper()
if sku not in supplier_items:
continue
item = supplier_items[sku]
if item['quantity_available'] != variant.inventory_quantity:
supplier_items_to_update[item['id']] = variant.inventory_quantity
# Now check if any inventory changes are there to update
if not supplier_items_to_update:
print("No inventory changes to update fulfil with")
return
print("Updating %d supplier items" % len(supplier_items_to_update))
# Call fulfil's bulk update api
bulk_update_args = []
for supplier_item_id, available_qty in supplier_items_to_update.items():
bulk_update_args.append([supplier_item_id])
bulk_update_args.append({
'quantity_available': available_qty,
})
ProductSupplier.write(*bulk_update_args)
if __name__ == '__main__':
fetch_inventory_and_update()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment