navarr/m2-module-regionlookup

Magento 2 地区查找 API

资助包维护!
navarr

安装次数: 4,181

依赖项: 0

建议者: 0

安全性: 0

星标: 0

关注者: 3

分支: 0

公开问题: 3

类型:magento2-module

1.0.0 2017-06-06 14:25 UTC

This package is auto-updated.

Last update: 2024-09-12 04:31:03 UTC


README

安装

此模块实际上仅供开发者使用。如果由于某些原因您不能使用composer(怎么会这样?),您可以在app/code中复制它,或者您可以只

composer require navarr/m2-module-regionlookup

到目前为止,该模块与所有Magento版本兼容,并且已设置composer版本约束,以便在它使用的模块有破坏性更改时不会安装。

用法

有三种使用方法,但目前只实现了两种

  • getRegionInfoByCode(string $countryId, string $regionCode) : \Magento\Directory\Api\Data\RegionInformationInterface, throws NoSuchEntityException
  • getRegionInfoByName(string $countryId, string $regionName) : \Magento\Directory\Api\Data\RegionInformationInterface, throws NoSuchEntityException

NoSuchEntityException在以下情况下抛出

  • 找不到国家ID
  • 找不到地区名称/代码

如果您敢使用getRegionInfoById(int $regionId),则会抛出LocalizedException

示例

public function __construct(RegionInformationAcquirerInterface $regionAcquirer)
{
    $state = $regionAcquirer->getRegionInfoByCode('US', 'OH');
    $state->getId(); // some number
    $state->getName(); // Ohio
    $state->getCode(); // OH
}