Developer forum

Forum » Templates » Add to Cart - Redirect=false

Add to Cart - Redirect=false

Nuno Aguiar
Reply

Hi,

 

We are trying to add a product to cart through ajax, but the system normally redirects to the product page.

 

I now we can set the Redirect parameter to redirect the user directly to the cart (for example), but we want to add to cart in AJAX, and use the HTTP Response to update the cart information, avoiding the carr Redirect. I thought of using Redirect=false, with no luck.

 

Can we do this?

 

Best Regards,

Nuno


Replies

 
Anders Ebdrup
Reply

Hi Nuno,

 

When making an ajax cart we have a page with no layout and the cart module attached, and after adding an item to the cart we redirect the to that page and reads the response with jquery:

$('.js-basket-add').submit(function (e) {
        var url = $('#json-cart-url').val();
        if (url) {
            e.preventDefault();
            var input = $(this).serialize() + "&Redirect=default.aspx?id=" + url;
            $.ajax(
            {
                url: "Default.aspx?id=" + url,
                type: "POST",
                data: input,
                dataType: "html",
                cache: false,
                traditional: true,
                success: function (data, textStatus, jqXHR) {
                    $(".js-minicart").html($(data).html());
                    $(".js-minicart").show();
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    console.log(jqXHR);
                    console.log(textStatus);
                    console.log(errorThrown);
                }
            });
            return false;
        }
    });

This is the content of the "no layout"-template:

<!--@DwContent(contentmain)-->
<!--@If(1=2)-->
<div class="dwcontent" id="contentmain" title="Main content" data-settings="template:ModuleOnly.cshtml"></div>
<!--@EndIf-->

 

Hope this can help you?

 

Best regards, Anders

 
Mikkel Ricky
Reply

I was going to say the same as Anders (basically). Having a page with no layout to render the cart is the way to go.

Best regards,
Mikkel

 
Nuno Aguiar
Reply

Hi Anders and Mikkel,

 

That's what we already do. I was looking for a way to prevent the 2nd request (the redirect) when in AJAX, so that the response from the 1st request (with cartcmd=add) would give us the end result (no layout just the cart using global tags)

 

In Portugal we have a saying "That which has no resolution, is resolved", so we will keep that 2nd request.

 

Thanks,

Nuno

 
Mikkel Ricky
Reply

The page that the cart redirects to is the referring page unless the redirect query parameter is set. So to stay on the cart page in the AJAX request you have to set the redirect parameter to the url of the cart page.

Best regards,
Mikkel

 
Aki Ruuskanen
Reply

I am also interested in a featrure where you can get the result in the first request. 

I have a productlist where a product is "expanded" when clicked on (like google image search). The URL is also changed with script so that it points to the product. 

I also have a page with a "json cart" (a no-layout-page that only generates the cartcontnet in json).

So when I first did a request to the json cart from the productlist to add a product the request is redirected to the URL for the productdetail which is not what I want. 

When I add the "redirect=jsoncartpage" I get the correct response but there is this redirection (302) from the "json cart" page to the "json cart" page.

So I would also like a "preventredirect=true" or something so that I get the cart response in the same request as the "add".

Regards / Aki

 

 

 
Nicolai Høeg Pedersen
Reply
This post has been marked as an answer

Hi Aki

You just post your ajax call to the cart using &redirect=false and it will just return the cart output in your json format without redirects.

So you get something like this: Default.aspx?ID=9011&cartcmd=add&Quantity=1&ProductID=PROD229&Redirect=false

Votes for this answer: 1
 
Aki Ruuskanen
Reply

Oh, there is a "redirect=false". Excellent. :)

 
Aki Ruuskanen
Reply

What about in the cart?  If I call it with a "redirect=false" it acutallt redirects to "/false" :)

Is there any way to get similar behaviour in the cart?

/Aki

 
Aki Ruuskanen
Reply

Solved it with redirect to the jsoncart-page. But I guess a redirect=false in the cart would also be nice. :)

 
Nicolai Høeg Pedersen
Reply

Hi Aki

It seems to only skip redirecting with the "add", "addmulti", "addwithpoints" commands...

 
Jesper Holm Damgaard
Reply

Hi Guys!

I have the same problem when using CartCmd=updateorderlines

@NP: Will you please consider aligning this behaviour across all cart commands? That would be nice, thx!

--
All the best, Jesper

 
Adrian Ursu Dynamicweb Employee
Adrian Ursu
Reply

Hi Guys,

We have encountered this issue a long time ago. I mean, the problem when updating the orderlines using redirect=false in an AJAX call.

When using redirect=false on an update, it looks like the system tries to load a page  "/false" which returned, obviously, a 404 error.
We have solved it by creating an empty page called false. I had no other way of solving it since the problem is very deep in DW.

Might be a solution until a fix is issued.

Adrian

 
Nicolai Pedersen
Reply
This post has been marked as an answer

Hi All

Yes, of course. Just made the change with TFS#33559 and it will be out with next hotfix of DW9.2

BR Nicolai

Votes for this answer: 2
 
Jesper Holm Damgaard
Reply

Sweet, thx! Jesper.

 
Kristian Kirkholt Dynamicweb Employee
Kristian Kirkholt
Reply

Hi All

This has now been implemented in Dynamicweb version 9.2.14 

You are able to find this build in the download section:

http://doc.dynamicweb.com/releases-and-downloads/releases

Please contact Dynamicweb Support if you need any additional help regarding this.

Kind Regards
Dynamicweb Support
Kristian Kirkholt

 

You must be logged in to post in the forum