We manage to create an empty cart using sales channel API, add/remove/change products, but does anyone knows how to connect an existing shop customer with
a cart? The ideea is to create a cart from outside for an user and the user will login into the shop and finalize the order.
if you create the cart and then log in the customer, the cart should be assigend to the customer, if you use the same context. Did you already tried this? The customer routes could be found here: \Shopware\Core\Checkout\Customer\SalesChannel\SalesChannelCustomerController
Create an empty cart by passing the SW-Access-Key taken from API access key(Sales Channel) -> get the sw-context-token (POST / sales-channel-api/v1/checkout/cart)
Get the cart by passing sw-context-token and sw-access-key -> get empty cart (GET /sales-channel-api/v1/checkout/cart)
Add a product to the cart by passing sw-context-token and sw-access-key (POST /sales-channel-api/v1/checkout/cart/product/{productId})
Try to login an existing shop customer by passing username, password , sw-context-token and sw-access-key (POST /sales-channel-api/v1/customer/login). Here I get this error even if the credentials are ok and I’m able to login with them in storefront:
I forgott to specify that we are using a shop channel and not a headless channel.
Is it necessary that the customer should be created with sales channel API in order to be logged in or should it work even if the customer is created from admin area?
Seems it was my fault when trying to login the customer by sending username and password in the headers instead as json data.
I’ve manage to login the customer and using returned sw-token-context I was able to add a product to a cart without creating an empty cart.
Seems the cart is created automatically when the product is added and also using that sw-token-context the cart is assigned to the logged in customer.
The problem is now that after logging in into the storefront, I’m not able to see that cart. Is there a setting for this? Meaning when logs in to load last cart(is possible to be multiple created with API)
so after you login your customer, the cart is gone?
Note, that the sw-context-token changes, if you login a customer. Maybe you try to get the cart with the old token?
I’m able to login the customer using sales channel API and create a cart which is automatically assigned to the customer. I can see this in the database with the sw-token-context of the logged in returned customer.
The problem is that when I’m trying to login into the shop storefront(without sales channel API) but the login form, a new token is generated and is not connected anymore with the cart I’ve created with sales channel API. If I’m setting the new generated token directly in database to that cart, I’m able to see it in storefront.
Of course, using sales channel API we can create multiple carts for a user, but I don’t think there’s a mechanism when user logs in into sotrefront to get the last cart of a customer identified by customer id and load that cart(by setting the token of newly logged in storefront user to the last cart).
Are the carts persistent in storefront? I mean for example if I have a cart then log out, and log in again with another clean browser I’ll be able to see my last not ordered cart?
Based on function ‘loginWithPassword’ from Shopware\Core\Checkout\Customer\SalesChannel\AccountService seems a new token is always generated if the context changes:
$newToken = $this-\>contextPersister-\>replace($context-\>getToken()); Function 'replace' loads only the cart from the current context.
not the cart is only saved by the cookie. So if you login somewhere else, the cart is lost.
For the rest, I don’t think this is currently possible with functionality provided by the Shopware core. But maybe you could write your own saleschannel controller with this logic.
as I understand you correctly your workflow would look like the following:
Login and filling cart via SalesChannelAPI
Calling the Storefront, login the customer and show the cart, which was build before
As you already said, changing the token in the cart table would be enough. Maybe you could somehow provide the old cart token while logging in the customer in the storefront. So you could identify the old cart and set the new token. I think a subscriber would be enough to get this done.
Hi there,
this is an old thread indeed.
Note before you authenticate, the cart has no reference to the customer_id in the cart table. So you might try to fill in that one after login based on the token ( sw-context-token)