-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Custom Country Data Endpoint (#311)
* Add Custom Country Data Endpoint * Exclude regions if optional * added comment
- Loading branch information
1 parent
5a3f03b
commit 42adfc4
Showing
4 changed files
with
89 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
namespace Bold\Checkout\Api; | ||
|
||
use Magento\Directory\Api\Data\CountryInformationInterface; | ||
|
||
interface GetCountryDataInterface | ||
{ | ||
/** | ||
* @param string $countryId | ||
* @return \Magento\Directory\Api\Data\CountryInformationInterface | ||
*/ | ||
public function getData(string $countryId): CountryInformationInterface; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<?php | ||
|
||
namespace Bold\Checkout\Model; | ||
|
||
use Bold\Checkout\Api\GetCountryDataInterface; | ||
use Magento\Directory\Api\CountryInformationAcquirerInterface; | ||
use Magento\Directory\Api\Data\CountryInformationInterface; | ||
use Magento\Directory\Api\Data\CountryInformationInterfaceFactory; | ||
use Magento\Framework\App\Config\ScopeConfigInterface; | ||
use Magento\Store\Model\ScopeInterface; | ||
use Magento\Store\Model\StoreManagerInterface; | ||
|
||
class GetCountryData implements GetCountryDataInterface | ||
{ | ||
/** | ||
* @var StoreManagerInterface | ||
*/ | ||
private $storeManager; | ||
|
||
/** | ||
* @var ScopeConfigInterface | ||
*/ | ||
private $scopeConfig; | ||
|
||
/** | ||
* @var CountryInformationAcquirerInterface | ||
*/ | ||
private $countryInformationAcquirer; | ||
|
||
/** | ||
* @var CountryInformationInterfaceFactory | ||
*/ | ||
private $countryInformationFactory; | ||
|
||
public function __construct( | ||
StoreManagerInterface $storeManager, | ||
ScopeConfigInterface $scopeConfig, | ||
CountryInformationAcquirerInterface $countryInformationAcquirer, | ||
CountryInformationInterfaceFactory $countryInformationFactory | ||
) { | ||
$this->storeManager = $storeManager; | ||
$this->scopeConfig = $scopeConfig; | ||
$this->countryInformationAcquirer = $countryInformationAcquirer; | ||
$this->countryInformationFactory = $countryInformationFactory; | ||
} | ||
|
||
public function getData(string $countryId): CountryInformationInterface | ||
{ | ||
$storeId = $this->storeManager->getStore()->getId(); | ||
$countriesRequiringStates = $this->scopeConfig->getValue('general/region/state_required', ScopeInterface::SCOPE_STORE, $storeId); | ||
$statesOptional = !in_array($countryId, explode(',', $countriesRequiringStates)); | ||
$countryData = $this->countryInformationAcquirer->getCountryInfo($countryId); | ||
|
||
// if states are optional build our own CountryInformation object omitting available_regions | ||
if ($statesOptional) { | ||
$customCountryData = $this->countryInformationFactory->create(); | ||
$customCountryData->setId($countryId); | ||
$customCountryData->setTwoLetterAbbreviation($countryData->getTwoLetterAbbreviation()); | ||
$customCountryData->setThreeLetterAbbreviation($countryData->getThreeLetterAbbreviation()); | ||
$customCountryData->setFullNameLocale($countryData->getFullNameLocale()); | ||
$customCountryData->setFullNameEnglish($countryData->getFullNameEnglish()); | ||
|
||
return $customCountryData; | ||
} | ||
|
||
return $countryData; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters