Jump to content

Redirect to Checkout after add-to-cart


kalimati
 Share

Recommended Posts

In the demo template, the add-to-cart triggers an ajax call via htmx. In my case the client will order one variant (training program) at a time. I would like to move the user directly to the checkout page once he clicks on add to cart. How can I achieve that?

Thanks.

Link to comment
Share on other sites

Just an idea as I dont have module v2. Maybe Kongondo will have a better solution.

You can try adding the hx-redirect header to the request and htmx will redirect to the value of the header,

eg, with custom markup: 

<div hx-post="{$config->urls->root}padloper/add/" hx-headers='{"hx-redirect": "{$config->urls->root}pad/checkout/"}'>Add to Cart</div>

 

More infos:

https://docs.kongondo.com/start/customise-markup.html#padloper-partial-templates-snippets

https://htmx.org/attributes/hx-headers/

https://htmx.org/docs/#response-headers

  • Like 1
Link to comment
Share on other sites

Thanks for your response, flydev.

I am generating the add to cart form using a function. $config->urls->root does not work inside it and so I am hard coding the URLs /padloper/add/ or /checkout/ where required.

I tried your code using hx-redirect. It worked partially. It added the product to the cart and redirected but to the backend to a URL like

http://localhost/processwire/shop/padloper/products/product-name/variant-name/?productId=1429&variationId=0&product-title=product-title%3A+attribute1-option+%2F+attribute2-option&quantity=1&totalQty=1&numberOfTitles=1&totalAmount=Rs 1.500%2C00

Link to comment
Share on other sites

  • 3 weeks later...

Hi @kalimati,

Apologies for the late response. I wasn't able to use the excellent idea by @flydev ??, i.e. HX-Redirect. It wouldn't work for me. Nothing happened at all. However, we can use htmx:afterSettle and redirect using vanilla JavaScript. I have created a demo for the whole thing here in Demo-4 in the demos repo. See the README for more details. Please let me know if you have any questions. 

Next I'll work on a demo with respect to your question about a minimal checkout form.

Edited by kongondo
  • Like 2
Link to comment
Share on other sites

On 8/20/2022 at 7:13 AM, kalimati said:

$config->urls->root does not work inside it

You cannot use $config (or other variables such as $page, $pages, etc) inside a function. They are out of scope. Use wire('config') instead. 

  • Like 2
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...