How to connect an existing customer with a cart created with sales channel API?

Hello,

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.

Thank you in advance!

Hello,

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

Best regards from Schöppingen

cool Michael Telgmann

Hello Michael,

Thank you for your quick answer.

I’ve tried the following steps:

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

  2. Get the cart by passing sw-context-token and sw-access-key -> get empty cart (GET /sales-channel-api/v1/checkout/cart)

  3. Add a product to the cart by passing  sw-context-token and sw-access-key  (POST /sales-channel-api/v1/checkout/cart/product/{productId})

  4. Try to login an existing shop customer by passing username, passwordsw-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:

{
    „errors“: [
        {
            „status“: „401“,
            „code“: „CHECKOUT__CUSTOMER_AUTH_BAD_CREDENTIALS“,
            „title“: „Unauthorized“,
            „detail“: „Invalid username and/or password.“,
            „meta“: {
                „parameters“:
            }
        }
    ]
}

 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?

I’m not sure what I’m doing wrong.

Best regards,

Sorin

 

Hello,

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)

Best regards,

Sorin

Hey Sorin,

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?

Best regards from Schöppingen

cool Michael Telgmann

Hello Michael,

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.

Best regards,

Sorin

 

 

Hi Sorin,

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.

Best regards from Schöppingen

cool Michael Telgmann

Hello Michael,

What do you recommend for an own implementation? 

Using an own controller OR creating a subscriber for CUSTOMER_LOGIN_EVENT?

Best regards,

Sorin

Hi Sorin,

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.

Best regards from Schöppingen

cool Michael Telgmann

1 „Gefällt mir“

Hello Michael,

Yes the flow will be the following:

  • login customers and creating carts from outside using SalesChannelAPI

  • customers will login into the shop based on a link and will see the last cart created for them

I think a subscriber will be enough and this will update the token for the cart with the token provided after user logs in.

Thank you very much for your answers!

Best regards,

Sorin

 

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)

Cheers
Karim