Last active
June 30, 2020 17:09
-
-
Save sinebeef/d54cac35c403b3fe91a339fa962b7c5d to your computer and use it in GitHub Desktop.
This file contains hidden or 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
# | |
# Grabs the product json and merges with the stock scrapey file and then imports using REST | |
import json, sys | |
import keys_file | |
from woocommerce import API | |
wcapi = API( | |
url=url, # Your store URL | |
consumer_key = consumer_key, | |
consumer_secret = consumer_secret, | |
wp_api=True, # Enable the WP REST API integration | |
version="wc/v3", # WooCommerce WP REST API version | |
timeout=15 | |
) | |
# This gets ALL the products from the database. with wcapi its all or just one :/ | |
# once grabbed will create 2 lists for simple & variable | |
all_products = [] | |
count = 1 | |
while count > 0: | |
variable_products = wcapi.get('products?per_page=100&page=' + str(count)) | |
# print(variable_products.status_code) | |
if variable_products.json(): | |
all_products.extend(variable_products.json()) | |
print("Amount of regular products: " + str(len(all_products))) | |
count = count + 1 | |
else: | |
break | |
# get all the 'variable' products as they have the variation children products in key:variations. | |
variables = [] | |
simples = [] | |
for product in all_products: | |
if product['type'] == 'variable': | |
variables.append(product) | |
if product['type'] == 'simple': | |
simples.append(product) | |
print("Amount of variable products: " + str(len(variables))) | |
print("Amount of variable products: " + str(len(simples))) | |
# reshape the scrape file with child id as key | |
with open("all-of-them.json","r") as r: | |
items = json.load(r) | |
file = { item['id']: item for item in items } | |
# Processing the variation products (products that have a parent ID and many children 'variation ID's') | |
for variable in variables: | |
new_list = [] | |
for child in variable['variations']: | |
if child in file: | |
#print(file[child]['sku']) | |
it = { | |
# child variation id | |
"id": str(file[child]['id']), | |
# set stock level, something or 0 | |
"stock_quantity": str(file[child]['uk_stock']), | |
} | |
# no UK or no USA, totally out of stock. | |
if int(file[child]['uk_stock']) == 0 and int(file[child]['usa_stock']) == 0: | |
it.update({ | |
"stock_status": "outofstock", | |
"backorders": "no", | |
"backorders_allowed": false, | |
"backordered": false, | |
}) | |
# check for date 24/24/2004 etc | |
if "/" in file[child]['usa_stock']: | |
it.update({ | |
"meta_data":[ | |
{ "key": "_uk_duedate", | |
"value": str(file[child]['uk_duedate']) | |
} | |
], | |
}) | |
# no UK stock but USA in stock so allow backorder with 14 day delivery message | |
if int(file[child]['uk_stock']) == 0 and int(file[child]['usa_stock']) < 0: | |
it.update({ | |
"stock_status": "onbackorder", | |
"backorders": "notify", | |
"backorders_allowed": true, | |
"backordered": true, | |
"meta_data":[ | |
{ "key": "_backorder_max", | |
"value": str(file[child]['usa_stock']) | |
}, | |
{ "key": "_backorder_description", | |
"value": "Approx 14 days delivery" | |
} | |
], | |
}) | |
else: | |
it.update({ | |
"stock_status": "instock", | |
"backorders": "no", | |
"backorders_allowed": false, | |
"backordered": false, | |
}) | |
new_list.append(it) | |
data = { "update": new_list } | |
params = 'products/%s/variations/batch' % ( str(variable['id'] ) ) | |
print(wcapi.post(params, data).json()) | |
exit() | |
else: | |
print("id from site does not exist in origional scrapey passed over id: " + str(child)) | |
simple_list = [] | |
for simple in simples: | |
if simple in file: | |
#create dict | |
it = { | |
# product id | |
"id": str(file[simple]['id']), | |
# set stock level, something or 0 | |
"stock_quantity": str(file[simple]['uk_stock']), | |
} | |
# no UK or no USA, totally out of stock. | |
if int(file[simple]['uk_stock']) == 0 and int(file[simple]['usa_stock']) == 0: | |
it.update({ | |
"stock_status": "outofstock", | |
"backorders": "no", | |
"backorders_allowed": false, | |
"backordered": false, | |
}) | |
# check for date 24/24/2004 etc | |
if "/" in file[simple]['usa_stock']: | |
it.update({ | |
"meta_data":[ | |
{ "key": "_uk_duedate", | |
"value": str(file[simple]['uk_duedate']) | |
} | |
], | |
}) | |
# no UK stock but USA in stock so allow backorder with 14 day delivery message | |
if int(file[simple]['uk_stock']) == 0 and int(file[simple]['usa_stock']) < 0: | |
it.update({ | |
"stock_status": "onbackorder", | |
"backorders": "notify", | |
"backorders_allowed": true, | |
"backordered": true, | |
"meta_data":[ | |
{ "key": "_backorder_max", | |
"value": str(file[simple]['usa_stock']) | |
}, | |
{ "key": "_backorder_description", | |
"value": "Approx 14 days delivery" | |
} | |
], | |
}) | |
else: | |
it.update({ | |
"stock_status": "instock", | |
"backorders": "no", | |
"backorders_allowed": false, | |
"backordered": false, | |
}) | |
simple_list.append(it) | |
else: | |
print("simple id from site does not exist in origional scrapey passed over id: " + str(simple)) | |
data = { "update": simple_list } | |
params = 'products/batch' | |
#print(wcapi.put(params, data).json()) | |
size = 100 | |
for start_index in range(0, len(simple_list), size): | |
print(wcapi.post('products/batch', {'update': simple_list[start_index:start_index + size]})) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment