shopify-script-creator icon indicating copy to clipboard operation
shopify-script-creator copied to clipboard

Feature request: use Bundle discount with most discount

Open vattujan opened this issue 2 years ago • 2 comments

Thank you for this amazing tool you created.

So, I have line-item script where there are multiple Bundle Discounts, with same or different products and percentages as discounts.

Now, when there are products that qualifies for multiple Bundle Discounts, the first BundleDiscount is applied. Is there a way to actually use BundleDiscount that gives the most amount of discounts?

vattujan avatar Feb 09 '23 10:02 vattujan

Unfortunately there isn't an automatic way to do that right now, and my first thoughts would be that this would be quite a bit of work to add (as well as likely to take up too much cpu instructions) as each discount campaign would need to be applied and then reset with the result stored, and finally the best discount selected and applied of the group.

The best way within the current version would be to sort them in the campaign list so that the best discounts would be applied first if. Downside is that only works well if it's reasonably obvious which ones would be the better deal when you are creating the script.

jgodson avatar Feb 09 '23 17:02 jgodson

Thank you for the response.

Best discount can be applied with the approach below, but of course it will be applied to all product quantity in cart and not just bundles product quantity.

And I am not sure how to incorporate this into Script created by the tool.

Just incase anyone could help:

// Define the bundle products and discount amounts
bundle_discounts = [
  {
    products: [
      {product_id: 6644206633131, quantity: 3},
      {product_id: 6644207354027, quantity: 1}
    ],
    discount_percentage: 9.0,
    message: "Discount 9%"
  },
  {
    products: [
      {product_id: 6644206633131, quantity: 3},
      {product_id: 6644207354027, quantity: 1}
    ],
    discount_percentage: 10.0,
    message: "Discount 10%"
  }
]

// Check if each bundle is in the cart and calculate the discount amount
max_discount_amount = Money.zero()
max_discount = nil
bundle_discounts.each do |bundle_discount|
  bundle_items_in_cart = true
  bundle_discount[:products].each do |bundle_product|
    unless line_item = Input.cart.line_items.detect { |item| item.variant.product.id == bundle_product[:product_id] && item.quantity >= bundle_product[:quantity] }
      bundle_items_in_cart = false
      break
    end
  end
  if bundle_items_in_cart
    discount_amount = Input.cart.subtotal_price * (bundle_discount[:discount_percentage] / 100.0)
    if discount_amount > max_discount_amount
      max_discount_amount = discount_amount
      max_discount = bundle_discount
    end
  end
end

// Apply the discount with the highest cart amount if a bundle was found
if max_discount
  Input.cart.line_items.each do |line_item|
    max_discount[:products].each do |bundle_product|
      if line_item.variant.product.id == bundle_product[:product_id]
        line_item.change_line_price(line_item.line_price * (1.0 - (max_discount[:discount_percentage] / 100.0)), message: max_discount[:message])
      end
    end
  end
end

// Return the updated cart
Output.cart = Input.cart

vattujan avatar Feb 10 '23 09:02 vattujan