src/Core/Checkout/Shipping/SalesChannel/ShippingMethodRoute.php line 43

  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Checkout\Shipping\SalesChannel;
  3. use Shopware\Core\Checkout\Shipping\ShippingMethodCollection;
  4. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  5. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Search\Sorting\FieldSorting;
  7. use Shopware\Core\Framework\Log\Package;
  8. use Shopware\Core\Framework\Plugin\Exception\DecorationPatternException;
  9. use Shopware\Core\System\SalesChannel\Entity\SalesChannelRepository;
  10. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\Routing\Annotation\Route;
  13. #[Route(defaults: ['_routeScope' => ['store-api']])]
  14. #[Package('checkout')]
  15. class ShippingMethodRoute extends AbstractShippingMethodRoute
  16. {
  17.     /**
  18.      * @internal
  19.      */
  20.     public function __construct(private readonly SalesChannelRepository $shippingMethodRepository)
  21.     {
  22.     }
  23.     public function getDecorated(): AbstractShippingMethodRoute
  24.     {
  25.         throw new DecorationPatternException(self::class);
  26.     }
  27.     #[Route(path'/store-api/shipping-method'name'store-api.shipping.method'methods: ['GET''POST'], defaults: ['_entity' => 'shipping_method'])]
  28.     public function load(Request $requestSalesChannelContext $contextCriteria $criteria): ShippingMethodRouteResponse
  29.     {
  30.         $criteria
  31.             ->addFilter(new EqualsFilter('active'true))
  32.             ->addAssociation('media');
  33.         if (empty($criteria->getSorting())) {
  34.             $criteria->addSorting(new FieldSorting('position'), new FieldSorting('name'FieldSorting::ASCENDING));
  35.         }
  36.         $result $this->shippingMethodRepository->search($criteria$context);
  37.         /** @var ShippingMethodCollection $shippingMethods */
  38.         $shippingMethods $result->getEntities();
  39.         if ($request->query->getBoolean('onlyAvailable') || $request->request->getBoolean('onlyAvailable')) {
  40.             $shippingMethods $shippingMethods->filterByActiveRules($context);
  41.         }
  42.         $result->assign(['entities' => $shippingMethods'elements' => $shippingMethods'total' => $shippingMethods->count()]);
  43.         return new ShippingMethodRouteResponse($result);
  44.     }
  45. }