DOPP API: Examples
This guide provides practical examples for using the DOPP API to manage and customize discount flows on your store. Each example covers a real-world scenario, helping you understand how to implement features like overriding cart items, specifying product collections, and reloading discounts in dynamic situations such as variant changes or infinite scrolling.
Override Cart Items for Bundle Discounts
Specify Product Collections for Collection-Based Discounts
Simulate Customer Tags for Loyalty Discounts
Trigger Discount Reload on Variant Change for Product Page Updates
Refresh Collection Page Discounts on New Items or Infinite Scroll
Scenario: You want to calculate discounts based on a custom cart setup, such as a product bundle, to see how discounts apply before the items are actually in the cart.
Scenario: If an existing discount is only applied when products belong to specific collections (like a discount limited to a "Clearance" collection), specify collectionIds to indicate the collections the product belongs to. This lets the API consider any collection-based discount conditions.
Scenario: If you offer discounts to specific customer groups, such as VIPs or loyal customers, simulate those tags to test discount calculations without requiring the customer to log in.
Scenario: If your theme has complex behavior around variant selection, trigger a discount recalculation by dispatching a regios-dopp:variant-change event. This ensures that discounts update correctly as variants change.
Scenario: For collection pages with a “Load More” button or infinite scroll, use the regios-dopp:collection-page:new-items event to trigger discount recalculations as new items load. This keeps the discount data accurate for all visible products.
This guide ensures you can effectively use the DOPP API for dynamic discount scenarios on your store.
Your feedback helps us improve. Could you please take a moment to leave a review on the Shopify App Store? Thank you!
Example Scenarios
Override Cart Items for Bundle Discounts
Specify Product Collections for Collection-Based Discounts
Simulate Customer Tags for Loyalty Discounts
Trigger Discount Reload on Variant Change for Product Page Updates
Refresh Collection Page Discounts on New Items or Infinite Scroll
API Use Case Examples
Override Cart Items for Bundle Discounts
Scenario: You want to calculate discounts based on a custom cart setup, such as a product bundle, to see how discounts apply before the items are actually in the cart.
const overriddenCartLines = [
{ productId: 111, variantId: 222, regularPriceInCents: 1500, quantity: 2 },
{ productId: 333, variantId: 444, regularPriceInCents: 2500, quantity: 1 }
];
try {
const result = await RegiosDOPP.api.v0?.calculateDiscountedPrices({
productId: 123,
variantId: 456,
regularPriceInCents: 1000,
cartLines: overriddenCartLines
});
console.log("Discounted Price with Override:", result);
} catch (error) {
console.error("Error:", error);
}
Specify Product Collections for Collection-Based Discounts
Scenario: If an existing discount is only applied when products belong to specific collections (like a discount limited to a "Clearance" collection), specify collectionIds to indicate the collections the product belongs to. This lets the API consider any collection-based discount conditions.
const product = {
productId: 123,
variantId: 456,
regularPriceInCents: 1000,
collectionIds: [101, 202, 303] // IDs of relevant collections
};
try {
const result = await RegiosDOPP.api.v0?.calculateDiscountedPrices(product);
console.log("Discounted Price with Collections:", result);
} catch (error) {
console.error("Error:", error);
}
Simulate Customer Tags for Loyalty Discounts
Scenario: If you offer discounts to specific customer groups, such as VIPs or loyal customers, simulate those tags to test discount calculations without requiring the customer to log in.
const simulatedCustomer = {
id: 789,
tags: ["VIP", "LoyalCustomer"]
};
try {
const result = await RegiosDOPP.api.v0?.calculateDiscountedPrices({
productId: 123,
variantId: 456,
regularPriceInCents: 1000,
customer: simulatedCustomer
});
console.log("Discounted Price with Customer Tags:", result);
} catch (error) {
console.error("Error:", error);
}
Trigger Discount Reload on Variant Change for Product Page Updates
Scenario: If your theme has complex behavior around variant selection, trigger a discount recalculation by dispatching a regios-dopp:variant-change event. This ensures that discounts update correctly as variants change.
window.dispatchEvent(new CustomEvent("regios-dopp:variant-change", {
detail: {
selectedVariant: {
id: 123456789, // New variant ID
price: 3000 // Price of the new variant in cents
},
quantity: 1 // Quantity of the selected variant
}
}));
Refresh Collection Page Discounts on New Items or Infinite Scroll
Scenario: For collection pages with a “Load More” button or infinite scroll, use the regios-dopp:collection-page:new-items event to trigger discount recalculations as new items load. This keeps the discount data accurate for all visible products.
function refreshDiscountsOnNewItems(newItems) {
window.dispatchEvent(new CustomEvent("regios-dopp:collection-page:new-items", {
detail: { uniqueKey: 'default', newItems }
}));
}
// Example usage: call `refreshDiscountsOnNewItems` when new items are fetched
await refreshDiscountsOnNewItems([
{ productId: 555, variantId: 666, regularPriceInCents: 2000 },
{ productId: 777, variantId: 888, regularPriceInCents: 3000 }
]);
This guide ensures you can effectively use the DOPP API for dynamic discount scenarios on your store.
Your Feedback Matters
Your feedback helps us improve. Could you please take a moment to leave a review on the Shopify App Store? Thank you!
Updated on: 03/11/2024
Thank you!