# Shopware 6.5: Create a new shipping method programmatically

**URL:** <https://forum.shopware.com/t/shopware-6-5-create-a-new-shipping-method-programmatically/100714>\
**Category:** Shopware 6 (German)\
**Tags:** general, administration, programming, plugin-wünsche\
**Created:** [2. August 2023 um 17:15 UTC](https://forum.shopware.com/t/shopware-6-5-create-a-new-shipping-method-programmatically/100714 "2023-08-02T17:15:25Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![radhu](https://avatars.discourse-cdn.com/v4/letter/r/b5ac83/32.png) [@radhu](https://forum.shopware.com/u/radhu)\
**Post date:** [2. August 2023 um 17:15 UTC](https://forum.shopware.com/t/shopware-6-5-create-a-new-shipping-method-programmatically/100714/1 "2023-08-02T17:15:26Z")

</div>

Hallo,

ich entwickle gerade ein Plug-in, das die Versandkosten anhand der Entfernung berechnen kann. Wenn ich das Plug-in installiere, möchte ich eine benutzerdefinierte Versandmethode im Backend hinzufügen.

Was kann ich tun, um dies zu erreichen? Für jede Hilfe wäre ich Ihnen sehr dankbar.

Bedankt

---

<div class="post-metadata">

**Author:** ![Max\_Shop](https://avatars.discourse-cdn.com/v4/letter/m/58f4c7/32.png) [@Max\_Shop](https://forum.shopware.com/u/Max_Shop)\
**Post date:** [2. August 2023 um 18:19 UTC](https://forum.shopware.com/t/shopware-6-5-create-a-new-shipping-method-programmatically/100714/2 "2023-08-02T18:19:31Z")

</div>

Bitte nutze einen kostenlosen Übersetzer, wie DeepL oder Google Translate, und poste deine Beiträge in Deutsch.

Das hier ist das entsprechende Entity: [https://github.com/shopware/platform/blob/db381d1112f1cc54f40bc35dc168de046312657d/src/Core/Checkout/Shipping/ShippingMethodDefinition.php#L37](https://github.com/shopware/platform/blob/db381d1112f1cc54f40bc35dc168de046312657d/src/Core/Checkout/Shipping/ShippingMethodDefinition.php#L37)

Du kannst über das Repository eine neue Versandart anlegen, genau so wie bei allen anderen Entities.

Den Code packst du dann in die Installationsroutine des Plugins: [https://developer.shopware.com/docs/guides/plugins/plugins/plugin-fundamentals/plugin-lifecycle](https://developer.shopware.com/docs/guides/plugins/plugins/plugin-fundamentals/plugin-lifecycle)

---

<div class="post-metadata">

**Author:** ![radhu](https://avatars.discourse-cdn.com/v4/letter/r/b5ac83/32.png) [@radhu](https://forum.shopware.com/u/radhu)\
**Post date:** [3. August 2023 um 10:27 UTC](https://forum.shopware.com/t/shopware-6-5-create-a-new-shipping-method-programmatically/100714/3 "2023-08-03T10:27:08Z")

</div>

> [@Max\_Shop](#):
>
> DeepL oder Google Translate

Vielen Dank für Ihre Hilfe. Ich konnte die Versandmethode erfolgreich zur Plugin-Installation hinzufügen. Damit die neue Versandart jedoch im Schaufenster sichtbar ist, muss sie auch zu den Verkaufskanälen hinzugefügt werden, ist das richtig?

Gibt es eine Möglichkeit, dies durch Programmierung zu erreichen? Ich habe versucht, ein Stück Code zu implementieren, aber es führte zu einem Fehler, der ausgelöst wurde.

```auto
public function assignNewShippingMethodToSalesChannels(): void
    {
        $context = Context::createDefaultContext();
        // Get the new shipping method entity by its ID
        $newShippingMethod = $this->shippingMethodRepository->search(
            (new Criteria([self::SHIPPING_METHOD_ID])),
            $context
        )->first();

        
        if (!$newShippingMethod) {
            return;
        }

        $newShippingMethod->setName(self::SHIPPING_METHOD_NAME);
        $newShippingMethod->setAvailabilityRuleId(self::AVAILABILITY_RULE_ID);
        $newShippingMethod->setDeliveryTimeId(self::DELIVERY_TIME_ID);
        // Fetch all sales channels
        $criteria = new Criteria();
        $criteria->addAssociation('shippingMethods');

        $salesChannels = $this->salesChannelRepository->search($criteria, $context)->getEntities();

        // Prepare the data for updating sales channels
        $salesChannelData = [];
        foreach ($salesChannels as $salesChannel) {
            
            $shippingMethods = $salesChannel->getShippingMethods();
            
            $shippingMethods->add($newShippingMethod);
            
            $salesChannelData[] = [
                'id' => $salesChannel->getId(),
                'shippingMethods' => $shippingMethods,
            ];
        }
        // Update the sales channels
        $this->salesChannelRepository->update($salesChannelData, $context);
    }

```

> [Shopware\Core\Framework\DataAbstractionLayer\Write\WriteException]  
> There are 2 error(s) while writing data.
> 
> 1. [/] Expected data to be array.
> 2. [/] Expected data to be array.

---

<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:** [3. August 2023 um 10:34 UTC](https://forum.shopware.com/t/shopware-6-5-create-a-new-shipping-method-programmatically/100714/4 "2023-08-03T10:34:18Z")

</div>

Wenn du nicht einmal 2 Minuten in die Stellung deiner Frage (in der richtigen Sprache) investieren kannst, dann erwarte nicht, dass andere auch nur 2 Sekunden Zeit in die Beantwortung deiner Frage investieren. Es gibt ein deutsches und ein englisches Forum. Thema geschlossen.

Viele Grüße

---

<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:** [3. August 2023 um 10:34 UTC](https://forum.shopware.com/t/shopware-6-5-create-a-new-shipping-method-programmatically/100714/5 "2023-08-03T10:34:25Z")

</div>



---

<div class="post-metadata">

**Author:** ![Max\_Shop](https://avatars.discourse-cdn.com/v4/letter/m/58f4c7/32.png) [@Max\_Shop](https://forum.shopware.com/u/Max_Shop)\
**Post date:** [4. August 2023 um 08:48 UTC](https://forum.shopware.com/t/shopware-6-5-create-a-new-shipping-method-programmatically/100714/6 "2023-08-04T08:48:03Z")

</div>



---

<div class="post-metadata">

**Author:** ![Max\_Shop](https://avatars.discourse-cdn.com/v4/letter/m/58f4c7/32.png) [@Max\_Shop](https://forum.shopware.com/u/Max_Shop)\
**Post date:** [4. August 2023 um 09:08 UTC](https://forum.shopware.com/t/shopware-6-5-create-a-new-shipping-method-programmatically/100714/7 "2023-08-04T09:08:55Z")

</div>

Danke für das nachträgliche übersetzen deines Beitrags.

Auf die Schnelle sieht dein Code Teilausschnitt eigentlich korrekt aus.

Das ist ein Beispiel, wie das JSON/Array aussehen müsste, wenn du per API lediglich EINEN (api/sales-channel/c644b9189a87488c81716f69924baf38) SalesChannel editierst:

`{"id":"c644b9189a87488c81716f69924baf38","shippingMethods":[{"id":"9f98fd72ec334b25835fbdf7037a8ab5"}]}`

Gib beim Debugging doch mal den Inhalt von s$alesChannelData aus.

---

<div class="post-metadata">

**Author:** ![radhu](https://avatars.discourse-cdn.com/v4/letter/r/b5ac83/32.png) [@radhu](https://forum.shopware.com/u/radhu)\
**Post date:** [4. August 2023 um 10:05 UTC](https://forum.shopware.com/t/shopware-6-5-create-a-new-shipping-method-programmatically/100714/8 "2023-08-04T10:05:29Z")

</div>

Vielen Dank für Ihre Antwort. Ich habe einige Änderungen an $salesChannelData vorgenommen, indem ich die IDs als Array an den`"shippingMethods"`-Schlüssel übergeben habe, wie Sie empfohlen haben, und zwar in diesem Format: `["id" : "9f98fd72ec334b25835fbdf7037a8ab5"].` Diese Änderung war erfolgreich, und ich danke Ihnen für Ihre Hilfe.
