Skip to content

Commit

Permalink
Add Custom Country Data Endpoint (#311)
Browse files Browse the repository at this point in the history
* Add Custom Country Data Endpoint

* Exclude regions if optional

* added comment
  • Loading branch information
nicolenorman authored Aug 28, 2024
1 parent 5a3f03b commit 42adfc4
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 0 deletions.
14 changes: 14 additions & 0 deletions Api/GetCountryDataInterface.php
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;
}
68 changes: 68 additions & 0 deletions Model/GetCountryData.php
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;
}
}
1 change: 1 addition & 0 deletions etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
<preference for="Bold\Checkout\Model\Order\CompleteOrderInterface" type="Bold\Checkout\Model\Order\CompleteOrderPool"/>
<preference for="Bold\Checkout\Api\Data\BoldQuoteInterface" type="Bold\Checkout\Model\Quote\QuoteExtensionData"/>
<preference for="Bold\Checkout\Api\BoldQuoteRepositoryInterface" type="Bold\Checkout\Model\BoldQuoteRepository"/>
<preference for="Bold\Checkout\Api\GetCountryDataInterface" type="Bold\Checkout\Model\GetCountryData"/>

<type name="Bold\Checkout\Model\Order\CompleteOrderPool">
<arguments>
Expand Down
6 changes: 6 additions & 0 deletions etc/webapi.xml
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,10 @@
<resource ref="Bold_Checkout::integration" />
</resources>
</route>
<route method="GET" url="/V1/countries/:countryId">
<service class="Bold\Checkout\Api\GetCountryDataInterface" method="getData"/>
<resources>
<resource ref="anonymous"/>
</resources>
</route>
</routes>

0 comments on commit 42adfc4

Please sign in to comment.