use Saved Products Actions
The useSavedProductsActions hook provides mutation functions to save and unsave products (favorites). Returns saveProduct() and unsaveProduct() functions that accept product IDs and variant information. Saving a product adds it to the user's products collection in the Shop app. Use with useSavedProducts() to display current saved status and implement optimistic UI updates.
Caution
This hook requires adding the following scopes to the manifest file:
product_list:write
For more details, see manifest.json.
Caution:
This hook requires adding the following scopes to the manifest file:
product_list:write
For more details, see manifest.json.
Anchor to useSavedProductsActionsuse Saved Products Actions()
use Saved Products Actions()
UseSavedProductsActionsReturnsUseSavedProductsActionsReturns
UseSavedProductsActionsReturns
- saveProduct
Save a product.
(params: FavoriteParams) => Promise<void> - unsaveProduct
Unsave a product.
(params: UnfavoriteParams) => Promise<void>
FavoriteParams
- productId
string - productVariantId
string - shopId
string
UnfavoriteParams
- productId
string - productVariantId
string - shopId
string
Examples
tsx
import {useSavedProductsActions, Button} from '@shopify/shop-minis-react'
export default function MyComponent() {
const {saveProduct, unsaveProduct} = useSavedProductsActions()
return (
<>
<Button
onClick={() =>
saveProduct({
productId: 'gid://shopify/Product/123',
productVariantId: 'gid://shopify/ProductVariant/456',
shopId: 'gid://shopify/Shop/42',
})
}
>
Save product
</Button>
<Button
onClick={() =>
unsaveProduct({
productId: 'gid://shopify/Product/123',
productVariantId: 'gid://shopify/ProductVariant/456',
shopId: 'gid://shopify/Shop/42',
})
}
>
Unsave product
</Button>
</>
)
}
Examples
Example code
tsx
import {useSavedProductsActions, Button} from '@shopify/shop-minis-react' export default function MyComponent() { const {saveProduct, unsaveProduct} = useSavedProductsActions() return ( <> <Button onClick={() => saveProduct({ productId: 'gid://shopify/Product/123', productVariantId: 'gid://shopify/ProductVariant/456', shopId: 'gid://shopify/Shop/42', }) } > Save product </Button> <Button onClick={() => unsaveProduct({ productId: 'gid://shopify/Product/123', productVariantId: 'gid://shopify/ProductVariant/456', shopId: 'gid://shopify/Shop/42', }) } > Unsave product </Button> </> ) }
Was this page helpful?