# Anlegen einer Bestellung mit Gast-Benutzer in StorefrontController

**URL:** <https://forum.shopware.com/t/anlegen-einer-bestellung-mit-gast-benutzer-in-storefrontcontroller/105404>\
**Category:** Programmierung\
**Tags:** programming\
**Created:** [2. Oktober 2024 um 13:09 UTC](https://forum.shopware.com/t/anlegen-einer-bestellung-mit-gast-benutzer-in-storefrontcontroller/105404 "2024-10-02T13:09:01Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![m.brather](https://avatars.discourse-cdn.com/v4/letter/m/82dd89/32.png) [@m.brather](https://forum.shopware.com/u/m.brather)\
**Post date:** [2. Oktober 2024 um 13:09 UTC](https://forum.shopware.com/t/anlegen-einer-bestellung-mit-gast-benutzer-in-storefrontcontroller/105404/1 "2024-10-02T13:09:01Z")

</div>

Hallo allerseits,

wir haben folgendes Problem.  
Wir wollen in unserem StorefrontController einen Gast-Benutzer anlegen und anschließend eine Bestellung erstellen.

Die Reihenfolge in der wir vorgehen:

- Gast-Benutzer erstellen über das customer repository (funktioniert)
- Dabei erstellen wir die CustomerId per Uuid::randomHex
- Login über den AccountService mit loginById
- Das daraus erhaltene contextToken ersetzt nun unser altes Token
- Speichern des Contexts über den SalesChannelContextPersister  
$this-\>salesChannelContextPersister-\>save(  
$this-\>contextToken,  
[‚customerId‘ =\> $customerId],  
$this-\>context-\>getSalesChannelId()  
);
- Platzieren der Bestellung über den OrderService  
$this-\>orderService-\>createOrder($orderDataBag, $this-\>context);

Anschließend erhalten wir die Fehlermeldung: „Customer is not logged in.“  
Was außerdem interessant ist. Wenn wir nachdem wir auf der Fehlerseite gelandet sind, das Ganze per Reload erneut ausführen, wird die Bestellung ohne Probleme angelegt.  
Haben wir hier irgendeinen Schritt vergessen oder falsch gemacht? Mir scheint, als wäre das Problem wohl hauptsächlich, dass User und Context nichts miteinander anfangen können. Sicher bin ich mir allerdings nicht.

---

<div class="post-metadata">

**Author:** ![EikeBrandtWarneke](https://avatars.discourse-cdn.com/v4/letter/e/cdc98d/32.png) [@EikeBrandtWarneke](https://forum.shopware.com/u/EikeBrandtWarneke)\
**Post date:** [2. Oktober 2024 um 13:36 UTC](https://forum.shopware.com/t/anlegen-einer-bestellung-mit-gast-benutzer-in-storefrontcontroller/105404/2 "2024-10-02T13:36:26Z")

</div>

Mal aus meiner Wühlkiste:

```auto
$customerSalesChannelContext = $this->salesChannelContextFactory->create(
            Uuid::randomHex(),
            $salesChannelId,
            [
                SalesChannelContextService::CUSTOMER_ID => $customer->getId(),
                SalesChannelContextService::PAYMENT_METHOD_ID => $paymentMethodId,
                SalesChannelContextService::SHIPPING_METHOD_ID => $shippingMethodId
                SalesChannelContextService::BILLING_ADDRESS_ID => $customer->getDefaultBillingAddressId(),
                SalesChannelContextService::SHIPPING_ADDRESS_ID => $customer->getDefaultShippingAddressId(),
            ]
        );

        $cart = new Cart(
            $customerSalesChannelContext->getToken()
        );

        $lineItem = new LineItem(
            Uuid::randomHex(),
            'product',
            Uuid::randomHex(),
            1
        );

        $lineItem->setLabel('...')
            ->setGood(true)
            ->setStackable(true)
            ->setRemovable(true)
            ->setDescription(null)
            ->setQuantityInformation(new QuantityInformation())
            ->setCover(null)
            ->setPayload([
                'productNumber' => '...'
                'customFields' => null,
                'taxId' => $taxId,
                'options' => [],
            ])
            ->setStates([State::IS_PHYSICAL]);

        try {
            $cart = $this->cartService->add(
                $cart,
                [$lineItem],
                $customerSalesChannelContext
            );

            $this->cartService->recalculate(
                $cart,
                $customerSalesChannelContext
            );

            $response = $this->cartOrderRoute->order(
                $cart,
                $customerSalesChannelContext,
                new RequestDataBag()
            );
        }
        catch (\Exception $exception) { // ...
        }

```

Also einfach einen neuen saleschannelcontext aus dem customer erstellen.

Viele Grüße

---

<div class="post-metadata">

**Author:** ![m.brather](https://avatars.discourse-cdn.com/v4/letter/m/82dd89/32.png) [@m.brather](https://forum.shopware.com/u/m.brather)\
**Post date:** [7. Oktober 2024 um 07:58 UTC](https://forum.shopware.com/t/anlegen-einer-bestellung-mit-gast-benutzer-in-storefrontcontroller/105404/3 "2024-10-07T07:58:39Z")

</div>

Hi,

danke für die schnelle Antwort. Ich versuche das gerade mal testweise umzusetzen. Allerdings bekomme ich dabei einen Fehler.

Argument #15 ($salesChannelContextFactory) must be of type Shopware\Core\System\SalesChannel\Context\SalesChannelContextFactory, Shopware\Core\System\SalesChannel\Context\CachedSalesChannelContextFactory given

Injected ist aber definitiv SalesChannelContextFactory. Sowohl im Constructor, als auch in der Services.xml.

---

<div class="post-metadata">

**Author:** ![m.brather](https://avatars.discourse-cdn.com/v4/letter/m/82dd89/32.png) [@m.brather](https://forum.shopware.com/u/m.brather)\
**Post date:** [8. Oktober 2024 um 13:03 UTC](https://forum.shopware.com/t/anlegen-einer-bestellung-mit-gast-benutzer-in-storefrontcontroller/105404/4 "2024-10-08T13:03:12Z")

</div>

Mein Problem hat sich nun wohl erledigt. Den Fehler konnte ich beseitigen, indem ich den Parameter im Constructor als AbstractSalesChannelContextFactory definiere. In der Service.xml allerdings die ChannelContextFactory injecte.  
Die Lösung von @EikeBrandtWarneke hat damit dann zum Erfolg geführt. Vielen Dank. 🙂

---

<div class="post-metadata">

**Author:** ![system](https://europe1.discourse-cdn.com/flex013/uploads/shopware/original/3X/2/9/29c7587ba660f00e4995d1743162782e5d6d8653.svg) [@system](https://forum.shopware.com/u/system)\
**Post date:** [7. November 2024 um 13:04 UTC](https://forum.shopware.com/t/anlegen-einer-bestellung-mit-gast-benutzer-in-storefrontcontroller/105404/5 "2024-11-07T13:04:10Z")

</div>

Dieses Thema wurde automatisch 30 Tage nach der letzten Antwort geschlossen. Es sind keine neuen Antworten mehr erlaubt.
