CarrierFinder.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\InstantPurchase\Model\ShippingMethodChoose;
  7. use Magento\Customer\Model\Address;
  8. use Magento\Framework\DataObject;
  9. use Magento\Shipping\Model\Config as CarriersConfig;
  10. use Magento\Store\Model\StoreManagerInterface;
  11. /**
  12. * Collect shipping rates for customer address without packaging estimation.
  13. */
  14. class CarrierFinder
  15. {
  16. /**
  17. * @var CarriersConfig
  18. */
  19. private $carriersConfig;
  20. /**
  21. * @var StoreManagerInterface
  22. */
  23. private $storeManager;
  24. /**
  25. * CarrierFinder constructor.
  26. * @param CarriersConfig $carriersConfig
  27. * @param StoreManagerInterface $storeManager
  28. */
  29. public function __construct(
  30. CarriersConfig $carriersConfig,
  31. StoreManagerInterface $storeManager
  32. ) {
  33. $this->carriersConfig = $carriersConfig;
  34. $this->storeManager = $storeManager;
  35. }
  36. /**
  37. * Finds carriers delivering to customer address
  38. *
  39. * @param Address $address
  40. * @return array
  41. */
  42. public function getCarriersForCustomerAddress(Address $address): array
  43. {
  44. $request = new DataObject([
  45. 'dest_country_id' => $address->getCountryId()
  46. ]);
  47. $carriers = [];
  48. foreach ($this->carriersConfig->getActiveCarriers($this->storeManager->getStore()->getId()) as $carrier) {
  49. $checked = $carrier->checkAvailableShipCountries($request);
  50. if (false !== $checked && null === $checked->getErrorMessage() && !empty($checked->getAllowedMethods())) {
  51. $carriers[] = $checked;
  52. }
  53. }
  54. return $carriers;
  55. }
  56. }