From c581486e8a38c00383636ededee2427d6bbb42c6 Mon Sep 17 00:00:00 2001 From: Rohan Date: Wed, 25 Jan 2023 18:24:59 +0530 Subject: [PATCH] fix: set the correct name for new items (#43) --- shopify_integration/products.py | 41 +++++++++++++++++++++++++++------ 1 file changed, 34 insertions(+), 7 deletions(-) diff --git a/shopify_integration/products.py b/shopify_integration/products.py index 2fb5866..8f86be1 100644 --- a/shopify_integration/products.py +++ b/shopify_integration/products.py @@ -47,7 +47,7 @@ def sync_items_from_shopify(shop_name: str): product: Product for product in products: - if product.attributes.get("variants"): + if has_variants(product): # if template/variant creation is disabled, don't create parent items if shopify_settings.create_variant_items: make_item(shopify_settings, product) @@ -89,7 +89,13 @@ def validate_items(shop_name: str, shopify_order: "Order"): ): shopify_products: List[Product] = shopify_settings.get_products(product_id) for product in shopify_products: - make_item(shopify_settings, product) + if has_variants(product): + if shopify_settings.create_variant_items: + # create the parent product item if it does not exist, and if template/variants + # is enabled in the Shopify settings instance + make_item(shopify_settings, product) + else: + make_item(shopify_settings, product) # create the child variant item if it does not exist variant_id = shopify_item.attributes.get("variant_id") @@ -121,9 +127,14 @@ def validate_items(shop_name: str, shopify_order: "Order"): def get_item_code(shopify_item: "LineItem") -> Optional[str]: item_code = frappe.db.get_value( - "Item", {"shopify_sku": shopify_item.attributes.get("sku")}, "item_code" + "Item", shopify_item.attributes.get("sku"), "item_code" ) + if not item_code: + item_code = frappe.db.get_value( + "Item", {"shopify_sku": shopify_item.attributes.get("sku")}, "item_code" + ) + if not item_code: item_code = frappe.db.get_value( "Item", @@ -162,8 +173,15 @@ def make_item( else: sync_item(shopify_settings, shopify_item) else: - # if template/variant creation is disabled, create items without any relationships - sync_item(shopify_settings, shopify_item) + # if template/variant creation is disabled, only create variant items + if any( + [ + isinstance(shopify_item, Product) + and has_variants(shopify_item), + isinstance(shopify_item, Variant), + ] + ): + sync_item(shopify_settings, shopify_item) def make_item_by_title(shopify_settings: "ShopifySettings", line_item_title: str): @@ -229,8 +247,17 @@ def create_product_attributes(shopify_item: Product) -> List[Dict]: return item_attributes -def has_variants(shopify_item: Product): - return bool(shopify_item.attributes.get("variants")) +def has_variants(product: Product): + # Shopify creates a product variant for ALL products, whether they actually + # have variants or not; the only way to tell if a product has variants is + # to check against the attributes + options = product.attributes.get("options") + if options: + option_values = [option.attributes.get("values") for option in options] + # flatten list of option values + option_values = [value for sublist in option_values for value in sublist] + return "Default Title" not in option_values + return False def update_item_attribute_values(item_attr: "ItemAttribute", values: List[str]):