Show shipping price in checkout for not selected methods

Hello!

I need to show price for shipping methods that are not selected in the checkout. I’ve decorated

Shopware\Core\Checkout\Shipping\SalesChannel\ShippingMethodRoute

Service, and configured DI to place my new class in the checkout controller. Here is the load function code

  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;
  }

But in some cases, it works correctly but sometimes when I change the shipping method it calculates one price for all of them, which is incorrect.

I’ve solved the issue. You need to also set the delivery shipping price to zero, otherwise, it doesn’t calculate it

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;
      }


      return $result;
  }
1 „Gefällt mir“