I am tryin to implement a custom template based webshop I bought from the net. Currently I am accessing the API via Ajax. My template now handles the adding of products into the cart, removeing them and so on. But now when I reload the page the sw-context-token is gone. How to handle that reload with the token? In the shopware frontend it works after reloading. Also: How is it possible after the cart is completed by the user to redirect the user to the payment process in the shopware storefront so that a can do a „session handover“?
*push* I have the same problem. Would like to login a user via API and switch to the Shopware Frontend to process the checkout.
Any solutions to this?
I have no solution for the moment. It seems that Shopware doens’t support this function. You can use the REST Api to log in a user but you can’t switch to the Frontend and take the Session to proceed…
Are there new information?
I can help you with the first question: You have to save sw-context-token by yourself and attach it to any subsequent request.
For example, you could store it in localStorage (localstorage.setItem(‚sw-context-token‘, res.data[‚sw-context-token‘])) or in a cookie.
For the second question I’ve opened an issue on GitHub: Question/Request: Session handover from API to native sessions · Issue #1314 · shopware/platform · GitHub. Hopefully someone from the team can point us to the right direction.
@saibotd For second question: I also saved the context token to cookie and than i plan to read it out via shopware plugin and give the basket opend via api the token from the shopware frontend and override it in the Cookie. So Shopware and the website where i opend the cart, will have the same context token. Thats my solution for now…
@mit3233 This is also what I"m doing now - it actually works rather well. For anyone looking for a quick solution:
/**
* @Route("/set/{cartId}", name="frontend.cart-transfer.set", methods={"GET"}, defaults={"XmlHttpRequest"=true})
*/
public function setCart($cartId, Request $request, SalesChannelContext $context): Response
{
$swContextToken = $this->get('session')->get('sw-context-token');
if ($swContextToken != $cartId)
$this->get('session')->set('sw-context-token', $cartId);
return $this->redirectToRoute("frontend.checkout.cart.page");
}
$cartId is the sw-context-token
from the API.
@saibotd Thanks for this solution. It works really well for synching the cart, but do you have any Idea if it would be possible to do something similiar with the login status of the user?
I’d relly like to prevent that a user who already has logged in through the API on our website to have to log in a second time for the checkout process.
Please ignore my last comment!
The problem of the lost login status of the user was caused by having different sales-channels for the headless and the native enviroment.