Developer forum

Forum » Rapido » Empty cart when changing languages

Empty cart when changing languages

Ivan Marijanović
Ivan Marijanović
Reply

Hi everybody.

We would like to empty cart when we user changes languge on webshop. So I added cartcmd=EmptyCart at end of link. What happends is that when I click on that link the cart gets emptied but there is no redirection - I stay on same page instead of going to different langugage page. if cart is empty the website opens different language page as expected.

It si Rapido 3.0 and DW 9.8.9

Thank you in advance.

Ivan


Replies

 
Nicolai Pedersen
Reply

You cannot do that in one request. You would have to put it into a javascript and when switching language, first make a request to empty the cart (fetch or XMLhttp), and then make the redirect to the new language...

BR Nicolai

 
Ivan Marijanović
Ivan Marijanović
Reply

Nicolai

 

Colud you give me example how to use fetch to empty cart. Now when I use javascript I get same result because it trys to redirect before EmptyCart is finsihed. It should probably use somekind of ajax call but I am not sure how to do that.

function redirectToLanguage(address) {
    googleEnchantImpressionEmptyCart();
    Cart.EmptyCart(event);
    window.location.href = address;
}

Ivan

 
Nicolai Pedersen
Reply
This post has been marked as an answer
<script>
    function emptycart() {
        
        fetch("https://dw9.local.dynamicweb.dk/?cartcmd=emptycart&redirect=false")
            .then(response => {
                if (!response.ok) {
                    throw new Error('Something went wrong! ' + response.statusText);
                }
                alert(response.status); //Skip this in the implementation
            })
    }
</script>
<button onclick="emptycart();">Empty cart</button>
Votes for this answer: 1
 
Ivan Marijanović
Ivan Marijanović
Reply

Nicolai

This works like a charm!

Thank you!

Ivan

 

You must be logged in to post in the forum