How to fix custom 'X' button redirect issue in LIQUID?

I added this code that creats a button that takes the user to the previous page.
X

It works well but I have a problem when a user presses the checkout button, and then goes back to the cart, and then when he presses the X button it will take him to the checkout button instead.

how can I solve it?
https://qdeqd2edwbgtwqwk-61323608109.shopifypreview.com

“when a user presses the checkout button, and then goes back to the cart, and then when he presses the X button it will take him to the checkout”

Previous Page: Checkout

Current Page: Cart

By this logic, isn’t the previous page the checkout page? If your goal is to “go back” then it’s doing it’s job. You should hard code this to go to the homepage instead of going back then. Because if you were in the checkout before you went to the cart, then the previous page is always going to be the checkout page. There’s nothing to fix here.

This will only work if you use the browser’s “Back” button to go from checkout to cart. Then it should go back to the page you were in before you went to checkout and cart.

It’s not going to make a difference but since you have history.go(-1), you should just replace it with history.back()

I tried doing what you said but it still takes me back to the checkout.

X

what do you suggest?

There isn’t any issue to fix. If you were in checkout before and now you are in the cart. Then when you go back, it’s going to checkout. What are you expecting here?

i want it to go to the page that took it to the cart at first.

if the customer is on product 1 and wants to the cart the X will take him back to product 1.

if the customer is on product 2 and wants to the cart the X will take him back to product 2.

but then when he goes to the checkout and comes back to the cart, he presses X and it takes him to the checkout, instead of product 1/2.

my question is there a way to fix that?

“i want it to go to the page that took it to the cart at first.”

In your case, the page that took you to the cart is the checkout. So it’s going back to checkout.

What you are asking for is not a simple one liner solution. There’s multiple case scenarios to check.

what if I’m coming from ur contact page? What if it’s the about page? What if I am coming from the homepage? What if I added to cart yesterday and then today I come and press the X? Where do you plan to store this information?

the best thing you can do is that when someone clicks on the X then take them to the product page of the latest item in cart. If there’s nothing in cart then go to the homepage. You can’t expect a back button to work conditionally, because it only has one purpose. Go back.

I can build the above functionality if you want, I just need your store url and password(if any)

thank you!

https://qdeqd2edwbgtwqwk-61323608109.shopifypreview.com

the code i added was a customer liquid code on the cart

Try replacing the custom liquid code with this


Now it’s not responsive at all.

what I’m I doing wrong?

Can you leave the code there so I can test it?

I made a mistake I put it on a different preview of mine.

see I put the code like you said here but it doesn’t react.

am I doing something wrong?

it’s like it’s almost almost good.

thank you

https://wgyo4t2ld3zuu2uc-61846847737.shopifypreview.com

Hey @orhl1234 ,

That is a different theme, the class names and everything is different.

You have to use this code there.


you are the best it’s almost perfect!

but why for example if I’m at the homepage and already have items in my cart, and I just press the cart, and then the X button, it won’t take me to the last page I have been to(homepage), it will take me to one of the items in the cart.

Do you see what I’m talking about?

thank you!

Forget whatever I said earlier. I think I’ve done my best here. Unfortunately, I can’t help you any further. If I have anything I will let you know.