diff --git a/ecommerce_integrations/unicommerce/api_client.py b/ecommerce_integrations/unicommerce/api_client.py index 8bb5336e..dcedac38 100644 --- a/ecommerce_integrations/unicommerce/api_client.py +++ b/ecommerce_integrations/unicommerce/api_client.py @@ -92,14 +92,17 @@ def get_unicommerce_item(self, sku: str) -> Optional[JsonDict]: if status: return item - def create_update_item(self, item_dict: JsonDict) -> Tuple[JsonDict, bool]: + def create_update_item(self, item_dict: JsonDict, update=False) -> Tuple[JsonDict, bool]: """Create/update item on unicommerce. ref: https://documentation.unicommerce.com/docs/createoredit-itemtype.html """ - return self.request( - endpoint="/services/rest/v1/catalog/itemType/createOrEdit", body={"itemType": item_dict} - ) + + endpoint = "/services/rest/v1/catalog/itemType/createOrEdit" + if update: + # Edit has separate endpoint even though docs suggest otherwise + endpoint = "/services/rest/v1/catalog/itemType/edit" + return self.request(endpoint=endpoint, body={"itemType": item_dict}) def get_sales_order(self, order_code: str) -> Optional[JsonDict]: """Get details for a sales order. diff --git a/ecommerce_integrations/unicommerce/product.py b/ecommerce_integrations/unicommerce/product.py index f33ae37f..ea61d98a 100644 --- a/ecommerce_integrations/unicommerce/product.py +++ b/ecommerce_integrations/unicommerce/product.py @@ -245,7 +245,10 @@ def upload_items_to_unicommerce( for item_code in item_codes: item_data = _build_unicommerce_item(item_code) - _, status = client.create_update_item(item_data) + sku = item_data.get("skuCode") + + item_exists = bool(client.get_unicommerce_item(sku)) + _, status = client.create_update_item(item_data, update=item_exists) if status: _handle_ecommerce_item(item_code) @@ -268,6 +271,9 @@ def _build_unicommerce_item(item_code: ItemCode) -> JsonDict: item_json["enabled"] = not bool(item.get("disabled")) for barcode in item.barcodes: + if not item_json.get("scanIdentifier"): + # Set first barcode as scan identifier + item_json["scanIdentifier"] = barcode.barcode if barcode.barcode_type == "EAN": item_json["ean"] = barcode.barcode elif barcode.barcode_type == "UPC-A": diff --git a/ecommerce_integrations/unicommerce/tests/fixtures/simple_item.json b/ecommerce_integrations/unicommerce/tests/fixtures/simple_item.json index 472ce91a..74e33ca6 100644 --- a/ecommerce_integrations/unicommerce/tests/fixtures/simple_item.json +++ b/ecommerce_integrations/unicommerce/tests/fixtures/simple_item.json @@ -10,7 +10,7 @@ "categoryCode": "Products", "name": "TITANIUM WATCH", "description": "This is a watch.", - "scanIdentifier": "TITANIUM_WATCH", + "scanIdentifier": "73513537", "length": 100, "width": 100, "height": 50,