Skip to content

Commit

Permalink
fix: set the correct name for new items (#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
Alchez authored Jan 25, 2023
1 parent 608a2d8 commit c581486
Showing 1 changed file with 34 additions and 7 deletions.
41 changes: 34 additions & 7 deletions shopify_integration/products.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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]):
Expand Down

0 comments on commit c581486

Please sign in to comment.