AJAX post to Save checkout attributes on Shopping Cart

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
5 years ago
I am on NopCommerce 3.6 and cannot upgrade for the moment.  This version suffers from lost checkout attributes if the visitor browses away from the page without having first clicked the checkout button.  So I am working on capturing the page down event and making an AJAX call to run an Action that will save the checkout attributes.

But I need help, I believe the problem is in the data I'm posting back, not containing the checkout attributes because after the post, the checkout attributes are cleared.

Please help!

Here is my code

@Shopping Cart controller

Added the following to the UpdateCart Action:

//Save checkout attributes when the cart is updated.
            ParseAndSaveCheckoutAttributes(cart, form);

Added the following Action:

//testing AJAX postback
        [ValidateInput(false)]        
        public virtual ActionResult SaveCheckoutAttributes (FormCollection form)
        {
            var cart = _workContext.CurrentCustomer.ShoppingCartItems
                .Where(sci => sci.ShoppingCartType == ShoppingCartType.ShoppingCart)
                .LimitPerStore(_storeContext.CurrentStore.Id)
                .ToList();

            //parse and save checkout attributes
            ParseAndSaveCheckoutAttributes(cart, form);

            return Json(new
            {
                html = "<p>Success</p>"
            }
                );

        }


@OrderSummary Partial View


<script type="text/javascript">
        $(document).ready(function () {
          var formsubmited = false;
            $('#SaveCheckoutAttributes').click(function () {

                if (!formsubmited) {
                    formsubmited = true;
                    var form = $('#form_formattributes :input, #form_formattributes textarea,  #form_formattributes select').serialize();
                    //addAntiForgeryToken(form);
                    $.ajax({
                        cache: false,
                        url: '@(Url.Action("SaveCheckoutAttributes", "ShoppingCart"))',
                        data: form,
                        type: "POST",
                        success: function (data) {
                            alert("Your shopping cart has been saved!");
                            if (data.Result == "OK") {
                                $(this).click();
                            }
                        }
                    });
                }
            });
        })
</script>



<button type="submit" name="SaveCheckoutAttributes" id="SaveCheckoutAttributes" class="btn bg-blue">
        <i class="fa fa-floppy-o"></i>
        @T("Save")
    </button>
5 years ago
Hello,

Please don't create duplicate post you can post on your existing post.

We as nop community should follow standards.

here is your same post https://www.nopcommerce.com/boards/t/55592/need-to-save-checkout-attributes-when-visitor-browses-away-form-the-shopping-cart.aspx.

Hope you will understand..!!
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.