Building a Wishlist Feature with Product Collections
Published by DJ Steinmetz on October 11, 2022
Prerequisites
To make use of the Product Collections resource, we need:
An active buyer user
Fully orderable products visible to the buyer user
Assumptions
For this tutorial, we are using the URL for the sandbox environment in the region US-West.
We have a product with an ID of
enterprise-product-id
that is orderable and visible to our buyer user.
Create a new Product Collection to represent our Wishlist
The first thing we need to do, is create the Product Collection itself. Let's name it "Wishlist" giving it a self-descriptive ID.
1POST https://sandboxapi.ordercloud.io/v1/me/productcollections HTTP/1.12Authorization: Bearer INSERT_ACCESS_TOKEN_HERE3Content-Type: application/json; charset=UTF-845{6 "ID": "wishlist",7 "Name": "Wishlist",8}
1import { Me } from "ordercloud-javascript-sdk";23Me.CreateProductCollection({4 ID: "wishlist",5 Name: "Wishlist",6})7.then((productCollection) => {8 console.log(productCollection);9})10.catch((ex) => console.log(ex));
1import { Me, OrderCloudError, ProductCollection } from "ordercloud-javascript-sdk";23Me.CreateProductCollection({4 ID: "wishlist",5 Name: "Wishlist",6})7.then((productCollection: ProductCollection) => {8 console.log(productCollection);9})10.catch((ex: OrderCloudError) => console.log(ex));
1using OrderCloud.SDK;23var productCollection = new ProductCollection4{5 ID = "wishlist",6 Name = "Wishlist",7};8try {9 ProductCollection response = await Me.CreateProductCollection(data);10 Console.WriteLine(response)11} catch(OrderCloudException ex) {12 Console.WriteLine(ex.Message);13}
Add a product to our Wishlist
Adding an entry to our Wishlist Product Collection is as easy as calling PUT /me/productcollections/{productCollectionID}/{productID}
with our Product Collection ID and the product ID we want to add to our collection. If successful, our call should return a 204 No Content
response.
1PUT https://sandboxapi.ordercloud.io/v1/me/productcollections/wishlist/enterprise-product-id HTTP/1.12Authorization: Bearer INSERT_ACCESS_TOKEN_HERE3Content-Type: application/json; charset=UTF-8
1import { Me } from "ordercloud-javascript-sdk";23Me.CreateProductCollectionEntry("wishlist", "enterprise-product-id")4.then(() => {5 console.log("Successfully added a product to your wishlist!");6})7.catch((ex) => console.log(ex));
1import { Me, OrderCloudError } from "ordercloud-javascript-sdk";23Me.CreateProductCollectionEntry("wishlist", "enterprise-product-id")4.then(() => {5 console.log("Successfully added a product to your wishlist!");6})7.catch((ex: OrderCloudError) => console.log(ex));
1using OrderCloud.SDK;23try {4 await Me.CreateProductCollectionEntry("wishlist", "enterprise-product-id");5 Console.WriteLine("Successfully added a product to your wishlist!")6} catch(OrderCloudException ex) {7 Console.WriteLine(ex.Message);8}
List products in our Wishlist
Now that we have our Wishlist Product Collection, and have added a product to it, let's list out the products in our Product Collection to see our Wishlist in action. We should see a list page containing one product (enterprise-product-id
) returned from the list call.
1GET https://sandboxapi.ordercloud.io/v1/me/productcollections/wishlist/products HTTP/1.12Authorization: Bearer INSERT_ACCESS_TOKEN_HERE3Content-Type: application/json; charset=UTF-8
1import { Me } from "ordercloud-javascript-sdk";23Me.ListProductCollectionEntries("wishlist")4.then((wishlist) => {5 console.log(wishlist.Items);6})7.catch((ex) => console.log(ex));
1import { Me, OrderCloudError } from "ordercloud-javascript-sdk";23Me.ListProductCollectionEntries("wishlist", "enterprise-product-id")4.then((wishlist: ListPageWithFacets<BuyerProduct>) => {5 console.log(wishlist.Items);6})7.catch((ex: OrderCloudError) => console.log(ex));
1using OrderCloud.SDK;23try {4 ListPageWithFacets<BuyerProduct> response = await Me.ListProductCollectionEntries("wishlist", "enterprise-product-id");5 Console.WriteLine(response.Items)6} catch(OrderCloudException ex) {7 Console.WriteLine(ex.Message);8}
Remove a product from our Wishlist
We may end up changing our mind, and wanting to remove products from our Wishlist. To remove a product from our Wishlist, we will call DELETE /me/productcollections/{productCollectionID}/{productID}
.
1DELETE https://sandboxapi.ordercloud.io/v1/me/productcollections/wishlist/enterprise-product-id HTTP/1.12Authorization: Bearer INSERT_ACCESS_TOKEN_HERE3Content-Type: application/json; charset=UTF-8
1import { Me } from "ordercloud-javascript-sdk";23Me.DeleteProductCollectionEntry("wishlist", "enterprise-product-id")4.then(() => {5 console.log("Successfully removed a product from your wishlist.");6})7.catch((ex) => console.log(ex));
1import { Me, OrderCloudError } from "ordercloud-javascript-sdk";23Me.DeleteProductCollectionEntry("wishlist", "enterprise-product-id")4.then(() => {5 console.log("Successfully removed a product from your wishlist.");6})7.catch((ex: OrderCloudError) => console.log(ex));
1using OrderCloud.SDK;23try {4 await Me.DeleteProductCollectionEntry("wishlist", "enterprise-product-id");5 Console.WriteLine("Successfully removed a product from your wishlist.")6} catch(OrderCloudException ex) {7 Console.WriteLine(ex.Message);8}
Implement your new Wishlist feature
That's it! It's that simple. Product Collections make building wishlist-like features for B2X commerce easy. Now that you have a basic understanding of how Product Collections work, you can take this knowledge and build a UI in your buyer application for buyer users to create and manage their wishlists.
Considerations
This implementation is one of the many permutations of the Product Collections resource. There are many ways to utilize Product Collections and a "wishlist" feature is just one of them. A few more example features that could make use of Product Collections are "Save for later" or "Favorites".
Product Collections are:
only usable by buyer users.
currently only visible to the buyer user who created them.
limited to 500 products per collection. There is no limit on the number of Product Collections a buyer user can have.
If a buyer user adds a product to a Product Collection and visibility between the product and buyer user is revoked, the product will not be returned from
GET /me/productcollections/{productCollectionID}/products
. If visibility is restored, the product will begin returning from the aforementioned call once again.
Check out the key highlights from the Knowledge Base article introducing Product Collections for more context.
Still have questions?
Ask in our Community Channel