Sort products when fetching from Woo API

default sort is by 'date desc' which is not always determinate

also, combine logic for fetching products
This commit is contained in:
Lance Edgar 2022-11-27 14:30:52 -06:00
parent ddced3e90c
commit 3d0f5ddd81
3 changed files with 63 additions and 35 deletions

View file

@ -2,7 +2,7 @@
################################################################################
#
# Rattail -- Retail Software Framework
# Copyright © 2010-2021 Lance Edgar
# Copyright © 2010-2022 Lance Edgar
#
# This file is part of Rattail.
#
@ -27,6 +27,7 @@ WooCommerce model importers
from woocommerce import API as WooAPI
from rattail import importing
from rattail_woocommerce.woocommerce.util import get_woocommerce_products
class ToWooCommerce(importing.Importer):
@ -114,23 +115,11 @@ class ProductImporter(ToWooCommerce):
Fetch existing products from WooCommerce.
"""
cache = {}
page = 1
while True:
response = self.api.get('products', params={'per_page': 100,
'page': page})
for product in response.json():
data = self.normalize_local_object(product)
normal = self.normalize_cache_object(product, data)
key = self.get_cache_key(product, normal)
cache[key] = normal
# TODO: this seems a bit hacky, is there a better way?
link = response.headers.get('Link')
if link and 'rel="next"' in link:
page += 1
else:
break
for product in get_woocommerce_products(self.api):
data = self.normalize_local_object(product)
normal = self.normalize_cache_object(product, data)
key = self.get_cache_key(product, normal)
cache[key] = normal
return cache
def get_single_local_object(self, key):