Personalized checkout and custom promotions with Shopify Scripts
Hi.
I have Shopify script for volume discounts (quantity >= 7 - 10%,...).
The script checks the line item quantity. All works.
But I've created product bundles and the same products in the cart with different properties are displayed as different line items.
One product can have a quantity 4 in line item row. And the same product can have quantity 3 in another row...
Shopify script count only line item quantity.
How can I count number unique products (ids)?
Then it will be possible to add a discount for those IDs, the total number of which satisfies the discount.
Solved! Go to the solution
This is an accepted solution.
You might have to use something like this solution here:
Retrieve Line Items: When processing an order, retrieve all line items from the order.
Extract Variant IDs: For each line item, extract the variant ID. The variant ID can be found using the .variant method in the Shopify Script API, which returns the specific product variant represented by the line item.
Track Unique Products: Create a data structure (like a set or dictionary) to track unique products. As you iterate through the line items, check if the variant ID is already in your data structure. If not, add it. This way, you can count unique products.
Apply Discounts Based on Unique Counts: Once you have the count of unique products, you can apply your volume discount logic based on this count.
This is an accepted solution.
You might have to use something like this solution here:
Retrieve Line Items: When processing an order, retrieve all line items from the order.
Extract Variant IDs: For each line item, extract the variant ID. The variant ID can be found using the .variant method in the Shopify Script API, which returns the specific product variant represented by the line item.
Track Unique Products: Create a data structure (like a set or dictionary) to track unique products. As you iterate through the line items, check if the variant ID is already in your data structure. If not, add it. This way, you can count unique products.
Apply Discounts Based on Unique Counts: Once you have the count of unique products, you can apply your volume discount logic based on this count.
Thank You. Works for me!
DISCOUNT_MIN7 = 'discount_min7'
DISCOUNT_MIN10 = 'discount_min10'
DISCOUNT_MIN20 = 'discount_min20'
MIN7_VALUE = 0
MIN10_VALUE = 0
MIN20_VALUE = 0
counts = Hash.new(0)
Input.cart.line_items.each do |line_item|
counts[line_item.variant.id] += line_item.quantity
end
Input.cart.line_items.each do |line_item|
properties = line_item.properties
variant = line_item.variant.id
variant_qty = counts.fetch(variant).to_f
# puts variant_qty
# puts counts.fetch(variant)
if line_item.line_price === line_item.original_line_price
if variant_qty >= 7 && variant_qty < 10 && properties.has_key?(DISCOUNT_MIN7)
MIN7_VALUE = properties[DISCOUNT_MIN7].to_f
line_item.change_line_price(line_item.line_price - line_item.line_price * (MIN7_VALUE / 100), message: "#{MIN7_VALUE}% VOLUME_DISCOUNT" )
elsif variant_qty >= 10 && variant_qty < 20 && properties.has_key?(DISCOUNT_MIN10)
MIN10_VALUE = properties[DISCOUNT_MIN10].to_f
line_item.change_line_price(line_item.line_price - line_item.line_price * (MIN10_VALUE / 100), message: "#{MIN10_VALUE}% VOLUME_DISCOUNT")
elsif variant_qty >= 20 && properties.has_key?(DISCOUNT_MIN20)
MIN20_VALUE = properties[DISCOUNT_MIN20].to_f
line_item.change_line_price(line_item.line_price - line_item.line_price * (MIN20_VALUE / 100), message: "#{MIN20_VALUE}% VOLUME_DISCOUNT")
end
end
end
Output.cart = Input.cart
Awesome, Glad I could help! Thanks for posting your code for others in this situation!
Hey Community! As the holiday season unfolds, we want to extend heartfelt thanks to a...
By JasonH Dec 6, 2024Dropshipping, a high-growth, $226 billion-dollar industry, remains a highly dynamic bus...
By JasonH Nov 27, 2024Hey Community! It’s time to share some appreciation and celebrate what we have accomplis...
By JasonH Nov 14, 2024