CountryWithWebsites.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. /**
  7. * Customer country with website specified attribute source
  8. *
  9. * @author Magento Core Team <core@magentocommerce.com>
  10. */
  11. namespace Magento\Customer\Model\ResourceModel\Address\Attribute\Source;
  12. use Magento\Customer\Model\Config\Share;
  13. use Magento\Directory\Model\AllowedCountries;
  14. use Magento\Store\Model\ScopeInterface;
  15. class CountryWithWebsites extends \Magento\Eav\Model\Entity\Attribute\Source\Table
  16. {
  17. /**
  18. * @var \Magento\Directory\Model\ResourceModel\Country\CollectionFactory
  19. */
  20. private $countriesFactory;
  21. /**
  22. * @var \Magento\Directory\Model\AllowedCountries
  23. */
  24. private $allowedCountriesReader;
  25. /**
  26. * @var array
  27. */
  28. private $options;
  29. /**
  30. * @var \Magento\Store\Model\StoreManagerInterface
  31. */
  32. private $storeManager;
  33. /**
  34. * @var Share
  35. */
  36. private $shareConfig;
  37. /**
  38. * CountryWithWebsites constructor.
  39. * @param \Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\CollectionFactory $attrOptionCollectionFactory
  40. * @param \Magento\Eav\Model\ResourceModel\Entity\Attribute\OptionFactory $attrOptionFactory
  41. * @param \Magento\Directory\Model\ResourceModel\Country\CollectionFactory $countriesFactory
  42. * @param AllowedCountries $allowedCountriesReader
  43. * @param \Magento\Store\Model\StoreManagerInterface $storeManager
  44. * @param Share $shareConfig
  45. */
  46. public function __construct(
  47. \Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\CollectionFactory $attrOptionCollectionFactory,
  48. \Magento\Eav\Model\ResourceModel\Entity\Attribute\OptionFactory $attrOptionFactory,
  49. \Magento\Directory\Model\ResourceModel\Country\CollectionFactory $countriesFactory,
  50. \Magento\Directory\Model\AllowedCountries $allowedCountriesReader,
  51. \Magento\Store\Model\StoreManagerInterface $storeManager,
  52. \Magento\Customer\Model\Config\Share $shareConfig
  53. ) {
  54. $this->countriesFactory = $countriesFactory;
  55. $this->allowedCountriesReader = $allowedCountriesReader;
  56. $this->storeManager = $storeManager;
  57. $this->shareConfig = $shareConfig;
  58. parent::__construct($attrOptionCollectionFactory, $attrOptionFactory);
  59. }
  60. /**
  61. * @inheritdoc
  62. */
  63. public function getAllOptions($withEmpty = true, $defaultValues = false)
  64. {
  65. if (!$this->options) {
  66. $allowedCountries = [];
  67. $websiteIds = [];
  68. if (!$this->shareConfig->isGlobalScope()) {
  69. foreach ($this->storeManager->getWebsites() as $website) {
  70. $countries = $this->allowedCountriesReader
  71. ->getAllowedCountries(ScopeInterface::SCOPE_WEBSITE, $website->getId());
  72. $allowedCountries = array_merge($allowedCountries, $countries);
  73. foreach ($countries as $countryCode) {
  74. $websiteIds[$countryCode][] = $website->getId();
  75. }
  76. }
  77. } else {
  78. $allowedCountries = $this->allowedCountriesReader->getAllowedCountries();
  79. }
  80. $this->options = $this->createCountriesCollection()
  81. ->addFieldToFilter('country_id', ['in' => $allowedCountries])
  82. ->toOptionArray();
  83. foreach ($this->options as &$option) {
  84. if (isset($websiteIds[$option['value']])) {
  85. $option['website_ids'] = $websiteIds[$option['value']];
  86. }
  87. }
  88. }
  89. return $this->options;
  90. }
  91. /**
  92. * Create Countries Collection with all countries
  93. *
  94. * @return \Magento\Directory\Model\ResourceModel\Country\Collection
  95. */
  96. private function createCountriesCollection()
  97. {
  98. return $this->countriesFactory->create();
  99. }
  100. }