diff --git a/ruby_scripts/lineItem/post_cart_amount_inc_disc_code_qualifier.rb b/ruby_scripts/lineItem/post_cart_amount_inc_disc_code_qualifier.rb new file mode 100644 index 0000000..bd91027 --- /dev/null +++ b/ruby_scripts/lineItem/post_cart_amount_inc_disc_code_qualifier.rb @@ -0,0 +1,30 @@ +class PostCartAmountIncDiscCodeQualifier < PostCartAmountQualifier + def initialize(comparison_type, amount) + super(comparison_type, amount) + end + + def match?(cart, selector = nil) + total = + case cart.discount_code + when CartDiscount::Percentage + if cart.subtotal_price >= cart.discount_code.minimum_order_amount + cart_subtotal_without_gc = cart.line_items.reduce(Money.zero) do |total, item| + total + (item.variant.product.gift_card? ? Money.zero : item.line_price) + end + gift_card_amount = cart.subtotal_price - cart_subtotal_without_gc + cart_subtotal_without_gc * ((Decimal.new(100) - cart.discount_code.percentage) / 100) + gift_card_amount + else + cart.subtotal_price + end + when CartDiscount::FixedAmount + if cart.subtotal_price >= cart.discount_code.minimum_order_amount + [cart.subtotal_price - cart.discount_code.amount, Money.zero].max + else + cart.subtotal_price + end + else + cart.subtotal_price + end + compare_amounts(total, @comparison_type, @amount) + end +end diff --git a/src/App.js b/src/App.js index 36368b2..253617d 100644 --- a/src/App.js +++ b/src/App.js @@ -364,7 +364,22 @@ export default class App extends Component { function generateClassCode(allClasses, classesUsed) { let code = ''; - classesUsed.forEach((className) => { + + // Qualifier and PostCartAmountQualifier need to come first since they are inherited by other classes + const classNameOrder = ["Campaign", "Qualifier", "PostCartAmountQualifier", "Selector"]; + classesUsed.sort((a, b) => { + // Get the index of each class in classNameOrder + const indexA = classNameOrder.indexOf(a); + const indexB = classNameOrder.indexOf(b); + + // If a class is not found in classNameOrder, index will be -1 + // Classes not in classNameOrder should come after those that are + if (indexA === -1) return 1; + if (indexB === -1) return -1; + + // Sort based on the index in classNameOrder + return indexA - indexB; + }).forEach((className) => { if (allClasses[className]) { code += allClasses[className] + '\n'; } else { @@ -391,7 +406,7 @@ export default class App extends Component { campaign.dependants.forEach((dependant) => addUsedClass(dependant, allClasses)); } - const inputsCode = campaign.inputs.map((input, index) => { + const inputsCode = campaign.inputs.map((input) => { if (input.inputs) { this.IL++; const code = this.generateCode(input, classesUsed, allClasses); @@ -427,6 +442,7 @@ ${INDENT[this.IL]})`; if (classesUsed.indexOf(inheritsFrom) === -1) { classesUsed.push(inheritsFrom); } + addUsedClass(inheritsFrom, allClasses); } if (classesUsed.indexOf(className) === -1) { diff --git a/src/scripts/lineItem.js b/src/scripts/lineItem.js index f15e9d5..9ca8ce9 100644 --- a/src/scripts/lineItem.js +++ b/src/scripts/lineItem.js @@ -527,6 +527,38 @@ class PercentageDiscount end end`, + PostCartAmountIncDiscCodeQualifier: ` +class PostCartAmountIncDiscCodeQualifier < PostCartAmountQualifier + def initialize(comparison_type, amount) + super(comparison_type, amount) + end + + def match?(cart, selector = nil) + total = + case cart.discount_code + when CartDiscount::Percentage + if cart.subtotal_price >= cart.discount_code.minimum_order_amount + cart_subtotal_without_gc = cart.line_items.reduce(Money.zero) do |total, item| + total + (item.variant.product.gift_card? ? Money.zero : item.line_price) + end + gift_card_amount = cart.subtotal_price - cart_subtotal_without_gc + cart_subtotal_without_gc * ((Decimal.new(100) - cart.discount_code.percentage) / 100) + gift_card_amount + else + cart.subtotal_price + end + when CartDiscount::FixedAmount + if cart.subtotal_price >= cart.discount_code.minimum_order_amount + [cart.subtotal_price - cart.discount_code.amount, Money.zero].max + else + cart.subtotal_price + end + else + cart.subtotal_price + end + compare_amounts(total, @comparison_type, @amount) + end +end`, + PostCartAmountQualifier: ` class PostCartAmountQualifier < Qualifier def initialize(comparison_type, amount) @@ -744,6 +776,38 @@ const CART_QUALIFIERS = [ } } }, + { + value: "PostCartAmountIncDiscCodeQualifier", + label: "Discounted Cart Subtotal (applied by discount code & scripts)", + description: "Will only apply if the cart subtotal, after applying the campaign and discount code, meets conditions", + inputs: { + condition: { + type: "select", + description: "Type of comparison", + options: [{ + value: "greater_than", + label: "Greater than" + }, + { + value: "less_than", + label: "Less than" + }, + { + value: "greater_than_or_equal", + label: "Greater than or equal to" + }, + { + value: "less_than_or_equal", + label: "Less than or equal to" + }, + ] + }, + amount: { + type: "number", + description: "Amount in dollars" + } + }, + }, { value: "ExcludeDiscountCodes", label: "Exclude Discount Codes",