AllowedCountries.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Customer\Model\Plugin;
  7. use Magento\Customer\Model\Config\Share;
  8. use Magento\Store\Api\Data\WebsiteInterface;
  9. use Magento\Store\Model\ScopeInterface;
  10. use Magento\Store\Model\StoreManagerInterface;
  11. /**
  12. * Class AllowedCountries
  13. */
  14. class AllowedCountries
  15. {
  16. /**
  17. * @var \Magento\Customer\Model\Config\Share
  18. */
  19. private $shareConfig;
  20. /**
  21. * @var StoreManagerInterface
  22. */
  23. private $storeManager;
  24. /**
  25. * @param Share $share
  26. * @param StoreManagerInterface $storeManager
  27. */
  28. public function __construct(
  29. Share $share,
  30. StoreManagerInterface $storeManager
  31. ) {
  32. $this->shareConfig = $share;
  33. $this->storeManager = $storeManager;
  34. }
  35. /**
  36. * Retrieve all allowed countries or specific by scope depends on customer share setting
  37. *
  38. * @param \Magento\Directory\Model\AllowedCountries $subject
  39. * @param string | null $filter
  40. * @param string $scope
  41. * @return array
  42. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  43. */
  44. public function beforeGetAllowedCountries(
  45. \Magento\Directory\Model\AllowedCountries $subject,
  46. $scope = ScopeInterface::SCOPE_WEBSITE,
  47. $scopeCode = null
  48. ) {
  49. if ($this->shareConfig->isGlobalScope()) {
  50. //Check if we have shared accounts - than merge all website allowed countries
  51. $scopeCode = array_map(function (WebsiteInterface $website) {
  52. return $website->getId();
  53. }, $this->storeManager->getWebsites());
  54. $scope = ScopeInterface::SCOPE_WEBSITES;
  55. }
  56. return [$scope, $scopeCode];
  57. }
  58. }