Have your say in Community Polls: What was/is your greatest motivation to start your own business?

Re: Add Order Line Item - Dynamic Quantity

Solved

How can I dynamically adjust packaging quantity with product order quantity?

MuhammedF
Tourist
4 0 1

Dear Shopify members,


I have a question on setting up a flow to add specific packaging item on four of my main products. Each main product has separate packaging item as well.

Below is the logic i use to set up the flow.

If customer buy Product A -> Add Packaging A
If customer buy Product B -> Add Packaging B
If customer buy Product C -> Add Packaging C
If customer buy Product D -> Add Packaging D

We can easily set up the flow for above using 4 IF conditions.


But my question is if a customer buys more than 1 quantity of a product, how can i configure the quantity field in the "add order line item" to add corresponding quantity of packaging item.
Suppose, if custome buys 3pcs of Product A and 2pcs of Product B, the flow has to add 3pcs of Packaging A and 2pcs of Packaging B.

MuhammedF_0-1698847142213.png


How is this possible ? Any Idea anyone ?

Accepted Solutions (2)
paul_n
Shopify Staff
1431 157 332

This is an accepted solution.

That is checking all lineItems on the order for each lineltem. You need to check the current lineitem only in that condition. When you add a condition do not select order first and instead look for the variable that has line item in the title. 

 

Here's what it looks like: 

Paul_N | Flow Product Manager @ Shopify
- Finding Flow useful? Leave us a review
- Need Flow help? Check out our help docs.
- Building for Flow? Check out Flow's dev docs.

View solution in original post

BallstadADM
Visitor
2 1 2

This is an accepted solution.

Hi, i have been strugeling with this issue also and i could not manage to use your solution. But now with "write Code" it was very easy

 

This was the setup

Dynamic quantity.jpg

 

Then by addin "run Code" i could take the quantity of the spesific SKU as a variable in the "add order line"

Dynamic quantity code.jpg

input

{
order {
lineItems {
sku
quantity
}
}
}

Output

type Output {
totalQuantity: Int!
}

Write code

export default function main(input) {
let totalQuantity = 0;
input.order.lineItems.forEach(item => {
if (item.sku === '90009') {
totalQuantity += item.quantity;
}
});
return {
totalQuantity: totalQuantity,
};
}

 

And here is the add order line

Dynamic quantity add line.jpg

Worked like a charm

View solution in original post

Replies 6 (6)

paul_n
Shopify Staff
1431 157 332

It might be easier to use a For Each loop on the `order / lineItems` so that each "Add order line item" is working with a single lineItem. Something like:

01-31-3mv9i-96tj6

 

You could them write some liquid that would add the current quantity to that quantity field. Something like:

{{lineItemsForeachitem.currentQuantity}}

 

Paul_N | Flow Product Manager @ Shopify
- Finding Flow useful? Leave us a review
- Need Flow help? Check out our help docs.
- Building for Flow? Check out Flow's dev docs.
MuhammedF
Tourist
4 0 1

Hi @paul_n 

 

Thankyou for your reply. I used this logic to update the flows.
Its working partially but has issues.
The packaging item is getting added for every line item in the order (since we have a for loop). I wanted to have the packaging item added only for the eligible items, not for every item in the order.

To make it clear, we have only 4 products that have corresponding packaging items, out of the total of say 50 items in our inventory.

MuhammedF_0-1698907993107.png

 

Would you be able to help ? Thanks in advance

paul_n
Shopify Staff
1431 157 332

If you have a condition in there to check the SKU that should make it so the action is only called for some line items. If it's being called for all of them, your condition is likely not designed correctly. I'd need to see the detail instead of the overview.

Paul_N | Flow Product Manager @ Shopify
- Finding Flow useful? Leave us a review
- Need Flow help? Check out our help docs.
- Building for Flow? Check out Flow's dev docs.
MuhammedF
Tourist
4 0 1

Hi @paul_n 

Below is a screenshot of conditions used:-

MuhammedF_0-1698927568634.png

 

MuhammedF_1-1698927730295.png


Would you be able to help please ?

paul_n
Shopify Staff
1431 157 332

This is an accepted solution.

That is checking all lineItems on the order for each lineltem. You need to check the current lineitem only in that condition. When you add a condition do not select order first and instead look for the variable that has line item in the title. 

 

Here's what it looks like: 

Paul_N | Flow Product Manager @ Shopify
- Finding Flow useful? Leave us a review
- Need Flow help? Check out our help docs.
- Building for Flow? Check out Flow's dev docs.

BallstadADM
Visitor
2 1 2

This is an accepted solution.

Hi, i have been strugeling with this issue also and i could not manage to use your solution. But now with "write Code" it was very easy

 

This was the setup

Dynamic quantity.jpg

 

Then by addin "run Code" i could take the quantity of the spesific SKU as a variable in the "add order line"

Dynamic quantity code.jpg

input

{
order {
lineItems {
sku
quantity
}
}
}

Output

type Output {
totalQuantity: Int!
}

Write code

export default function main(input) {
let totalQuantity = 0;
input.order.lineItems.forEach(item => {
if (item.sku === '90009') {
totalQuantity += item.quantity;
}
});
return {
totalQuantity: totalQuantity,
};
}

 

And here is the add order line

Dynamic quantity add line.jpg

Worked like a charm