0

We are using Sitecore 9.0.2 with SXA 1.8

I have a product detail page with a 'Register Interest' link. On click, it should open a form to fill user details which can be pre-populated if logged in. On submit of the form, the system should send an email to an internal group with the product details and the user information.

Can I achieve this functionality OOTB? If yes, how do I pass the product information to the form from the product details page? Or should I build this functionality as a custom MVC form?

What is the best way to achieve this functionality?

1 Answer 1

1

No and no. You will not be able to do this with ootb Forms. But you also do not need to build the whole form custom.

What you need is Forms with a few customizations..

First of all you'll need a solution for the mails. You can check the Forms Extensions from my colleague Bart for that. That has a submit action that can send emails with the form data (to a fixed address if needed). It also includes some options to pre-populate fields (although you might want to use the 9.1 version for that).

Secondly you'll need to be able to pass the data. Therefor I would create a custom field. There is official documentation on this subject, and you could also check the code in the above mentioned forms extensions as those also have custom fields (e.g. hidden field). This custom field should read the necessary data (might use the querystring for that - add the product id as a query parameter to the link from your product page) and put that in the field.

The field can be a hidden input field if you don't want to show any output or you could also display product information on the form as it doesn't even have to be an input field (that's what I would do). Once you put that field on the form, the data is added to the form and will be saved and available in the email data.

Example:

public class ProductModel : InputViewModel<string>, IBindingSettings
{
    public ProductModel()
    {
        ...
    }

    public string ProductTitle { get; set; }
    public string ProductId { get; set; }

    public override string Value => $"{ProductId} - {ProductTitle}";

    protected override void InitItemProperties(Item item)
    {
        base.InitItemProperties(item);
        this.InitBindingSettingsProperties(item);
        if (Guid.TryParse(WebUtil.GetSafeQueryString("product"), out Guid productId))
        {
            var product = ...;  // get product with the id
            if (product != null)
            {
                ProductTitle = ...;
                ProductId = productId;
                ...
                }
            }
        }
    }

    protected override void UpdateItemFields(Item item)
    {
        base.UpdateItemFields(item);
        this.UpdateBindingSettingsFields(item);
    }

and the view for the field:

@using Feature.FormsExtensions.Views
@using Sitecore.Mvc
@model ProductModel

@if (Html.Sitecore().IsExperienceFormsEditMode())
{
    <h3 class="form-title">
        <div>
            Product: @Model.ProductTitle
        </div>
    </h3>
}
else
{
    <h3 class="form-title">
        <div>
            Product:
            <span class="small">
                @Model.Product
            </span>
        </div>
        ...
    </h3>
}

Note that this is just example code.. might have typo's and such ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.