Senior Salesforce Developer
Selling Gift Certificates on Salesforce Commerce Cloud using SFRA
One method to generate Gift Certificates using the SFRA framework
Salesforce Commerce Cloud's platform has built in Gift Certificate functionality, however their most updated codebase, SFRA, does not leverage this to allow a business to generate gift certificates out of the box. This blog will help show one of many methods to enable this functionality.
The most recent version of Salesforce Commerce Cloud’s (SFCC) Storefront Reference Architecture (SFRA) does not come with the built-in ability to purchase Gift Certificates in the code. However, with a little work we can use the built-in classes for Gift Certificate creation and allow our users to place orders for Gift Certificates right from the website.
For this project, there are a few requirements from the client that we need to take into account that are not part of the standard “Gift Certificate” bundle. The client would like their customers to be able to purchase “themed” Gift Certificates. The “theme” would trigger a custom themed email to be sent from Marketing Cloud, and would alter the image of the Gift Certificate that is used across the product detail page (PDP), cart, checkout, and order confirmation. The client also only wants to offer the Gift Certificate in predetermined amounts, so the customer can only select from the amounts offered and not create, say, a $1 Gift Certificate. Finally, the client needs to create two different kinds of Gift Certificates: one that is purchased on the front end and another that is generated for loyalty members. Each Gift Certificate will have a different prefix added to the ID.
Our first step to meet these requirements is creating a “Gift Certificate” product with variations for both price and theme. This allows the client to set separate pricing for each variant product, as well as add new pricing and themes as requirements change. Setting up the Gift Certificate product this way also allows for standard product management - the client can update images, inventory, descriptions, and other metadata just like any other product.
The “Gift Certificate” product we created must be assigned to the storefront catalog assigned to the site being used. Don’t forget this step, as otherwise the product will not be purchasable. Each variant product also needs to have pricing and inventory assigned before we can continue. The System Object “ProductLineItem” needs 3 new custom attributes assigned to it in order to support the Gift Certificate data. Attributes for the Recipient Name, Recipient Email, and Message all need to be added to the object as strings.
Make sure shipping and tax calculations are set up appropriately for this product as well. For this client, a shipping exception will be made for this product in every shipping method, and the product attribute for taxes is set to “taxfree”, so no taxes are calculated on this price.
In the Site Preferences, a few new options for Gift Certificate management should be added. First would be an input to let the system know which product ID the “Gift Certificate” should be tied to. This will come in use later during the order placement process. There should be two additional inputs added here: one for the purchased Gift Certificate prefix and another for the loyalty Gift Certificate prefix.
As a last step, before we move to code, the System Objects for both GiftCertificate and GiftCertificateLineItem need to be updated to capture the custom theme information that is being passed. For both of these objects, adding a new attribute called “GCTheme” with a “string” type will suffice.
Once the product is added to the cart, it’s treated as a normal product all the way through to the “CheckoutServices-PlaceOrder” controller. Just before the order is created, a function will be run that converts each “Gift Certificate” product line item to a gift card line item. To do this, the code loops through each line item in the basket, and check if the ID matches the Gift Certificate ID set in our Site Preferences. If there is a match, the code gets the quantity for that product line item and creates a matching gift certificate line item for each. EX: a customer added three $20 Birthday gift certificates to their cart, we will create 3 separate gift certificate line item objects and add each to the cart separately.
During creation, the gift certificate line item is created using the product price and the recipient email. The internal functions to add the message, name, and validate the email are used as-is from the custom information on the product line item. The theme and ID are added to the ID section for the time being. After creation, the original product line item is removed from the cart. Finally, the gift certificate line items are each looked at to set the price value explicitly.
createGiftCertificateLineItem function example:
When the final order is placed, the gift card line items are finalized in the order.
The orders are then sent via XML to the Order Management System (OMS). When the payment is approved and authorized, the order and all line items are sent back in an approval XML for processing. During processing, if the script comes across a gift certificate line item a function runs to create the final Gift Certificates at that time. During creation, the ID that held both the number code and theme are split apart, with the code being applied to the ID only and the theme being applied to a custom attribute on the Gift Certificate. Once the Gift Certificate is created and finalized, an email is triggered through an SFMC Trigger to send the Gift Certificate to the recipient email address.
The same finalize function is used to create Loyalty Reward Gift Certificates - the type passed through for those is set to “Rewards”, whereas the type passed through for purchased certificates is “Purchased”.
finalizeGiftCertificate function example:
After these processes, the Gift Certificates are successfully available to be viewed in the Business Manager, and are ready for use as a payment method during checkout. My next post will detail how to allow for Gift Certificates as a payment method in SFRA checkout.