# Versandpreis in der Kaufabwicklung für nicht ausgewählte Methoden anzeigen

**URL:** <https://forum.shopware.com/t/versandpreis-in-der-kaufabwicklung-fuer-nicht-ausgewaehlte-methoden-anzeigen/99098>\
**Category:** Shopware 6 (German)\
**Tags:** programming\
**Created:** [27. März 2023 um 10:08 UTC](https://forum.shopware.com/t/versandpreis-in-der-kaufabwicklung-fuer-nicht-ausgewaehlte-methoden-anzeigen/99098 "2023-03-27T10:08:33Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![patryk.jurkiewicz19](https://avatars.discourse-cdn.com/v4/letter/p/8edcca/32.png) [@patryk.jurkiewicz19](https://forum.shopware.com/u/patryk.jurkiewicz19)\
**Post date:** [27. März 2023 um 10:08 UTC](https://forum.shopware.com/t/versandpreis-in-der-kaufabwicklung-fuer-nicht-ausgewaehlte-methoden-anzeigen/99098/1 "2023-03-27T10:08:33Z")

</div>

Hallo!

Ich muss den Preis für Versandmethoden anzeigen, die nicht in der Kasse ausgewählt werden. Ich habe dekoriert

Shopware\Core\Checkout\Shipping\SalesChannel\ShippingMethodRoute

Service und konfigurierte DI, um meine neue Klasse im Checkout-Controller zu platzieren. Hier ist der Code der Ladefunktion

public function load(Request $request, SalesChannelContext $context, Criteria $criteria): ShippingMethodRouteResponse {

```
    $result = $this->shippingMethodRoute->load($request, $context, $criteria);

    $shippingMethods = $result->getShippingMethods()->filterByActiveRules($context);

    $cart = $this->cartService->getCart($context->getToken(), $context);
    /** @var ShippingMethodEntity $shippingMethod */
    foreach ($shippingMethods as $shippingMethod) {
        $cartClone = clone $cart;
        $cartData = $cartClone->getData();
        $deliveries = $cartClone->getDeliveries();
        /** @var Delivery $delivery */
        foreach ($deliveries as $delivery) {
            $delivery->setShippingMethod($shippingMethod);
        }
        $cartData->set("shipping-method-" . $shippingMethod->getId(), $shippingMethod);
        $this->deliveryCalculator->calculate($cartData, $cartClone, $deliveries, $context);
        $shippingCost = $cartClone->getDeliveries()->getShippingCosts()->sum()->getTotalPrice();
        var_dump($shippingCost);
        $shippingMethod->shippingPrice = $shippingCost;
    }

    return $result;
}

```

Aber in einigen Fällen funktioniert es richtig, aber manchmal, wenn ich die Versandart ändere, wird ein Preis für alle berechnet, was nicht korrekt ist.

---

<div class="post-metadata">

**Author:** ![patryk.jurkiewicz19](https://avatars.discourse-cdn.com/v4/letter/p/8edcca/32.png) [@patryk.jurkiewicz19](https://forum.shopware.com/u/patryk.jurkiewicz19)\
**Post date:** [27. März 2023 um 11:09 UTC](https://forum.shopware.com/t/versandpreis-in-der-kaufabwicklung-fuer-nicht-ausgewaehlte-methoden-anzeigen/99098/2 "2023-03-27T11:09:42Z")

</div>

Ich habe das Problem gelöst. Sie müssen auch den Versandpreis für die Lieferung auf Null setzen, sonst wird er nicht berechnet.

```
 public function load(Request $request, SalesChannelContext $context, Criteria $criteria): ShippingMethodRouteResponse {

      $criteria->addAssociation('prices');
      $result = $this->shippingMethodRoute->load($request, $context, $criteria);
      $originalResult = clone $result;
      $cart = $this->cartService->getCart($context->getToken(), $context);
      $shippingMethods = $result->getShippingMethods()->filterByActiveRules($context);
      /** @var ShippingMethodEntity $shippingMethod */
      try {
          foreach ($shippingMethods as $shippingMethod) {
              $cartClone = clone $cart;
              $cartData = $cartClone->getData();
              $deliveries = $cartClone->getDeliveries();
              /** @var Delivery $delivery */
              foreach ($deliveries as $delivery) {
                  $delivery->setShippingCosts(new CalculatedPrice(0, 0, new CalculatedTaxCollection(), new TaxRuleCollection()));
                  $delivery->setShippingMethod($shippingMethod);
              }
              $cartData->set(DeliveryProcessor::buildKey($shippingMethod->getId()), $shippingMethod);
              $this->deliveryCalculator->calculate($cartData, $cartClone, $deliveries, $context);
              $shippingCost = $cartClone->getDeliveries()->getShippingCosts()->sum()->getTotalPrice();
              $shippingMethod->shippingPrice = $shippingCost;
          }
      } catch (\Error $exception) {
          return $originalResult;
      }

```
