Questions and discussions about using the Shopify CLI and Shopify-built libraries.
Hi, I'm using resourcePicker in a Polaris app. I have passed:
multiple: true
Using the below example from the QR code app, how would I save multiple products in one selection to setFormState? Currently it only saves and displays the first array item to state and to the page display. I can push the result into another array successfully, but can't work out how to set it the same in the state. Presumably it's the products[0] shown below. Any pointers please?
async function selectProduct() { const products = await window.shopify.resourcePicker({ type: "product",
multiple: true, action: "select", }); if (products) { const { images, id, variants, title, handle } = products[0]; setFormState({ ...formState, productId: id, productVariantId: variants[0].id, productTitle: title, productHandle: handle, productAlt: images[0]?.altText, productImage: images[0]?.originalSrc, }); } }
Solved! Go to the solution
This is an accepted solution.
Hi Johnldt,
In your code, you are only selecting the first product from the array of selected products using products[0]
. If you want to handle multiple products, you will need to iterate over the products
array and handle each product individually.
Here is an example of how you can modify your code to handle multiple products:
async function selectProduct() {
const products = await window.shopify.resourcePicker({
type: "product",
multiple: true,
action: "select",
});
if (products) {
const allSelectedProducts = products.map(product => {
const { images, id, variants, title, handle } = product;
return {
productId: id,
productVariantId: variants[0].id,
productTitle: title,
productHandle: handle,
productAlt: images[0]?.altText,
productImage: images[0]?.originalSrc,
}
});
setFormState({
...formState,
selectedProducts: allSelectedProducts
});
}
}
In this code, we are using the Array.prototype.map
function to create a new array of product objects, each containing the data for one of the selected products. Then we are updating the form state with this new array.
Please note that this will replace the entire form state with the new array of selected products. If you have other fields in your form state that you want to preserve, you will need to modify this code accordingly.
Hope this helps!
Liam | Developer Advocate @ Shopify
- Was my reply helpful? Click Like to let me know!
- Was your question answered? Mark it as an Accepted Solution
- To learn more visit Shopify.dev or the Shopify Web Design and Development Blog
This is an accepted solution.
Hi Johnldt,
In your code, you are only selecting the first product from the array of selected products using products[0]
. If you want to handle multiple products, you will need to iterate over the products
array and handle each product individually.
Here is an example of how you can modify your code to handle multiple products:
async function selectProduct() {
const products = await window.shopify.resourcePicker({
type: "product",
multiple: true,
action: "select",
});
if (products) {
const allSelectedProducts = products.map(product => {
const { images, id, variants, title, handle } = product;
return {
productId: id,
productVariantId: variants[0].id,
productTitle: title,
productHandle: handle,
productAlt: images[0]?.altText,
productImage: images[0]?.originalSrc,
}
});
setFormState({
...formState,
selectedProducts: allSelectedProducts
});
}
}
In this code, we are using the Array.prototype.map
function to create a new array of product objects, each containing the data for one of the selected products. Then we are updating the form state with this new array.
Please note that this will replace the entire form state with the new array of selected products. If you have other fields in your form state that you want to preserve, you will need to modify this code accordingly.
Hope this helps!
Liam | Developer Advocate @ Shopify
- Was my reply helpful? Click Like to let me know!
- Was your question answered? Mark it as an Accepted Solution
- To learn more visit Shopify.dev or the Shopify Web Design and Development Blog
Thanks very much Liam - appreciated. That works great 🙂 And thanks for the tip - I will need to pass more, as I'm building a GraphQL query from the bigger picture, so a nice heads-up there.
All I need is to apply the code from the same collection that I have used the above code. Also if I have multiple value collection selected then comma separated value should be inserted in the database.
I need this code in my app me dropdown field me collection selected option use me mere store jitni collection wo sahi collection honi honi show and database insert. Multiple collection should be selected, tab value should be comma separated through insert.
I need this code in my app me dropdown field me collection selected option use my store collection and this collection show my custom app backend side. save value in database. Multiple collection should be selected, tab value should be comma separated through insert.