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.