Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ query DeliveryInput {
cart {
deliveryGroups {
id
deliveryOptions {
deliveryMethodType
}
}
}
discount {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{%- if flavor contains "vanilla-js" -%}
import {
DeliveryDiscountSelectionStrategy,
DeliveryMethod,
DiscountClass,
} from "../generated/api";

Expand All @@ -15,11 +16,6 @@ import {
*/

export function cartDeliveryOptionsDiscountsGenerateRun(input) {
const firstDeliveryGroup = input.cart.deliveryGroups[0];
if (!firstDeliveryGroup) {
throw new Error("No delivery groups found");
}

const hasShippingDiscountClass = input.discount.discountClasses.includes(
DiscountClass.Shipping,
);
Expand All @@ -28,20 +24,29 @@ export function cartDeliveryOptionsDiscountsGenerateRun(input) {
return {operations: []};
}

// Discount only the groups with shipping options.
const targets = input.cart.deliveryGroups
// If you also want to apply a discount for other delivery methods (e.g. LOCAL), ensure to account for that here.
.filter(({deliveryOptions}) =>
deliveryOptions.some(
({deliveryMethodType}) =>
deliveryMethodType === DeliveryMethod.Shipping,
),
)
.map(({id}) => ({deliveryGroup: {id}}));

if (targets.length === 0) {
return {operations: []};
}

return {
operations: [
{
deliveryDiscountsAdd: {
candidates: [
{
message: "FREE DELIVERY",
targets: [
{
deliveryGroup: {
id: firstDeliveryGroup.id,
},
},
],
targets,
value: {
percentage: {
value: 100,
Expand All @@ -58,6 +63,7 @@ export function cartDeliveryOptionsDiscountsGenerateRun(input) {
{%- elsif flavor contains "typescript" -%}
import {
DeliveryDiscountSelectionStrategy,
DeliveryMethod,
DiscountClass,
DeliveryInput,
CartDeliveryOptionsDiscountsGenerateRunResult,
Expand All @@ -66,11 +72,6 @@ import {
export function cartDeliveryOptionsDiscountsGenerateRun(
input: DeliveryInput,
): CartDeliveryOptionsDiscountsGenerateRunResult {
const firstDeliveryGroup = input.cart.deliveryGroups[0];
if (!firstDeliveryGroup) {
throw new Error("No delivery groups found");
}

const hasShippingDiscountClass = input.discount.discountClasses.includes(
DiscountClass.Shipping,
);
Expand All @@ -79,20 +80,29 @@ export function cartDeliveryOptionsDiscountsGenerateRun(
return {operations: []};
}

// Discount only the groups with shipping options.
const targets = input.cart.deliveryGroups
// If you also want to apply a discount for other delivery methods (e.g. LOCAL), ensure to account for that here.
.filter(({deliveryOptions}) =>
deliveryOptions.some(
({deliveryMethodType}) =>
deliveryMethodType === DeliveryMethod.Shipping,
),
)
.map(({id}) => ({deliveryGroup: {id}}));

if (targets.length === 0) {
return {operations: []};
}

return {
operations: [
{
deliveryDiscountsAdd: {
candidates: [
{
message: "FREE DELIVERY",
targets: [
{
deliveryGroup: {
id: firstDeliveryGroup.id,
},
},
],
targets,
value: {
percentage: {
value: 100,
Expand Down
Loading
Loading