Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add PostCartAmountIncDiscCodeQualifier #108

Merged
merged 1 commit into from
Jun 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions ruby_scripts/lineItem/post_cart_amount_inc_disc_code_qualifier.rb
Original file line number Diff line number Diff line change
@@ -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
20 changes: 18 additions & 2 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"];
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because of the new inheritance from PostCartAmountQualifier, I needed to make sure the classes were declared before inheriting from them.

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 {
Expand All @@ -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);
Expand Down Expand Up @@ -427,6 +442,7 @@ ${INDENT[this.IL]})`;
if (classesUsed.indexOf(inheritsFrom) === -1) {
classesUsed.push(inheritsFrom);
}
addUsedClass(inheritsFrom, allClasses);
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Didn't have second level inheritance accounted for (ie: NewestThing > Thing and then Thing > OldestThing), but this covers that.

}

if (classesUsed.indexOf(className) === -1) {
Expand Down
64 changes: 64 additions & 0 deletions src/scripts/lineItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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",
Expand Down