edinhobledinhobl MemberComments: 17 Received thanks: 0 Member since: May 2020 edited June 2020

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"?

Answers

  • mit3233mit3233 MemberComments: 62 Received thanks: 1 Member since: January 2018

    *push* I have the same problem. Would like to login a user via API and switch to the Shopware Frontend to process the checkout.

  • edinhobledinhobl MemberComments: 17 Received thanks: 0 Member since: May 2020

    Any solutions to this?

  • mit3233mit3233 MemberComments: 62 Received thanks: 1 Member since: January 2018

    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....

  • KassorKassor MemberComments: 11 Received thanks: 1 Member since: June 2020

    Are there new information?

  • saibotdsaibotd MemberComments: 3 Received thanks: 1 Member since: September 2020

    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.

  • saibotdsaibotd MemberComments: 3 Received thanks: 1 edited September 2020 Member since: September 2020

    For the second question I've opened an issue on GitHub: https://github.com/shopware/platform/issues/1314. Hopefully someone from the team can point us to the right direction.

  • mit3233mit3233 MemberComments: 62 Received thanks: 1 Member since: January 2018

    @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....

  • saibotdsaibotd MemberComments: 3 Received thanks: 1 edited September 2020 Member since: September 2020

    @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.

    Thanked by 1FelixWienberg
  • FelixWienbergFelixWienberg MemberComments: 2 Received thanks: 0 Member since: December 2020

    @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.

  • FelixWienbergFelixWienberg MemberComments: 2 Received thanks: 0 Member since: December 2020

    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. 

     

Sign In or Register to comment.