Skip to content

Instantly share code, notes, and snippets.

@bkspace
Last active October 29, 2024 13:57
Show Gist options
  • Save bkspace/d978922d1076bcdd9686ebd87f542605 to your computer and use it in GitHub Desktop.
Save bkspace/d978922d1076bcdd9686ebd87f542605 to your computer and use it in GitHub Desktop.
Discount Kit discount detection
{% liquid
assign discounts = shop.metafields.discount_kit.campaign.value.discounts
assign current_date = 'now' | date: '%s' | times: 1
# Initialize an array to store discount distances
assign discount_distances = ''
for discount in discounts
assign config = discount.discountNode.config.value
assign starts_at = discount.startsAt | date: '%s' | times: 1
assign ends_at = discount.endsAt | date: '%s' | times: 1
# Initial eligibility checks
if discount.discountType != discount_type
continue
endif
if ends_at > 0 and ends_at <= current_date
continue
endif
if current_date <= starts_at
continue
endif
if config.matchRule.excludeSale and product.compare_at_price > 0
continue
endif
if config.allowB2b != true and customer.b2b?
continue
endif
if config.customerTags
assign matching_tag = false
for tag in customer.tags
for customer_tag in config.customerTags
if tag == customer_tag
assign matching_tag = true
break
endif
endfor
endfor
if matching_tag != true
continue
endif
endif
if config.marketHandles
assign matching_market = false
for market in config.marketHandles
if market == localization.market.handle
assign matching_market = true
break
endif
endfor
if matching_market != true
continue
endif
endif
if config.currencyCode and config.currencyCode != cart.currency.iso_code
continue
endif
# Initialize totals for matching items
assign matching_subtotal = 0
assign matching_quantity = 0
# Iterate over cart items to accumulate totals based on matchRule
for item in cart.items
assign item_matches = config.matchRule.all
case config.matchRule.type
when 'collection'
for col in config.matchRule.include
assign product_collections = item.product.collections | map: 'id'
assign col_num = col | times: 1
if product_collections contains col_num
assign item_matches = true
endif
endfor
when 'product'
for product_id in config.matchRule.include
assign product_id_num = product_id | times: 1
if item.product.id == product_id_num
assign item_matches = true
endif
endfor
when 'productSelection'
for product_selection in config.matchRule.selection
assign product_id_num = product_selection.id | times: 1
if item.product.id == product_id_num
assign item_matches = true
endif
endfor
endcase
# Accumulate totals if the item matches
if item_matches
assign matching_subtotal = matching_subtotal | plus: item.original_line_price
assign matching_quantity = matching_quantity | plus: item.quantity
endif
endfor
# Proceed only if there are matching items
# Sort tiers based on 'buys.value' in ascending order
assign sorted_tiers = config.tiers | sort: 'buys.value'
assign current_tier = null
assign next_tier = null
for tier in sorted_tiers
if tier.buys.type == 'minimumSubtotal'
assign required = tier.buys.value
if matching_subtotal >= required
assign current_tier = tier
else
assign next_tier = tier
break
endif
elsif tier.buys.type == 'minimumQuantity'
assign required = tier.buys.value
if matching_quantity >= required
assign current_tier = tier
else
assign next_tier = tier
break
endif
endif
endfor
# Calculate distance to next tier
if next_tier != null
if next_tier.buys.type == 'minimumSubtotal'
assign distance = next_tier.buys.value | minus: matching_subtotal | money
capture distance_label
echo 'Spend ' | append: distance | append: ' more'
endcapture
elsif next_tier.buys.type == 'minimumQuantity'
assign distance = next_tier.buys.value | minus: matching_quantity
capture distance_label
echo 'Add ' | append: distance | append: ' more item(s)'
endcapture
endif
else
assign distance_label = 'You have reached the highest discount tier'
endif
# Append the discount name and distance to the discount_distances array
assign discount_info = discount.discountTitle | append: ': ' | append: distance_label
assign discount_distances = discount_distances | append: discount_info | append: '||'
endfor
# Remove the trailing separator
assign discount_distances = discount_distances | split: '||'
echo discount_distances
%}
{% liquid
assign discounts = shop.metafields.discount_kit.campaign.value.discounts
assign current_date = 'now' | date: '%s' | times: 1
assign matched_config_id = null
for discount in discounts
assign config = discount.discountNode.config.value
assign starts_at = discount.startsAt | date: '%s' | times: 1
assign ends_at = discount.endsAt | date: '%s' | times: 1
if discount.discountType != discount_type
continue
endif
if prerequisite_type
assign first_tier = config.tiers | first
if first_tier.buys.type != prerequisite_type
continue
endif
endif
if ends_at > 0 and ends_at <= current_date
continue
endif
if current_date <= starts_at
continue
endif
if config.matchRule.excludeSale
if product.compare_at_price > 0
continue
endif
endif
if config.allowB2b != true
if customer.b2b?
continue
endif
endif
if config.customerTags
assign matching_tag = false
for tag in customer.tags
for customer_tag in config.customerTags
if tag == customer_tag
assign matching_tag = true
break
endif
endfor
endfor
if matching_tag != true
continue
endif
endif
if config.marketHandles
assign matching_market = false
for market in config.marketHandles
if market == localization.market.handle
assign matching_market = true
break
endif
endfor
if matching_market != true
continue
endif
endif
if config.currencyCode
if config.currencyCode != cart.currency.iso_code
continue
endif
endif
if config.matchRule == blank
assign matched_config_id = discount.id
break
endif
if config.matchRule.all == true
assign matched_config_id = discount.id
break
endif
if config.matchRule.type == 'collection'
for col in config.matchRule.include
assign product_collections = product.collections | map: 'id'
assign col_num = col | times: 1
if product_collections contains col_num
assign matched_config_id = discount.id
break
endif
endfor
endif
if config.matchRule.type == 'product'
for product_id in config.matchRule.include
assign product_id_num = product_id | times: 1
if product.id == product_id_num
assign matched_config_id = discount.id
break
endif
endfor
endif
if config.matchRule.type == 'productSelection'
for product_selection in config.matchRule.selection
assign product_id_num = product_selection.id | times: 1
if product.id == product_id_num
assign matched_config_id = discount.id
break
endif
endfor
endif
endfor
if matched_config_id
echo matched_config_id
endif
%}
{% assign discounts = shop.metafields.discount_kit.campaign.value.discounts %}
{% capture matching_discount_id %}
{% render 'match_discount', discount_type: 'PRODUCT_VOLUME' %}
{% endcapture %}
{% assign stripped_discount_id = matching_discount_id | strip | strip_newlines | strip_html %}
{% assign discount = discounts | where: 'id', stripped_discount_id | first %}
{{ discount }}
@belevels
Copy link

current_tier is not used

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment