Newsletter checkbox at checkout

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
10 years ago
Hi,

I want to implement a checkbox to subscribe to the newsletter at the last step of the checkout process. Does anyone know a simple way to do that?
10 years ago
There is no simple way to do this.

You would have to add a new property to the Nop.Web.Models.Checkout.CheckoutConfirmModel something like:

public bool SubscribeToNewsLetter { get; set; }


Then change the Presentation\Nop.Web\Views\Checkout\Confirm.cshtml view and add something like:

@Html.CheckBoxFor(model => model.SubscribeToNewsLetter)


You could add it above the terms of service:

<div class="subscribe-to-newsletter">
                        @Html.CheckBoxFor(model => model.SubscribeToNewsLetter)
                    </div>

<div class="terms-of-service">
                        <input id="termsofservice" type="checkbox" name="termsofservice" />
                        @T("Checkout.TermsOfService.IAccept")
                        <span class="read" onclick="javascript:OpenWindow('@Url.RouteUrl("TopicPopup", new { SystemName = "conditionsofuse" })', 450, 500, true)">@T("Checkout.TermsOfService.Read")</span>
                    </div>


Then you would have to change Nop.Web.Controllers.CheckoutController.ConfirmOrder() post action and add the following code:

if (placeOrderResult.Success)
                {

if (Model.SubscribeToNewsLetter)
                    {
                        var workContext = EngineContext.Current.Resolve<IWorkContext>();
                        var newsLetterSubscriptionService =
                            EngineContext.Current.Resolve<INewsLetterSubscriptionService>();
                        //save newsletter value
                        var newsletter = newsLetterSubscriptionService.GetNewsLetterSubscriptionByEmailAndStoreId(workContext.CurrentCustomer.Email, _storeContext.CurrentStore.Id);
                        if (newsletter != null)
                        {
                            if (model.Newsletter)
                            {
                                newsletter.Active = true;
                                newsLetterSubscriptionService.UpdateNewsLetterSubscription(newsletter);
                            }
                            //else
                            //{
                                //When registering, not checking the newsletter check box should not take an existing email address off of the subscription list.
                                //_newsLetterSubscriptionService.DeleteNewsLetterSubscription(newsletter);
                            //}
                        }
                        else
                        {
                            if (model.Newsletter)
                            {
                                newsLetterSubscriptionService.InsertNewsLetterSubscription(new NewsLetterSubscription()
                                {
                                    NewsLetterSubscriptionGuid = Guid.NewGuid(),
                                    Email = model.Email,
                                    Active = true,
                                    StoreId = _storeContext.CurrentStore.Id,
                                    CreatedOnUtc = DateTime.UtcNow
                                });
                            }
                        }
                    }



                    _httpContext.Session["OrderPaymentInfo"] = null;
                    var postProcessPaymentRequest = new PostProcessPaymentRequest()
                    {
                        Order = placeOrderResult.PlacedOrder
                    };
                    _paymentService.PostProcessPayment(postProcessPaymentRequest);

                    if (_webHelper.IsRequestBeingRedirected || _webHelper.IsPostBeingDone)
                    {
                        //redirection or POST has been done in PostProcessPayment
                        return Content("Redirected");
                    }
                    else
                    {
                        return RedirectToRoute("CheckoutCompleted", new { orderId = placeOrderResult.PlacedOrder.Id });
                    }
                }

Do note that this code has not been tested but a developer should be able to make it work.

I hope this is useful!
10 years ago
Thank you very much, it really helped.
However my approach was a little bit different.

In OpcConfirmOrder.cshtml I added the following above the ToS checkbox:

    <div class="subscribe-to-newsletter">
        <input type="checkbox" id="checknewsletter" checked="checked"/><span> subscribe to newsletter</span>
    </div>


In public.onepagecheckout.js I modified this "if":

        if (termOfServiceOk) {
            Checkout.setLoadWaiting('confirm-order');
            $.ajax({
                cache: false,
                url: this.saveUrl,
                type: 'post',
                success: this.nextStep,
                complete: this.resetLoadWaiting,
                error: Checkout.ajaxFailure,
                data: { confirmNewsletter:checknewsletter.checked }
            });
        } else {
            return false;
        }



In CheckoutController.cs:

                if (confirmNewsletter)
                {
                    var workContext = EngineContext.Current.Resolve<IWorkContext>();
                    var newsLetterSubscriptionService = EngineContext.Current.Resolve<INewsLetterSubscriptionService>();

                    newsLetterSubscriptionService.InsertNewsLetterSubscription(new NewsLetterSubscription()
                    {
                        NewsLetterSubscriptionGuid = Guid.NewGuid(),
                        Email = workContext.CurrentCustomer.Email,
                        Active = true,
                        StoreId = _storeContext.CurrentStore.Id,
                        CreatedOnUtc = DateTime.UtcNow
                    });
                }


(be sure to include the required services)
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.