init
This commit is contained in:
@@ -0,0 +1,6 @@
|
||||
Copyright © 2022-present
|
||||
|
||||
This file included in Stemcell/ChineseDirectory is licensed under OSL 3.0
|
||||
|
||||
http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
|
||||
Please see LICENSE.txt for the full text of the OSL 3.0 license
|
||||
@@ -0,0 +1,74 @@
|
||||
<?php
|
||||
namespace Stemcell\ChineseDirectory\Controller\Index;
|
||||
|
||||
use Stemcell\ChineseDirectory\Model\CollectionFactory;
|
||||
use Magento\Directory\Model\ResourceModel\Region\CollectionFactory as RegionCollectionFactory;
|
||||
use Magento\Directory\Model\RegionFactory;
|
||||
use Magento\Framework\App\Action\HttpGetActionInterface;
|
||||
use Magento\Framework\App\RequestInterface;
|
||||
use Stemcell\Customer\Api\Data\Http\JsonResponse;
|
||||
use Magento\Framework\Controller\Result\JsonFactory as ResultJsonFactory;
|
||||
|
||||
class Index implements HttpGetActionInterface
|
||||
{
|
||||
protected CollectionFactory $collectionFactory;
|
||||
|
||||
protected RequestInterface $request;
|
||||
|
||||
protected JsonResponse $jsonResponse;
|
||||
|
||||
protected ResultJsonFactory $resultJsonFactory;
|
||||
|
||||
protected RegionCollectionFactory $regionCollectionFactory;
|
||||
|
||||
const TYPE = ['city', 'district', 'town'];
|
||||
|
||||
public function __construct(
|
||||
CollectionFactory $collectionFactory,
|
||||
RequestInterface $request,
|
||||
JsonResponse $jsonResponse,
|
||||
RegionCollectionFactory $regionCollectionFactory,
|
||||
ResultJsonFactory $resultJsonFactory
|
||||
) {
|
||||
$this->collectionFactory = $collectionFactory;
|
||||
$this->request = $request;
|
||||
$this->jsonResponse = $jsonResponse;
|
||||
$this->regionCollectionFactory = $regionCollectionFactory;
|
||||
$this->resultJsonFactory = $resultJsonFactory;
|
||||
}
|
||||
|
||||
protected function getParentCode($type, $parentCode) {
|
||||
if ($type==='city') {
|
||||
$parentCode = $this->regionCollectionFactory->create()
|
||||
->addFieldToFilter('main_table.region_id', $parentCode)
|
||||
->getFirstItem()
|
||||
->getData('code');
|
||||
}
|
||||
|
||||
return $parentCode;
|
||||
}
|
||||
|
||||
public function execute()
|
||||
{
|
||||
$resultJson = $this->resultJsonFactory->create();
|
||||
$type = strtolower($this->request->getParam('type'));
|
||||
$parentCode = $this->request->getParam('administrativeId');
|
||||
$parentCode = $this->getParentCode($type, $parentCode);
|
||||
|
||||
if (!in_array($type, self::TYPE)) {
|
||||
$this->jsonResponse->setErrors(true)->setMessages([__('Invalid type')]);
|
||||
return $resultJson->setData($this->jsonResponse);
|
||||
}
|
||||
|
||||
$collection = $this->collectionFactory->create($type)
|
||||
->addFieldToFilter('parent_code', $parentCode)
|
||||
->addFieldToSelect(['name'])
|
||||
->load();
|
||||
|
||||
$this->jsonResponse->setErrors(false)->setData([
|
||||
'administrative' => $collection->getData()
|
||||
])
|
||||
->setData(['parentCode' => $parentCode]);
|
||||
return $resultJson->setData($this->jsonResponse);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
<?php
|
||||
namespace Stemcell\ChineseDirectory\Helper;
|
||||
|
||||
use Magento\Directory\Model\AllowedCountries;
|
||||
use Magento\Directory\Model\Region;
|
||||
use Magento\Framework\App\Config\ScopeConfigInterface;
|
||||
use Magento\Store\Model\ScopeInterface;
|
||||
|
||||
class Data extends \Magento\Directory\Helper\Data
|
||||
{
|
||||
protected const STORE_ID = 'store_id';
|
||||
|
||||
/**
|
||||
* Get current scope from request
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function getCurrentScope(): array
|
||||
{
|
||||
$scope = [
|
||||
'type' => ScopeConfigInterface::SCOPE_TYPE_DEFAULT,
|
||||
'value' => null,
|
||||
];
|
||||
$request = $this->_getRequest();
|
||||
if ($request->getParam(ScopeInterface::SCOPE_WEBSITE)) {
|
||||
$scope = [
|
||||
'type' => ScopeInterface::SCOPE_WEBSITE,
|
||||
'value' => $request->getParam(ScopeInterface::SCOPE_WEBSITE),
|
||||
];
|
||||
} elseif ($request->getParam(ScopeInterface::SCOPE_STORE)) {
|
||||
$scope = [
|
||||
'type' => ScopeInterface::SCOPE_STORE,
|
||||
'value' => $request->getParam(ScopeInterface::SCOPE_STORE),
|
||||
];
|
||||
} elseif ($request->getParam(self::STORE_ID)) {
|
||||
$scope = [
|
||||
'type' => ScopeInterface::SCOPE_STORE,
|
||||
'value' => $request->getParam(self::STORE_ID),
|
||||
];
|
||||
}
|
||||
|
||||
return $scope;
|
||||
}
|
||||
/**
|
||||
* Retrieve regions data
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getRegionData()
|
||||
{
|
||||
$scope = $this->getCurrentScope();
|
||||
$allowedCountries = $this->scopeConfig->getValue(
|
||||
AllowedCountries::ALLOWED_COUNTRIES_PATH,
|
||||
$scope['type'],
|
||||
$scope['value']
|
||||
);
|
||||
$countryIds = explode(',', $allowedCountries);
|
||||
$collection = $this->_regCollectionFactory->create();
|
||||
$collection->addCountryFilter($countryIds)->load();
|
||||
$regions = [
|
||||
'config' => [
|
||||
'show_all_regions' => $this->isShowNonRequiredState(),
|
||||
'regions_required' => $this->getCountriesWithStatesRequired(),
|
||||
],
|
||||
];
|
||||
foreach ($collection as $region) {
|
||||
/** @var $region Region */
|
||||
if (!$region->getRegionId()) {
|
||||
continue;
|
||||
}
|
||||
$regions[$region->getCountryId()][$region->getCode()] = [
|
||||
'code' => $region->getCode(),
|
||||
'name' => (string)__($region->getName()),
|
||||
];
|
||||
}
|
||||
return $regions;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
This Open Software License (the "License") applies to any original work of authorship (the "Original Work") whose owner (the "Licensor") has placed the following licensing notice adjacent to the copyright notice for the Original Work:
|
||||
|
||||
Licensed under the Open Software License version 3.0
|
||||
|
||||
1) Grant of Copyright License. Licensor grants You a worldwide, royalty-free, non-exclusive, sublicensable license, for the duration of the copyright, to do the following:
|
||||
|
||||
a) to reproduce the Original Work in copies, either alone or as part of a collective work;
|
||||
|
||||
b) to translate, adapt, alter, transform, modify, or arrange the Original Work, thereby creating derivative works ("Derivative Works") based upon the Original Work;
|
||||
|
||||
c) to distribute or communicate copies of the Original Work and Derivative Works to the public, with the proviso that copies of Original Work or Derivative Works that You distribute or communicate shall be licensed under this Open Software License;
|
||||
|
||||
d) to perform the Original Work publicly; and
|
||||
|
||||
e) to display the Original Work publicly.
|
||||
|
||||
2) Grant of Patent License. Licensor grants You a worldwide, royalty-free, non-exclusive, sublicensable license, under patent claims owned or controlled by the Licensor that are embodied in the Original Work as furnished by the Licensor, for the duration of the patents, to make, use, sell, offer for sale, have made, and import the Original Work and Derivative Works.
|
||||
|
||||
3) Grant of Source Code License. The term "Source Code" means the preferred form of the Original Work for making modifications to it and all available documentation describing how to modify the Original Work. Licensor agrees to provide a machine-readable copy of the Source Code of the Original Work along with each copy of the Original Work that Licensor distributes. Licensor reserves the right to satisfy this obligation by placing a machine-readable copy of the Source Code in an information repository reasonably calculated to permit inexpensive and convenient access by You for as long as Licensor continues to distribute the Original Work.
|
||||
|
||||
4) Exclusions From License Grant. Neither the names of Licensor, nor the names of any contributors to the Original Work, nor any of their trademarks or service marks, may be used to endorse or promote products derived from this Original Work without express prior permission of the Licensor. Except as expressly stated herein, nothing in this License grants any license to Licensor's trademarks, copyrights, patents, trade secrets or any other intellectual property. No patent license is granted to make, use, sell, offer for sale, have made, or import embodiments of any patent claims other than the licensed claims defined in Section 2. No license is granted to the trademarks of Licensor even if such marks are included in the Original Work. Nothing in this License shall be interpreted to prohibit Licensor from licensing under terms different from this License any Original Work that Licensor otherwise would have a right to license.
|
||||
|
||||
5) External Deployment. The term "External Deployment" means the use, distribution, or communication of the Original Work or Derivative Works in any way such that the Original Work or Derivative Works may be used by anyone other than You, whether those works are distributed or communicated to those persons or made available as an application intended for use over a network. As an express condition for the grants of license hereunder, You must treat any External Deployment by You of the Original Work or a Derivative Work as a distribution under section 1(c).
|
||||
|
||||
6) Attribution Rights. You must retain, in the Source Code of any Derivative Works that You create, all copyright, patent, or trademark notices from the Source Code of the Original Work, as well as any notices of licensing and any descriptive text identified therein as an "Attribution Notice." You must cause the Source Code for any Derivative Works that You create to carry a prominent Attribution Notice reasonably calculated to inform recipients that You have modified the Original Work.
|
||||
|
||||
7) Warranty of Provenance and Disclaimer of Warranty. Licensor warrants that the copyright in and to the Original Work and the patent rights granted herein by Licensor are owned by the Licensor or are sublicensed to You under the terms of this License with the permission of the contributor(s) of those copyrights and patent rights. Except as expressly stated in the immediately preceding sentence, the Original Work is provided under this License on an "AS IS" BASIS and WITHOUT WARRANTY, either express or implied, including, without limitation, the warranties of non-infringement, merchantability or fitness for a particular purpose. THE ENTIRE RISK AS TO THE QUALITY OF THE ORIGINAL WORK IS WITH YOU. This DISCLAIMER OF WARRANTY constitutes an essential part of this License. No license to the Original Work is granted by this License except under this disclaimer.
|
||||
|
||||
8) Limitation of Liability. Under no circumstances and under no legal theory, whether in tort (including negligence), contract, or otherwise, shall the Licensor be liable to anyone for any indirect, special, incidental, or consequential damages of any character arising as a result of this License or the use of the Original Work including, without limitation, damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses. This limitation of liability shall not apply to the extent applicable law prohibits such limitation.
|
||||
|
||||
9) Acceptance and Termination. If, at any time, You expressly assented to this License, that assent indicates your clear and irrevocable acceptance of this License and all of its terms and conditions. If You distribute or communicate copies of the Original Work or a Derivative Work, You must make a reasonable effort under the circumstances to obtain the express assent of recipients to the terms of this License. This License conditions your rights to undertake the activities listed in Section 1, including your right to create Derivative Works based upon the Original Work, and doing so without honoring these terms and conditions is prohibited by copyright law and international treaty. Nothing in this License is intended to affect copyright exceptions and limitations (including "fair use" or "fair dealing"). This License shall terminate immediately and You may no longer exercise any of the rights granted to You by this License upon your failure to honor the conditions in Section 1(c).
|
||||
|
||||
10) Termination for Patent Action. This License shall terminate automatically and You may no longer exercise any of the rights granted to You by this License as of the date You commence an action, including a cross-claim or counterclaim, against Licensor or any licensee alleging that the Original Work infringes a patent. This termination provision shall not apply for an action alleging patent infringement by combinations of the Original Work with other software or hardware.
|
||||
|
||||
11) Jurisdiction, Venue and Governing Law. Any action or suit relating to this License may be brought only in the courts of a jurisdiction wherein the Licensor resides or in which Licensor conducts its primary business, and under the laws of that jurisdiction excluding its conflict-of-law provisions. The application of the United Nations Convention on Contracts for the International Sale of Goods is expressly excluded. Any use of the Original Work outside the scope of this License or after its termination shall be subject to the requirements and penalties of copyright or patent law in the appropriate jurisdiction. This section shall survive the termination of this License.
|
||||
|
||||
12) Attorneys' Fees. In any action to enforce the terms of this License or seeking damages relating thereto, the prevailing party shall be entitled to recover its costs and expenses, including, without limitation, reasonable attorneys' fees and costs incurred in connection with such action, including any appeal of such action. This section shall survive the termination of this License.
|
||||
|
||||
13) Miscellaneous. If any provision of this License is held to be unenforceable, such provision shall be reformed only to the extent necessary to make it enforceable.
|
||||
|
||||
14) Definition of "You" in This License. "You" throughout this License, whether in upper or lower case, means an individual or a legal entity exercising rights under, and complying with all of the terms of, this License. For legal entities, "You" includes any entity that controls, is controlled by, or is under common control with you. For purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
15) Right to Use. You may use the Original Work in all ways not otherwise restricted or conditioned by this License or by law, and Licensor promises not to interfere with or be responsible for such uses by You.
|
||||
|
||||
16) Modification of This License. This License is Copyright © 2005 Lawrence Rosen. Permission is granted to copy, distribute, or communicate this License without modification. Nothing in this License permits You to modify this License as applied to the Original Work or to Derivative Works. However, You may modify the text of this License and copy, distribute or communicate your modified version (the "Modified License") and apply it to other original works of authorship subject to the following conditions: (i) You may not indicate in any way that your Modified License is the "Open Software License" or "OSL" and you may not use those names in the name of your Modified License; (ii) You must replace the notice specified in the first paragraph above with the notice "Licensed under <insert your license name here>" or with a notice of your own that is not confusingly similar to the notice in this License; and (iii) You may not claim that your original works are open source software unless your Modified License has been approved by Open Source Initiative (OSI) and You comply with its license review and certification process.
|
||||
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
namespace Stemcell\ChineseDirectory\Model;
|
||||
|
||||
use Magento\Framework\Model\AbstractModel;
|
||||
use Stemcell\ChineseDirectory\Model\ResourceModel\City as CityResourceModel;
|
||||
|
||||
class City extends AbstractModel
|
||||
{
|
||||
protected function _construct()
|
||||
{
|
||||
$this->_init(CityResourceModel::class);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
namespace Stemcell\ChineseDirectory\Model;
|
||||
|
||||
use Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection;
|
||||
use Magento\Framework\ObjectManagerInterface;
|
||||
|
||||
class CollectionFactory
|
||||
{
|
||||
protected ObjectManagerInterface $_objectManager;
|
||||
|
||||
public function __construct(
|
||||
ObjectManagerInterface $_objectManager
|
||||
) {
|
||||
$this->_objectManager = $_objectManager;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param String $collectionType
|
||||
* @param array $data
|
||||
* @return AbstractCollection
|
||||
*/
|
||||
public function create(String $collectionType, array $data = []) {
|
||||
$collection = 'Stemcell\ChineseDirectory\Model\ResourceModel\\'.ucfirst($collectionType).'\Collection';
|
||||
if (strtolower($collectionType)==='region') {
|
||||
$collection = 'Magento\Directory\Model\ResourceModel\Region\Collection';
|
||||
}
|
||||
return $this->_objectManager->create($collection, $data);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
<?php
|
||||
namespace Stemcell\ChineseDirectory\Model;
|
||||
|
||||
use Magento\Framework\Model\AbstractModel;
|
||||
|
||||
class District extends AbstractModel
|
||||
{
|
||||
protected function _construct()
|
||||
{
|
||||
$this->_init(ResourceModel\District::class);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
namespace Stemcell\ChineseDirectory\Model\Entity\Attribute\Backend;
|
||||
|
||||
use Magento\Eav\Model\Entity\Attribute\Backend\AbstractBackend;
|
||||
use Magento\Framework\App\ObjectManager;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
class City extends AbstractBackend
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
}
|
||||
|
||||
public function beforeSave($object)
|
||||
{
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
namespace Stemcell\ChineseDirectory\Model\Entity\Attribute\Backend;
|
||||
|
||||
use Magento\Eav\Model\Entity\Attribute\Backend\AbstractBackend;
|
||||
use Stemcell\ChineseDirectory\Model\CollectionFactory;
|
||||
|
||||
class District extends AbstractBackend
|
||||
{
|
||||
protected CollectionFactory $collectionFactory;
|
||||
|
||||
public function __construct(
|
||||
CollectionFactory $collectionFactory
|
||||
) {
|
||||
$this->collectionFactory = $collectionFactory;
|
||||
}
|
||||
|
||||
public function beforeSave($object)
|
||||
{
|
||||
if (is_numeric($object->getData('district_code'))) {
|
||||
$district = $this->collectionFactory->create('district')
|
||||
->addFieldToFilter('code', $object->getData('district_code'))
|
||||
->getFirstItem();
|
||||
$object->setData('district', $district->getData('name'));
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
namespace Stemcell\ChineseDirectory\Model\Entity\Attribute\Backend;
|
||||
|
||||
use Magento\Eav\Model\Entity\Attribute\Backend\AbstractBackend;
|
||||
use Stemcell\ChineseDirectory\Model\CollectionFactory;
|
||||
|
||||
class DistrictCode extends AbstractBackend
|
||||
{
|
||||
protected CollectionFactory $collectionFactory;
|
||||
|
||||
public function __construct(
|
||||
CollectionFactory $collectionFactory
|
||||
) {
|
||||
$this->collectionFactory = $collectionFactory;
|
||||
}
|
||||
|
||||
public function beforeSave($object)
|
||||
{
|
||||
if (is_numeric($object->getData('district_code'))) {
|
||||
$object->setData('district_code', $object->getData('district_code'));
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
namespace Stemcell\ChineseDirectory\Model\Entity\Attribute\Backend;
|
||||
|
||||
use Stemcell\ChineseDirectory\Model\CollectionFactory;
|
||||
use Magento\Eav\Model\Entity\Attribute\Backend\AbstractBackend;
|
||||
|
||||
class Town extends AbstractBackend
|
||||
{
|
||||
protected CollectionFactory $collectionFactory;
|
||||
|
||||
public function __construct(
|
||||
CollectionFactory $collectionFactory
|
||||
) {
|
||||
$this->collectionFactory = $collectionFactory;
|
||||
}
|
||||
|
||||
public function beforeSave($object)
|
||||
{
|
||||
if (is_numeric($object->getData('town_code'))) {
|
||||
$district = $this->collectionFactory->create('town')
|
||||
->addFieldToFilter('code', $object->getData('town_code'))
|
||||
->getFirstItem();
|
||||
$object->setData('town', $district->getData('name'));
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
<?php
|
||||
namespace Stemcell\ChineseDirectory\Model\Entity\Attribute\Source;
|
||||
|
||||
use Magento\Eav\Model\Entity\Attribute\Source\AbstractSource;
|
||||
|
||||
class CityId extends AbstractSource
|
||||
{
|
||||
public function getAllOptions()
|
||||
{
|
||||
return $this->_options = [
|
||||
['value' => '', 'label' => __('Please select a city.')]
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
<?php
|
||||
namespace Stemcell\ChineseDirectory\Model\Entity\Attribute\Source;
|
||||
|
||||
use Magento\Eav\Model\Entity\Attribute\Source\AbstractSource;
|
||||
|
||||
class DistrictId extends AbstractSource
|
||||
{
|
||||
public function getAllOptions()
|
||||
{
|
||||
return $this->_options = [
|
||||
['value' => '', 'label' => __('Please select a district.')]
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
<?php
|
||||
namespace Stemcell\ChineseDirectory\Model\Entity\Attribute\Source;
|
||||
|
||||
use Magento\Eav\Model\Entity\Attribute\Source\AbstractSource;
|
||||
|
||||
class TownId extends AbstractSource
|
||||
{
|
||||
public function getAllOptions()
|
||||
{
|
||||
return $this->_options = [
|
||||
['value' => '', 'label' => __('Please select street.')]
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
<?php
|
||||
namespace Stemcell\ChineseDirectory\Model\ResourceModel;
|
||||
|
||||
use Magento\Framework\Model\ResourceModel\Db\AbstractDb;
|
||||
|
||||
class City extends AbstractDb
|
||||
{
|
||||
|
||||
protected function _construct()
|
||||
{
|
||||
$this->_init('directory_country_city', 'code');
|
||||
$this->_isPkAutoIncrement = false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
<?php
|
||||
namespace Stemcell\ChineseDirectory\Model\ResourceModel\City;
|
||||
|
||||
use Stemcell\ChineseDirectory\Model\City;
|
||||
use Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection;
|
||||
use \Stemcell\ChineseDirectory\Model\ResourceModel;
|
||||
|
||||
class Collection extends AbstractCollection
|
||||
{
|
||||
protected function _construct()
|
||||
{
|
||||
$this->_init(City::class, ResourceModel\City::class);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
<?php
|
||||
namespace Stemcell\ChineseDirectory\Model\ResourceModel;
|
||||
|
||||
use Magento\Framework\Model\ResourceModel\Db\AbstractDb;
|
||||
|
||||
class District extends AbstractDb
|
||||
{
|
||||
|
||||
protected function _construct()
|
||||
{
|
||||
$this->_init('directory_country_district', 'code');
|
||||
$this->_isPkAutoIncrement = false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
<?php
|
||||
namespace Stemcell\ChineseDirectory\Model\ResourceModel\District;
|
||||
|
||||
use Stemcell\ChineseDirectory\Model\District;
|
||||
use Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection;
|
||||
use Stemcell\ChineseDirectory\Model\ResourceModel\District as DistrictResourceModel;
|
||||
|
||||
class Collection extends AbstractCollection
|
||||
{
|
||||
protected function _construct()
|
||||
{
|
||||
$this->_init(District::class, DistrictResourceModel::class);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
namespace Stemcell\ChineseDirectory\Model\ResourceModel;
|
||||
|
||||
use Magento\Framework\Model\ResourceModel\Db\AbstractDb;
|
||||
|
||||
class Town extends AbstractDb
|
||||
{
|
||||
protected function _construct()
|
||||
{
|
||||
$this->_init('directory_country_town', 'code');
|
||||
$this->_isPkAutoIncrement = false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
<?php
|
||||
namespace Stemcell\ChineseDirectory\Model\ResourceModel\Town;
|
||||
|
||||
use Stemcell\ChineseDirectory\Model\Town;
|
||||
use Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection;
|
||||
use \Stemcell\ChineseDirectory\Model\ResourceModel;
|
||||
|
||||
class Collection extends AbstractCollection
|
||||
{
|
||||
protected function _construct()
|
||||
{
|
||||
$this->_init(Town::class, ResourceModel\Town::class);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
<?php
|
||||
namespace Stemcell\ChineseDirectory\Model;
|
||||
|
||||
use Magento\Framework\Model\AbstractModel;
|
||||
|
||||
class Town extends AbstractModel
|
||||
{
|
||||
protected function _construct()
|
||||
{
|
||||
$this->_init(ResourceModel\Town::class);
|
||||
}
|
||||
}
|
||||
+44
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
namespace Stemcell\ChineseDirectory\Observer\Order\Address;
|
||||
|
||||
use Magento\Framework\DataObject\Copy\Config;
|
||||
use Magento\Framework\Event\Observer;
|
||||
use Magento\Framework\Event\ObserverInterface;
|
||||
use Magento\Quote\Model\Quote;
|
||||
use Magento\Sales\Model\Order;
|
||||
|
||||
class AttachExtensionAttributeObserver implements ObserverInterface
|
||||
{
|
||||
/**
|
||||
* @var Config
|
||||
*/
|
||||
protected Config $fieldsetConfig;
|
||||
|
||||
public function __construct(
|
||||
Config $config
|
||||
) {
|
||||
$this->fieldsetConfig = $config;
|
||||
}
|
||||
|
||||
public function execute(Observer $observer)
|
||||
{
|
||||
/* @var $order Order */
|
||||
$order = $observer->getData('order');
|
||||
|
||||
/* @var $quote Quote */
|
||||
$quote = $observer->getData('quote');
|
||||
$aspect = 'to_order_address';
|
||||
$fields = $this->fieldsetConfig->getFieldset('sales_convert_quote_address');
|
||||
$extensionAttributes = $quote->getShippingAddress()->getExtensionAttributes();
|
||||
$orderAddress = $order->getShippingAddress();
|
||||
foreach ($fields as $code => $node) {
|
||||
if (empty($node[$aspect])) {
|
||||
continue;
|
||||
}
|
||||
$method = 'get' . str_replace('_', '', ucwords($code, '_'));
|
||||
if (method_exists($extensionAttributes, $method) && $extensionAttributes->$method()) {
|
||||
$orderAddress->setData($code, $extensionAttributes->$method());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
<?php
|
||||
namespace Stemcell\ChineseDirectory\Plugin\ShippingAddress;
|
||||
|
||||
use Magento\Framework\App\ObjectManager;
|
||||
use Magento\Quote\Model\Quote;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Magento\Framework\DataObject\Copy\Config;
|
||||
use \Magento\Quote\Api\Data\AddressInterface;
|
||||
|
||||
class ExtensionAttribute
|
||||
{
|
||||
/**
|
||||
* @var Config
|
||||
*/
|
||||
protected Config $fieldsetConfig;
|
||||
|
||||
public function __construct(
|
||||
Config $config
|
||||
) {
|
||||
$this->fieldsetConfig = $config;
|
||||
}
|
||||
|
||||
public function afterGetShippingAddress(Quote $quote, Quote\Address $result) {
|
||||
/* @var $logger LoggerInterface */
|
||||
$logger = ObjectManager::getInstance()->get(LoggerInterface::class);
|
||||
$aspect = 'to_customer_address';
|
||||
$fields = $this->fieldsetConfig->getFieldset('sales_convert_quote_address');
|
||||
$extensionAttributes = $result->getExtensionAttributes();
|
||||
|
||||
foreach ($fields as $code => $node) {
|
||||
if (empty($node[$aspect])) {
|
||||
continue;
|
||||
}
|
||||
$method = 'set' . str_replace('_', '', ucwords($code, '_'));
|
||||
|
||||
if (method_exists($extensionAttributes, $method) && $result->getData($code)) {
|
||||
$extensionAttributes->$method($result->getData($code));
|
||||
$result->setCustomAttribute($code, $result->getData($code));
|
||||
}
|
||||
}
|
||||
return $result;
|
||||
|
||||
}
|
||||
|
||||
public function beforeSetShippingAddress(Quote $quote, AddressInterface $address) {
|
||||
|
||||
$aspect = 'to_customer_address';
|
||||
$fields = $this->fieldsetConfig->getFieldset('sales_convert_quote_address');
|
||||
$extensionAttributes = $address->getExtensionAttributes();
|
||||
$shippingAddress = $quote->getAddressesCollection()->getItemById($address->getId())
|
||||
?? $quote->getShippingAddress();
|
||||
foreach ($fields as $code => $node) {
|
||||
if (empty($node[$aspect])) {
|
||||
continue;
|
||||
}
|
||||
$method = 'get' . str_replace('_', '', ucwords($code, '_'));
|
||||
|
||||
if (method_exists($extensionAttributes, $method) && $extensionAttributes->$method()) {
|
||||
$shippingAddress->addData([ $code => $extensionAttributes->$method()]);
|
||||
}
|
||||
}
|
||||
return [$address];
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
# Mage2 Module Stemcell ChineseDirectory
|
||||
|
||||
``stemcell/module-chinesedirectory``
|
||||
|
||||
- [Main Functionalities](#markdown-header-main-functionalities)
|
||||
- [Installation](#markdown-header-installation)
|
||||
- [Configuration](#markdown-header-configuration)
|
||||
- [Specifications](#markdown-header-specifications)
|
||||
- [Attributes](#markdown-header-attributes)
|
||||
|
||||
|
||||
## Main Functionalities
|
||||
address for chinese administrative division
|
||||
|
||||
## Installation
|
||||
\* = in production please use the `--keep-generated` option
|
||||
|
||||
### Type 1: Zip file
|
||||
|
||||
- Unzip the zip file in `app/code/Stemcell`
|
||||
- Enable the module by running `php bin/magento module:enable Stemcell_ChineseDirectory`
|
||||
- Apply database updates by running `php bin/magento setup:upgrade`\*
|
||||
- Flush the cache by running `php bin/magento cache:flush`
|
||||
|
||||
### Type 2: Composer
|
||||
|
||||
- Make the module available in a composer repository for example:
|
||||
- private repository `repo.magento.com`
|
||||
- public repository `packagist.org`
|
||||
- public github repository as vcs
|
||||
- Add the composer repository to the configuration by running `composer config repositories.repo.magento.com composer https://repo.magento.com/`
|
||||
- Install the module composer by running `composer require stemcell/module-chinesedirectory`
|
||||
- enable the module by running `php bin/magento module:enable Stemcell_ChineseDirectory`
|
||||
- apply database updates by running `php bin/magento setup:upgrade`\*
|
||||
- Flush the cache by running `php bin/magento cache:flush`
|
||||
|
||||
|
||||
## Configuration
|
||||
|
||||
|
||||
|
||||
|
||||
## Specifications
|
||||
|
||||
|
||||
|
||||
|
||||
## Attributes
|
||||
|
||||
- CustomerAddress - City (city_id)
|
||||
|
||||
- CustomerAddress - District (district_id)
|
||||
|
||||
- CustomerAddress - District (district)
|
||||
|
||||
- CustomerAddress - Town (town_id)
|
||||
|
||||
- CustomerAddress - Town (town)
|
||||
|
||||
- Sales - City (city_id)
|
||||
|
||||
- Sales - District (district_id)
|
||||
|
||||
- Sales - District (district)
|
||||
|
||||
- Sales - Town (town_id)
|
||||
|
||||
- Sales - Town (town)
|
||||
|
||||
- Sales - Town (town_Id)
|
||||
|
||||
@@ -0,0 +1,115 @@
|
||||
<?php
|
||||
namespace Stemcell\ChineseDirectory\Setup\Patch\Data;
|
||||
|
||||
use Magento\Framework\App\ResourceConnection;
|
||||
use Magento\Framework\Setup\ModuleDataSetupInterface;
|
||||
use Magento\Framework\Setup\Patch\DataPatchInterface;
|
||||
use Magento\Framework\Setup\Patch\PatchRevertableInterface;
|
||||
use Magento\Directory\Model\ResourceModel\Region\CollectionFactory as RegionCollectionFactory;
|
||||
use Magento\Directory\Model\ResourceModel\Region as RegionResourceModel;
|
||||
use \Magento\Framework\Filesystem\Driver\File;
|
||||
use Magento\Framework\Module\Dir as ModuleDir;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
class UpdateDirectoryRegion implements DataPatchInterface, PatchRevertableInterface
|
||||
{
|
||||
protected RegionCollectionFactory $regionCollection;
|
||||
protected RegionResourceModel $regionResourceModel;
|
||||
protected File $file;
|
||||
protected ModuleDir $moduleDir;
|
||||
protected ModuleDataSetupInterface $moduleDataSetup;
|
||||
protected ResourceConnection $resourceConnection;
|
||||
protected LoggerInterface $logger;
|
||||
|
||||
public function __construct(
|
||||
RegionCollectionFactory $regionCollection,
|
||||
RegionResourceModel $regionResourceModel,
|
||||
File $file,
|
||||
ModuleDir $moduleDir,
|
||||
ModuleDataSetupInterface $moduleDataSetup,
|
||||
ResourceConnection $resourceConnection,
|
||||
LoggerInterface $logger
|
||||
) {
|
||||
$this->regionCollection = $regionCollection;
|
||||
$this->regionResourceModel = $regionResourceModel;
|
||||
$this->file = $file;
|
||||
$this->moduleDir = $moduleDir;
|
||||
$this->moduleDataSetup = $moduleDataSetup;
|
||||
$this->resourceConnection = $resourceConnection;
|
||||
$this->logger = $logger;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return RegionResourceModel\Collection
|
||||
*/
|
||||
protected function getChineseRegion() {
|
||||
return $this->regionCollection->create()
|
||||
->addFieldToFilter('country_id', 'CN')
|
||||
->addFieldToSelect("*")
|
||||
->load();
|
||||
}
|
||||
|
||||
protected function getDataPath() {
|
||||
return $this->moduleDir->getDir('Stemcell_ChineseDirectory').'/data/pcas-code.json';
|
||||
}
|
||||
|
||||
public static function getDependencies()
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
public function getAliases()
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
public function apply()
|
||||
{
|
||||
$data = $this->file->fileGetContents($this->getDataPath());
|
||||
$data = json_decode($data, true);
|
||||
$regions = $this->getChineseRegion();
|
||||
$connection = $this->moduleDataSetup->getConnection();
|
||||
foreach ($data as $region) {
|
||||
$cities = $region['children'];
|
||||
/* @var $directoryRegion \Magento\Directory\Model\Region */
|
||||
$directoryRegion = $regions->addFieldToFilter('region_id', $region['region_id'])->getFirstItem();
|
||||
|
||||
if ($directoryRegion->getId()) {
|
||||
$connection->update(
|
||||
$this->resourceConnection->getTableName('directory_country_region'),
|
||||
['code' => $region['code'], 'default_name' => $region['name']],
|
||||
'region_id='.$region['region_id']
|
||||
);
|
||||
$bind = ['locale' => 'zh_Hans_CN', 'region_id' => $region['region_id'], 'name' => $region['name']];
|
||||
$connection->insert($this->resourceConnection->getTableName('directory_country_region_name'), $bind);
|
||||
foreach ($cities as $city) {
|
||||
$bind = ['code' => intval($city['code']), 'parent_code' => intval($region['code']), 'name' => $city['name']];
|
||||
$connection->insert($this->resourceConnection->getTableName('directory_country_city'), $bind);
|
||||
$districts = $city['children'];
|
||||
foreach ($districts as $district) {
|
||||
$towns = $district['children'];
|
||||
$bind = ['code' => intval($district['code']), 'parent_code' => intval($city['code']), 'name' => $district['name']];
|
||||
$connection->insert($this->resourceConnection->getTableName('directory_country_district'), $bind);
|
||||
foreach ($towns as $town) {
|
||||
$bind = ['code' => intval($town['code']), 'parent_code' => intval($district['code']), 'name' => $town['name']];
|
||||
$connection->insert($this->resourceConnection->getTableName('directory_country_town'), $bind);
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
$temp = $region;
|
||||
unset($temp['children']);
|
||||
$this->logger->warning($region['name'].' 在directory_country_region中没有对应的数据', $temp);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function revert()
|
||||
{
|
||||
$connection = $this->moduleDataSetup->getConnection();
|
||||
$connection->delete($this->resourceConnection->getTableName('directory_country_region_name'), 'locale=zh_Hans_CN');
|
||||
$connection->dropTable($this->resourceConnection->getTableName('directory_country_city'));
|
||||
$connection->dropTable($this->resourceConnection->getTableName('directory_country_district'));
|
||||
$connection->dropTable($this->resourceConnection->getTableName('directory_country_town'));
|
||||
}
|
||||
}
|
||||
+126
@@ -0,0 +1,126 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Stemcell\ChineseDirectory\Setup\Patch\Schema;
|
||||
|
||||
use Magento\Customer\Model\Indexer\Address\AttributeProvider;
|
||||
use Magento\Customer\Setup\CustomerSetup;
|
||||
use Magento\Customer\Setup\CustomerSetupFactory;
|
||||
use Magento\Eav\Model\Entity\Attribute\Set;
|
||||
use Magento\Eav\Model\Entity\Attribute\SetFactory;
|
||||
use Magento\Framework\Setup\ModuleDataSetupInterface;
|
||||
use Magento\Framework\Setup\Patch\PatchRevertableInterface;
|
||||
use Magento\Framework\Setup\Patch\SchemaPatchInterface;
|
||||
|
||||
class AddCityIdCustomerAddressAttribute implements SchemaPatchInterface, PatchRevertableInterface
|
||||
{
|
||||
|
||||
/**
|
||||
* @var ModuleDataSetupInterface
|
||||
*/
|
||||
private $moduleDataSetup;
|
||||
/**
|
||||
* @var CustomerSetup
|
||||
*/
|
||||
private $customerSetupFactory;
|
||||
/**
|
||||
* @var SetFactory
|
||||
*/
|
||||
private $attributeSetFactory;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param ModuleDataSetupInterface $moduleDataSetup
|
||||
* @param CustomerSetupFactory $customerSetupFactory
|
||||
* @param SetFactory $attributeSetFactory
|
||||
*/
|
||||
public function __construct(
|
||||
ModuleDataSetupInterface $moduleDataSetup,
|
||||
CustomerSetupFactory $customerSetupFactory,
|
||||
SetFactory $attributeSetFactory
|
||||
) {
|
||||
$this->moduleDataSetup = $moduleDataSetup;
|
||||
$this->customerSetupFactory = $customerSetupFactory;
|
||||
$this->attributeSetFactory = $attributeSetFactory;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function apply()
|
||||
{
|
||||
$this->moduleDataSetup->getConnection()->startSetup();
|
||||
/** @var CustomerSetup $customerSetup */
|
||||
$customerSetup = $this->customerSetupFactory->create(['setup' => $this->moduleDataSetup]);
|
||||
$customerEntity = $customerSetup->getEavConfig()->getEntityType(AttributeProvider::ENTITY);
|
||||
$attributeSetId = $customerEntity->getDefaultAttributeSetId();
|
||||
|
||||
/** @var $attributeSet Set */
|
||||
$attributeSet = $this->attributeSetFactory->create();
|
||||
$attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);
|
||||
|
||||
$customerSetup->addAttribute(AttributeProvider::ENTITY, 'city_code', [
|
||||
'type' => 'static',
|
||||
'label' => 'City',
|
||||
'input' => 'select',
|
||||
'source' => 'Stemcell\ChineseDirectory\Model\Entity\Attribute\Source\CityId',
|
||||
'required' => false,
|
||||
'visible' => true,
|
||||
'position' => 330,
|
||||
'system' => false,
|
||||
'user_defined' => false,
|
||||
'backend' => ''
|
||||
]);
|
||||
|
||||
$attribute = $customerSetup->getEavConfig()->getAttribute(AttributeProvider::ENTITY, 'city_code');
|
||||
$attribute->addData([
|
||||
'used_in_forms' => [
|
||||
'adminhtml_customer_address',
|
||||
'customer_address_edit',
|
||||
'customer_register_address'
|
||||
]
|
||||
]);
|
||||
$attribute->addData([
|
||||
'attribute_set_id' => $attributeSetId,
|
||||
'attribute_group_id' => $attributeGroupId
|
||||
|
||||
]);
|
||||
$attribute->save();
|
||||
|
||||
$this->moduleDataSetup->getConnection()->endSetup();
|
||||
}
|
||||
|
||||
public function revert()
|
||||
{
|
||||
$this->moduleDataSetup->getConnection()->startSetup();
|
||||
/** @var CustomerSetup $customerSetup */
|
||||
$customerSetup = $this->customerSetupFactory->create(['setup' => $this->moduleDataSetup]);
|
||||
$customerSetup->removeAttribute(\Magento\Customer\Model\Customer::ENTITY, 'city_id');
|
||||
|
||||
$this->moduleDataSetup->getConnection()->endSetup();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getAliases()
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function getDependencies()
|
||||
{
|
||||
return [
|
||||
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
+125
@@ -0,0 +1,125 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Stemcell\ChineseDirectory\Setup\Patch\Schema;
|
||||
|
||||
use Magento\Customer\Model\Indexer\Address\AttributeProvider;
|
||||
use Magento\Customer\Setup\CustomerSetup;
|
||||
use Magento\Customer\Setup\CustomerSetupFactory;
|
||||
use Magento\Eav\Model\Entity\Attribute\Set;
|
||||
use Magento\Eav\Model\Entity\Attribute\SetFactory;
|
||||
use Magento\Framework\Setup\ModuleDataSetupInterface;
|
||||
use Magento\Framework\Setup\Patch\PatchRevertableInterface;
|
||||
use Magento\Framework\Setup\Patch\SchemaPatchInterface;
|
||||
|
||||
class AddDistrictCustomerAddressAttribute implements SchemaPatchInterface, PatchRevertableInterface
|
||||
{
|
||||
|
||||
/**
|
||||
* @var ModuleDataSetupInterface
|
||||
*/
|
||||
private $moduleDataSetup;
|
||||
/**
|
||||
* @var CustomerSetup
|
||||
*/
|
||||
private $customerSetupFactory;
|
||||
/**
|
||||
* @var SetFactory
|
||||
*/
|
||||
private $attributeSetFactory;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param ModuleDataSetupInterface $moduleDataSetup
|
||||
* @param CustomerSetupFactory $customerSetupFactory
|
||||
* @param SetFactory $attributeSetFactory
|
||||
*/
|
||||
public function __construct(
|
||||
ModuleDataSetupInterface $moduleDataSetup,
|
||||
CustomerSetupFactory $customerSetupFactory,
|
||||
SetFactory $attributeSetFactory
|
||||
) {
|
||||
$this->moduleDataSetup = $moduleDataSetup;
|
||||
$this->customerSetupFactory = $customerSetupFactory;
|
||||
$this->attributeSetFactory = $attributeSetFactory;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function apply()
|
||||
{
|
||||
$this->moduleDataSetup->getConnection()->startSetup();
|
||||
/** @var CustomerSetup $customerSetup */
|
||||
$customerSetup = $this->customerSetupFactory->create(['setup' => $this->moduleDataSetup]);
|
||||
$customerEntity = $customerSetup->getEavConfig()->getEntityType(AttributeProvider::ENTITY);
|
||||
$attributeSetId = $customerEntity->getDefaultAttributeSetId();
|
||||
|
||||
$attributeSet = $this->attributeSetFactory->create();
|
||||
$attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);
|
||||
|
||||
$customerSetup->addAttribute(AttributeProvider::ENTITY, 'district', [
|
||||
'type' => 'static',
|
||||
'label' => 'District',
|
||||
'input' => 'text',
|
||||
'source' => '',
|
||||
'required' => false,
|
||||
'visible' => true,
|
||||
'position' => 333,
|
||||
'system' => false,
|
||||
'user_defined' => false,
|
||||
'backend' => 'Stemcell\ChineseDirectory\Model\Entity\Attribute\Backend\District'
|
||||
]);
|
||||
|
||||
$attribute = $customerSetup->getEavConfig()->getAttribute(AttributeProvider::ENTITY, 'district');
|
||||
$attribute->addData([
|
||||
'used_in_forms' => [
|
||||
'adminhtml_customer_address',
|
||||
'customer_address_edit',
|
||||
'customer_register_address'
|
||||
]
|
||||
]);
|
||||
$attribute->addData([
|
||||
'attribute_set_id' => $attributeSetId,
|
||||
'attribute_group_id' => $attributeGroupId
|
||||
|
||||
]);
|
||||
$attribute->save();
|
||||
|
||||
$this->moduleDataSetup->getConnection()->endSetup();
|
||||
}
|
||||
|
||||
public function revert()
|
||||
{
|
||||
$this->moduleDataSetup->getConnection()->startSetup();
|
||||
/** @var CustomerSetup $customerSetup */
|
||||
$customerSetup = $this->customerSetupFactory->create(['setup' => $this->moduleDataSetup]);
|
||||
$customerSetup->removeAttribute(\Magento\Customer\Model\Customer::ENTITY, 'district');
|
||||
|
||||
$this->moduleDataSetup->getConnection()->endSetup();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getAliases()
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function getDependencies()
|
||||
{
|
||||
return [
|
||||
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
+126
@@ -0,0 +1,126 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Stemcell\ChineseDirectory\Setup\Patch\Schema;
|
||||
|
||||
use Magento\Customer\Model\Indexer\Address\AttributeProvider;
|
||||
use Magento\Customer\Setup\CustomerSetup;
|
||||
use Magento\Customer\Setup\CustomerSetupFactory;
|
||||
use Magento\Eav\Model\Entity\Attribute\Set;
|
||||
use Magento\Eav\Model\Entity\Attribute\SetFactory;
|
||||
use Magento\Framework\Setup\ModuleDataSetupInterface;
|
||||
use Magento\Framework\Setup\Patch\PatchRevertableInterface;
|
||||
use Magento\Framework\Setup\Patch\SchemaPatchInterface;
|
||||
|
||||
class AddDistrictIdCustomerAddressAttribute implements SchemaPatchInterface, PatchRevertableInterface
|
||||
{
|
||||
|
||||
/**
|
||||
* @var ModuleDataSetupInterface
|
||||
*/
|
||||
private $moduleDataSetup;
|
||||
/**
|
||||
* @var CustomerSetup
|
||||
*/
|
||||
private $customerSetupFactory;
|
||||
/**
|
||||
* @var SetFactory
|
||||
*/
|
||||
private $attributeSetFactory;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param ModuleDataSetupInterface $moduleDataSetup
|
||||
* @param CustomerSetupFactory $customerSetupFactory
|
||||
* @param SetFactory $attributeSetFactory
|
||||
*/
|
||||
public function __construct(
|
||||
ModuleDataSetupInterface $moduleDataSetup,
|
||||
CustomerSetupFactory $customerSetupFactory,
|
||||
SetFactory $attributeSetFactory
|
||||
) {
|
||||
$this->moduleDataSetup = $moduleDataSetup;
|
||||
$this->customerSetupFactory = $customerSetupFactory;
|
||||
$this->attributeSetFactory = $attributeSetFactory;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function apply()
|
||||
{
|
||||
$this->moduleDataSetup->getConnection()->startSetup();
|
||||
/** @var CustomerSetup $customerSetup */
|
||||
$customerSetup = $this->customerSetupFactory->create(['setup' => $this->moduleDataSetup]);
|
||||
$customerEntity = $customerSetup->getEavConfig()->getEntityType(AttributeProvider::ENTITY);
|
||||
$attributeSetId = $customerEntity->getDefaultAttributeSetId();
|
||||
|
||||
/** @var $attributeSet Set */
|
||||
$attributeSet = $this->attributeSetFactory->create();
|
||||
$attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);
|
||||
|
||||
$customerSetup->addAttribute(AttributeProvider::ENTITY, 'district_code', [
|
||||
'type' => 'static',
|
||||
'label' => 'District',
|
||||
'input' => 'select',
|
||||
'source' => 'Stemcell\ChineseDirectory\Model\Entity\Attribute\Source\DistrictId',
|
||||
'required' => false,
|
||||
'visible' => true,
|
||||
'position' => 333,
|
||||
'system' => false,
|
||||
'user_defined' => false,
|
||||
'backend' => ''
|
||||
]);
|
||||
|
||||
$attribute = $customerSetup->getEavConfig()->getAttribute(AttributeProvider::ENTITY, 'district_code');
|
||||
$attribute->addData([
|
||||
'used_in_forms' => [
|
||||
'adminhtml_customer_address',
|
||||
'customer_address_edit',
|
||||
'customer_register_address'
|
||||
]
|
||||
]);
|
||||
$attribute->addData([
|
||||
'attribute_set_id' => $attributeSetId,
|
||||
'attribute_group_id' => $attributeGroupId
|
||||
|
||||
]);
|
||||
$attribute->save();
|
||||
|
||||
$this->moduleDataSetup->getConnection()->endSetup();
|
||||
}
|
||||
|
||||
public function revert()
|
||||
{
|
||||
$this->moduleDataSetup->getConnection()->startSetup();
|
||||
/** @var CustomerSetup $customerSetup */
|
||||
$customerSetup = $this->customerSetupFactory->create(['setup' => $this->moduleDataSetup]);
|
||||
$customerSetup->removeAttribute(\Magento\Customer\Model\Customer::ENTITY, 'district_id');
|
||||
|
||||
$this->moduleDataSetup->getConnection()->endSetup();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getAliases()
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function getDependencies()
|
||||
{
|
||||
return [
|
||||
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
+124
@@ -0,0 +1,124 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Stemcell\ChineseDirectory\Setup\Patch\Schema;
|
||||
|
||||
use Magento\Customer\Model\Indexer\Address\AttributeProvider;
|
||||
use Magento\Customer\Setup\CustomerSetup;
|
||||
use Magento\Customer\Setup\CustomerSetupFactory;
|
||||
use Magento\Eav\Model\Entity\Attribute\Set;
|
||||
use Magento\Eav\Model\Entity\Attribute\SetFactory;
|
||||
use Magento\Framework\Setup\ModuleDataSetupInterface;
|
||||
use Magento\Framework\Setup\Patch\SchemaPatchInterface;
|
||||
use Magento\Framework\Setup\Patch\PatchRevertableInterface;
|
||||
|
||||
class AddTownCustomerAddressAttribute implements SchemaPatchInterface, PatchRevertableInterface
|
||||
{
|
||||
|
||||
/**
|
||||
* @var ModuleDataSetupInterface
|
||||
*/
|
||||
private $moduleDataSetup;
|
||||
/**
|
||||
* @var CustomerSetup
|
||||
*/
|
||||
private $customerSetupFactory;
|
||||
/**
|
||||
* @var SetFactory
|
||||
*/
|
||||
private $attributeSetFactory;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param ModuleDataSetupInterface $moduleDataSetup
|
||||
* @param CustomerSetupFactory $customerSetupFactory
|
||||
* @param SetFactory $attributeSetFactory
|
||||
*/
|
||||
public function __construct(
|
||||
ModuleDataSetupInterface $moduleDataSetup,
|
||||
CustomerSetupFactory $customerSetupFactory,
|
||||
SetFactory $attributeSetFactory
|
||||
) {
|
||||
$this->moduleDataSetup = $moduleDataSetup;
|
||||
$this->customerSetupFactory = $customerSetupFactory;
|
||||
$this->attributeSetFactory = $attributeSetFactory;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function apply()
|
||||
{
|
||||
$this->moduleDataSetup->getConnection()->startSetup();
|
||||
/** @var CustomerSetup $customerSetup */
|
||||
$customerSetup = $this->customerSetupFactory->create(['setup' => $this->moduleDataSetup]);
|
||||
$customerEntity = $customerSetup->getEavConfig()->getEntityType(AttributeProvider::ENTITY);
|
||||
$attributeSetId = $customerEntity->getDefaultAttributeSetId();
|
||||
|
||||
/** @var $attributeSet Set */
|
||||
$attributeSet = $this->attributeSetFactory->create();
|
||||
$attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);
|
||||
|
||||
$customerSetup->addAttribute(AttributeProvider::ENTITY, 'town', [
|
||||
'type' => 'static',
|
||||
'label' => 'Town',
|
||||
'input' => 'text',
|
||||
'source' => '',
|
||||
'required' => false,
|
||||
'visible' => true,
|
||||
'position' => 330,
|
||||
'user_defined' => false,
|
||||
'system' => false,
|
||||
'backend' => 'Stemcell\ChineseDirectory\Model\Entity\Attribute\Backend\Town'
|
||||
]);
|
||||
|
||||
$attribute = $customerSetup->getEavConfig()->getAttribute(AttributeProvider::ENTITY, 'town');
|
||||
$attribute->addData([
|
||||
'used_in_forms' => [
|
||||
'adminhtml_customer_address',
|
||||
'customer_address_edit',
|
||||
'customer_register_address'
|
||||
]
|
||||
]);
|
||||
$attribute->addData([
|
||||
'attribute_set_id' => $attributeSetId,
|
||||
'attribute_group_id' => $attributeGroupId
|
||||
|
||||
]);
|
||||
$attribute->save();
|
||||
|
||||
$this->moduleDataSetup->getConnection()->endSetup();
|
||||
}
|
||||
|
||||
public function revert()
|
||||
{
|
||||
$this->moduleDataSetup->getConnection()->startSetup();
|
||||
/** @var CustomerSetup $customerSetup */
|
||||
$customerSetup = $this->customerSetupFactory->create(['setup' => $this->moduleDataSetup]);
|
||||
$customerSetup->removeAttribute(\Magento\Customer\Model\Customer::ENTITY, 'town');
|
||||
|
||||
$this->moduleDataSetup->getConnection()->endSetup();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getAliases()
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function getDependencies()
|
||||
{
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
+126
@@ -0,0 +1,126 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Stemcell\ChineseDirectory\Setup\Patch\Schema;
|
||||
|
||||
use Magento\Customer\Model\Indexer\Address\AttributeProvider;
|
||||
use Magento\Customer\Setup\CustomerSetup;
|
||||
use Magento\Customer\Setup\CustomerSetupFactory;
|
||||
use Magento\Eav\Model\Entity\Attribute\Set;
|
||||
use Magento\Eav\Model\Entity\Attribute\SetFactory;
|
||||
use Magento\Framework\Setup\ModuleDataSetupInterface;
|
||||
use Magento\Framework\Setup\Patch\SchemaPatchInterface;
|
||||
use Magento\Framework\Setup\Patch\PatchRevertableInterface;
|
||||
|
||||
class AddTownIdCustomerAddressAttribute implements SchemaPatchInterface, PatchRevertableInterface
|
||||
{
|
||||
|
||||
/**
|
||||
* @var ModuleDataSetupInterface
|
||||
*/
|
||||
private $moduleDataSetup;
|
||||
/**
|
||||
* @var CustomerSetup
|
||||
*/
|
||||
private $customerSetupFactory;
|
||||
/**
|
||||
* @var SetFactory
|
||||
*/
|
||||
private $attributeSetFactory;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param ModuleDataSetupInterface $moduleDataSetup
|
||||
* @param CustomerSetupFactory $customerSetupFactory
|
||||
* @param SetFactory $attributeSetFactory
|
||||
*/
|
||||
public function __construct(
|
||||
ModuleDataSetupInterface $moduleDataSetup,
|
||||
CustomerSetupFactory $customerSetupFactory,
|
||||
SetFactory $attributeSetFactory
|
||||
) {
|
||||
$this->moduleDataSetup = $moduleDataSetup;
|
||||
$this->customerSetupFactory = $customerSetupFactory;
|
||||
$this->attributeSetFactory = $attributeSetFactory;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function apply()
|
||||
{
|
||||
$this->moduleDataSetup->getConnection()->startSetup();
|
||||
/** @var CustomerSetup $customerSetup */
|
||||
$customerSetup = $this->customerSetupFactory->create(['setup' => $this->moduleDataSetup]);
|
||||
$customerEntity = $customerSetup->getEavConfig()->getEntityType(AttributeProvider::ENTITY);
|
||||
$attributeSetId = $customerEntity->getDefaultAttributeSetId();
|
||||
|
||||
/** @var $attributeSet Set */
|
||||
$attributeSet = $this->attributeSetFactory->create();
|
||||
$attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);
|
||||
|
||||
$customerSetup->addAttribute(AttributeProvider::ENTITY, 'town_code', [
|
||||
'type' => 'static',
|
||||
'label' => 'Town',
|
||||
'input' => 'select',
|
||||
'source' => 'Stemcell\ChineseDirectory\Model\Entity\Attribute\Source\TownId',
|
||||
'required' => false,
|
||||
'visible' => true,
|
||||
'position' => 334,
|
||||
'system' => false,
|
||||
'user_defined' => false,
|
||||
'backend' => ''
|
||||
]);
|
||||
|
||||
$attribute = $customerSetup->getEavConfig()->getAttribute(AttributeProvider::ENTITY, 'town_code');
|
||||
$attribute->addData([
|
||||
'used_in_forms' => [
|
||||
'adminhtml_customer_address',
|
||||
'customer_address_edit',
|
||||
'customer_register_address'
|
||||
]
|
||||
]);
|
||||
$attribute->addData([
|
||||
'attribute_set_id' => $attributeSetId,
|
||||
'attribute_group_id' => $attributeGroupId
|
||||
|
||||
]);
|
||||
$attribute->save();
|
||||
|
||||
$this->moduleDataSetup->getConnection()->endSetup();
|
||||
}
|
||||
|
||||
public function revert()
|
||||
{
|
||||
$this->moduleDataSetup->getConnection()->startSetup();
|
||||
/** @var CustomerSetup $customerSetup */
|
||||
$customerSetup = $this->customerSetupFactory->create(['setup' => $this->moduleDataSetup]);
|
||||
$customerSetup->removeAttribute(\Magento\Customer\Model\Customer::ENTITY, 'town_id');
|
||||
|
||||
$this->moduleDataSetup->getConnection()->endSetup();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getAliases()
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function getDependencies()
|
||||
{
|
||||
return [
|
||||
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
+122
@@ -0,0 +1,122 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Stemcell\ChineseDirectory\Setup\Patch\Schema;
|
||||
|
||||
use Magento\Customer\Model\Indexer\Address\AttributeProvider;
|
||||
use Magento\Customer\Setup\CustomerSetup;
|
||||
use Magento\Customer\Setup\CustomerSetupFactory;
|
||||
use Magento\Eav\Model\Entity\Attribute\SetFactory;
|
||||
use Magento\Framework\Setup\ModuleDataSetupInterface;
|
||||
use \Magento\Framework\Setup\Patch\SchemaPatchInterface;
|
||||
use Magento\Framework\Setup\Patch\PatchRevertableInterface;
|
||||
|
||||
class UpdateCustomerNameToNotRequired implements SchemaPatchInterface, PatchRevertableInterface
|
||||
{
|
||||
|
||||
/**
|
||||
* @var ModuleDataSetupInterface
|
||||
*/
|
||||
private $moduleDataSetup;
|
||||
/**
|
||||
* @var CustomerSetup
|
||||
*/
|
||||
private $customerSetupFactory;
|
||||
/**
|
||||
* @var SetFactory
|
||||
*/
|
||||
private $attributeSetFactory;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param ModuleDataSetupInterface $moduleDataSetup
|
||||
* @param CustomerSetupFactory $customerSetupFactory
|
||||
* @param SetFactory $attributeSetFactory
|
||||
*/
|
||||
public function __construct(
|
||||
ModuleDataSetupInterface $moduleDataSetup,
|
||||
CustomerSetupFactory $customerSetupFactory,
|
||||
SetFactory $attributeSetFactory
|
||||
) {
|
||||
$this->moduleDataSetup = $moduleDataSetup;
|
||||
$this->customerSetupFactory = $customerSetupFactory;
|
||||
$this->attributeSetFactory = $attributeSetFactory;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function apply()
|
||||
{
|
||||
$this->moduleDataSetup->getConnection()->startSetup();
|
||||
/** @var CustomerSetup $customerSetup */
|
||||
$customerSetup = $this->customerSetupFactory->create(['setup' => $this->moduleDataSetup]);
|
||||
|
||||
$entityAttributes = [
|
||||
'customer_address' => [
|
||||
'lastname' => [
|
||||
'is_visible' => false,
|
||||
'is_required' => false,
|
||||
'is_system' => false,
|
||||
'is_user_defined' => true
|
||||
],
|
||||
'street' => [
|
||||
'sort_order' => 355,
|
||||
'position' => 355,
|
||||
],
|
||||
'postcode' => [
|
||||
'is_visible' => false,
|
||||
'is_required' => false
|
||||
],
|
||||
'company' => [
|
||||
'is_visible' => false
|
||||
],
|
||||
'telephone' => [
|
||||
'sort_order' => 79,
|
||||
'validate_rules' => '{"max_text_length":11,"min_text_length":1}',
|
||||
'position' => 79,
|
||||
],
|
||||
'city' => [
|
||||
'backend_model' => \Stemcell\ChineseDirectory\Model\Entity\Attribute\Backend\City::class
|
||||
]
|
||||
],
|
||||
];
|
||||
|
||||
$customerSetup->upgradeAttributes($entityAttributes);
|
||||
$this->moduleDataSetup->getConnection()->endSetup();
|
||||
}
|
||||
|
||||
public function revert()
|
||||
{
|
||||
$this->moduleDataSetup->getConnection()->startSetup();
|
||||
/** @var CustomerSetup $customerSetup */
|
||||
$customerSetup = $this->customerSetupFactory->create(['setup' => $this->moduleDataSetup]);
|
||||
$customerSetup->updateAttribute(AttributeProvider::ENTITY,'lastname','is_required',1);
|
||||
$customerSetup->updateAttribute(AttributeProvider::ENTITY,'city','backend');
|
||||
$this->moduleDataSetup->getConnection()->endSetup();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getAliases()
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function getDependencies()
|
||||
{
|
||||
return [
|
||||
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
{
|
||||
"name": "stemcell/module-chinesedirectory",
|
||||
"description": "address for chinese administrative division",
|
||||
"type": "magento2-module",
|
||||
"license": "OSL-3.0",
|
||||
"authors": [
|
||||
{
|
||||
"name": "Liang Feng",
|
||||
"email": "344531331@qq.com"
|
||||
}
|
||||
],
|
||||
"minimum-stability": "dev",
|
||||
"require": {
|
||||
"experius/module-extracheckoutaddressfields": "*"
|
||||
},
|
||||
"autoload": {
|
||||
"files": [
|
||||
"registration.php"
|
||||
],
|
||||
"psr-4": {
|
||||
"Stemcell\\ChineseDirectory\\": ""
|
||||
}
|
||||
}
|
||||
}
|
||||
File diff suppressed because one or more lines are too long
@@ -0,0 +1,72 @@
|
||||
<?xml version="1.0" ?>
|
||||
<schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Setup/Declaration/Schema/etc/schema.xsd">
|
||||
<table name="customer_address_entity">
|
||||
<column name="city_code" nullable="true" xsi:type="int" comment="City"/>
|
||||
<column name="district_code" nullable="true" xsi:type="int" comment="District"/>
|
||||
<column name="district" nullable="true" xsi:type="varchar" comment="District"/>
|
||||
<column name="town_code" nullable="true" xsi:type="int" comment="Town"/>
|
||||
<column name="town" nullable="true" xsi:type="varchar" comment="Town"/>
|
||||
</table>
|
||||
<table name="sales_order_address">
|
||||
<column name="city_code" nullable="true" xsi:type="int" comment="City"/>
|
||||
<column name="district_code" nullable="true" xsi:type="int" comment="District"/>
|
||||
<column name="district" nullable="true" xsi:type="varchar" comment="District"/>
|
||||
<column name="town_code" nullable="true" xsi:type="int" comment="Town"/>
|
||||
<column name="town" nullable="true" xsi:type="varchar" comment="Town"/>
|
||||
</table>
|
||||
<table name="quote_address">
|
||||
<column name="city_code" nullable="true" xsi:type="int" comment="City"/>
|
||||
<column name="district_code" nullable="true" xsi:type="int" comment="District"/>
|
||||
<column name="district" nullable="true" xsi:type="varchar" comment="District"/>
|
||||
<column name="town_code" nullable="true" xsi:type="int" comment="Town"/>
|
||||
<column name="town" nullable="true" xsi:type="varchar" comment="Town"/>
|
||||
</table>
|
||||
<table name="directory_country_city" engine="innodb" comment="Directory Country City">
|
||||
<column name="code" nullable="false" xsi:type="int" unsigned="true" identity="false" comment="City Code"/>
|
||||
<column name="parent_code" nullable="false" xsi:type="int" unsigned="true" comment="Directory Country Region Code"/>
|
||||
<column name="name" nullable="false" xsi:type="varchar" comment="District"/>
|
||||
<index referenceId="directory_country_city_parent_code" indexType="btree">
|
||||
<column name="parent_code"/>
|
||||
</index>
|
||||
<constraint xsi:type="primary" referenceId="PRIMARY">
|
||||
<column name="code" />
|
||||
</constraint>
|
||||
<constraint xsi:type="unique" referenceId="directory_country_city_unique_code">
|
||||
<column name="code"/>
|
||||
</constraint>
|
||||
</table>
|
||||
<table name="directory_country_district" engine="innodb" comment="Directory Country District">
|
||||
<column name="code" nullable="false" xsi:type="int" unsigned="true" identity="false" comment="District Code"/>
|
||||
<column name="parent_code" nullable="false" xsi:type="int" unsigned="true" comment="Directory Country City Code"/>
|
||||
<column name="name" nullable="false" xsi:type="varchar" comment="District"/>
|
||||
<index referenceId="directory_country_city_parent_code" indexType="btree">
|
||||
<column name="parent_code"/>
|
||||
</index>
|
||||
<constraint xsi:type="foreign" referenceId="directory_country_district_code"
|
||||
table="directory_country_district" column="parent_code" referenceTable="directory_country_city"
|
||||
referenceColumn="code" onDelete="CASCADE"/>
|
||||
<constraint xsi:type="primary" referenceId="PRIMARY">
|
||||
<column name="code" />
|
||||
</constraint>
|
||||
<constraint xsi:type="unique" referenceId="directory_country_district_unique_code">
|
||||
<column name="code"/>
|
||||
</constraint>
|
||||
</table>
|
||||
<table name="directory_country_town" engine="innodb" comment="Directory Country Town">
|
||||
<column name="code" nullable="false" xsi:type="int" unsigned="true" identity="false" comment="Town Code"/>
|
||||
<column name="parent_code" nullable="false" xsi:type="int" unsigned="true" comment="Directory Country Town Code"/>
|
||||
<column name="name" nullable="false" xsi:type="varchar" comment="District"/>
|
||||
<index referenceId="directory_country_town_parent_code" indexType="btree">
|
||||
<column name="parent_code"/>
|
||||
</index>
|
||||
<constraint xsi:type="foreign" referenceId="directory_country_city_code"
|
||||
table="directory_country_town" column="parent_code" referenceTable="directory_country_district"
|
||||
referenceColumn="code" onDelete="CASCADE"/>
|
||||
<constraint xsi:type="primary" referenceId="PRIMARY">
|
||||
<column name="code" />
|
||||
</constraint>
|
||||
<constraint xsi:type="unique" referenceId="directory_country_town_unique_code">
|
||||
<column name="code"/>
|
||||
</constraint>
|
||||
</table>
|
||||
</schema>
|
||||
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" ?>
|
||||
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
|
||||
<type name="Magento\Quote\Model\Quote">
|
||||
<plugin name="set_extension_attribute_data_to_shipping_address" type="Stemcell\ChineseDirectory\Plugin\ShippingAddress\ExtensionAttribute" />
|
||||
</type>
|
||||
</config>
|
||||
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.0"?>
|
||||
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
|
||||
<event name="sales_model_service_quote_submit_before">
|
||||
<observer name="attach_extension_attribute" instance="Stemcell\ChineseDirectory\Observer\Order\Address\AttachExtensionAttributeObserver" />
|
||||
</event>
|
||||
</config>
|
||||
@@ -0,0 +1,24 @@
|
||||
<?xml version="1.0" ?>
|
||||
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Api/etc/extension_attributes.xsd">
|
||||
<extension_attributes for="Magento\Customer\Api\Data\AddressInterface">
|
||||
<attribute code="city_code" type="int"/>
|
||||
<attribute code="district_code" type="int"/>
|
||||
<attribute code="district" type="string"/>
|
||||
<attribute code="town_code" type="int"/>
|
||||
<attribute code="town" type="string"/>
|
||||
</extension_attributes>
|
||||
<extension_attributes for="Magento\Quote\Api\Data\AddressInterface">
|
||||
<attribute code="city_code" type="int"/>
|
||||
<attribute code="district_code" type="int"/>
|
||||
<attribute code="district" type="string"/>
|
||||
<attribute code="town_code" type="int"/>
|
||||
<attribute code="town" type="string"/>
|
||||
</extension_attributes>
|
||||
<extension_attributes for="Magento\Sales\Api\Data\OrderAddressInterface">
|
||||
<attribute code="city_code" type="int"/>
|
||||
<attribute code="district_code" type="int"/>
|
||||
<attribute code="district" type="string"/>
|
||||
<attribute code="town_code" type="int"/>
|
||||
<attribute code="town" type="string"/>
|
||||
</extension_attributes>
|
||||
</config>
|
||||
@@ -0,0 +1,76 @@
|
||||
<?xml version="1.0" ?>
|
||||
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:DataObject/etc/fieldset.xsd">
|
||||
<scope id="global">
|
||||
<fieldset id="extra_checkout_shipping_address_fields">
|
||||
<field name="city_code">
|
||||
<aspect name="to_order_address"/>
|
||||
<aspect name="to_quote_address"/>
|
||||
<aspect name="to_customer_address"/>
|
||||
</field>
|
||||
<field name="district_code">
|
||||
<aspect name="to_order_address"/>
|
||||
<aspect name="to_quote_address"/>
|
||||
<aspect name="to_customer_address"/>
|
||||
</field>
|
||||
<field name="district">
|
||||
<aspect name="to_order_address"/>
|
||||
<aspect name="to_quote_address"/>
|
||||
<aspect name="to_customer_address"/>
|
||||
</field>
|
||||
<field name="town_code">
|
||||
<aspect name="to_order_address"/>
|
||||
<aspect name="to_quote_address"/>
|
||||
<aspect name="to_customer_address"/>
|
||||
</field>
|
||||
<field name="town">
|
||||
<aspect name="to_order_address"/>
|
||||
<aspect name="to_quote_address"/>
|
||||
<aspect name="to_customer_address"/>
|
||||
</field>
|
||||
</fieldset>
|
||||
<fieldset id="extra_checkout_billing_address_fields">
|
||||
<field name="city_code">
|
||||
<aspect name="to_order_address"/>
|
||||
<aspect name="to_customer_address"/>
|
||||
</field>
|
||||
<field name="district_code">
|
||||
<aspect name="to_order_address"/>
|
||||
<aspect name="to_customer_address"/>
|
||||
</field>
|
||||
<field name="district">
|
||||
<aspect name="to_order_address"/>
|
||||
<aspect name="to_customer_address"/>
|
||||
</field>
|
||||
<field name="town_code">
|
||||
<aspect name="to_order_address"/>
|
||||
<aspect name="to_customer_address"/>
|
||||
</field>
|
||||
<field name="town">
|
||||
<aspect name="to_order_address"/>
|
||||
<aspect name="to_customer_address"/>
|
||||
</field>
|
||||
</fieldset>
|
||||
<fieldset id="sales_convert_quote_address">
|
||||
<field name="city_code">
|
||||
<aspect name="to_order_address"/>
|
||||
<aspect name="to_customer_address"/>
|
||||
</field>
|
||||
<field name="district_code">
|
||||
<aspect name="to_order_address"/>
|
||||
<aspect name="to_customer_address"/>
|
||||
</field>
|
||||
<field name="district">
|
||||
<aspect name="to_order_address"/>
|
||||
<aspect name="to_customer_address"/>
|
||||
</field>
|
||||
<field name="town_code">
|
||||
<aspect name="to_order_address"/>
|
||||
<aspect name="to_customer_address"/>
|
||||
</field>
|
||||
<field name="town">
|
||||
<aspect name="to_order_address"/>
|
||||
<aspect name="to_customer_address"/>
|
||||
</field>
|
||||
</fieldset>
|
||||
</scope>
|
||||
</config>
|
||||
@@ -0,0 +1,14 @@
|
||||
<?xml version="1.0"?>
|
||||
<!--
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
-->
|
||||
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
|
||||
<router id="standard">
|
||||
<route id="stemcell_chinesedirectory" frontName="stemcell_chinesedirectory">
|
||||
<module name="Stemcell_ChineseDirectory" />
|
||||
</route>
|
||||
</router>
|
||||
</config>
|
||||
@@ -0,0 +1,9 @@
|
||||
<?xml version="1.0" ?>
|
||||
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
|
||||
<module name="Stemcell_ChineseDirectory">
|
||||
<sequence>
|
||||
<module name="Magento_Customer"/>
|
||||
<module name="Magento_Directory"/>
|
||||
</sequence>
|
||||
</module>
|
||||
</config>
|
||||
@@ -0,0 +1,9 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
use Magento\Framework\Component\ComponentRegistrar;
|
||||
|
||||
ComponentRegistrar::register(ComponentRegistrar::MODULE, 'Stemcell_ChineseDirectory', __DIR__);
|
||||
|
||||
@@ -0,0 +1,227 @@
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
|
||||
define([
|
||||
'jquery',
|
||||
'mage/template',
|
||||
'underscore',
|
||||
'jquery-ui-modules/widget',
|
||||
'mage/validation'
|
||||
], function ($, mageTemplate, _) {
|
||||
'use strict';
|
||||
|
||||
$.widget('stemcell.administrativeUpdater', {
|
||||
options: {
|
||||
regionTemplate:
|
||||
'<option value="<%- data.value %>" <% if (data.isSelected) { %>selected="selected"<% } %>>' +
|
||||
'<%- data.title %>' +
|
||||
'</option>',
|
||||
isRegionRequired: true,
|
||||
currentCity: null,
|
||||
},
|
||||
|
||||
/**
|
||||
*
|
||||
* @private
|
||||
*/
|
||||
_create: function () {
|
||||
// this._initCountryElement();
|
||||
|
||||
this.currentCityOption = this.options.currentCity;
|
||||
this.regionTmpl = mageTemplate(this.options.regionTemplate);
|
||||
|
||||
// this._updateRegion(this.element.find('option:selected').val());
|
||||
|
||||
this.element.on('change', $.proxy(function (e) {
|
||||
this.setOption = false;
|
||||
this.currentCityOption = $(e.target).val();
|
||||
console.log($(e.target).val());
|
||||
$(this.options.subListId).val('');
|
||||
this._updateRegion($(e.target).val());
|
||||
}, this));
|
||||
},
|
||||
|
||||
/**
|
||||
*
|
||||
* @private
|
||||
*/
|
||||
_initCountryElement: function () {
|
||||
|
||||
if (this.options.isMultipleCountriesAllowed) {
|
||||
this.element.parents('div.field').show();
|
||||
this.element.on('change', $.proxy(function (e) {
|
||||
// clear region inputs on country change
|
||||
$(this.options.subListId).val('');
|
||||
this._updateRegion($(e.target).val());
|
||||
}, this));
|
||||
|
||||
if (this.options.isCountryRequired) {
|
||||
this.element.addClass('required-entry');
|
||||
this.element.parents('div.field').addClass('required');
|
||||
}
|
||||
} else {
|
||||
this.element.parents('div.field').hide();
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* Remove options from dropdown list
|
||||
*
|
||||
* @param {Object} selectElement - jQuery object for dropdown list
|
||||
* @private
|
||||
*/
|
||||
_removeSelectOptions: function (selectElement) {
|
||||
selectElement.find('option').each(function (index) {
|
||||
if (index) {
|
||||
$(this).remove();
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
* Render dropdown list
|
||||
* @param {Object} selectElement - jQuery object for dropdown list
|
||||
* @param {String} key - region code
|
||||
* @param {Object} value - region object
|
||||
* @private
|
||||
*/
|
||||
_renderSelectOption: function (selectElement, key, value) {
|
||||
selectElement.append($.proxy(function () {
|
||||
var name = value.name.replace(/[!"#$%&'()*+,.\/:;<=>?@[\\\]^`{|}~]/g, '\\$&'),
|
||||
tmplData,
|
||||
tmpl;
|
||||
|
||||
if (value.code && $(name).is('span')) {
|
||||
key = value.code;
|
||||
value.name = $(name).text();
|
||||
}
|
||||
|
||||
tmplData = {
|
||||
value: value.code,
|
||||
title: value.name,
|
||||
isSelected: false
|
||||
};
|
||||
|
||||
if (this.options.selectedValue === key) {
|
||||
tmplData.isSelected = true;
|
||||
}
|
||||
|
||||
tmpl = this.regionTmpl({
|
||||
data: tmplData
|
||||
});
|
||||
|
||||
return $(tmpl);
|
||||
}, this));
|
||||
},
|
||||
|
||||
/**
|
||||
* Takes clearError callback function as first option
|
||||
* If no form is passed as option, look up the closest form and call clearError method.
|
||||
* @private
|
||||
*/
|
||||
_clearError: function () {
|
||||
var args = ['clearError', this.options.subListId, this.options.regionInputId, this.options.postcodeId];
|
||||
|
||||
if (this.options.clearError && typeof this.options.clearError === 'function') {
|
||||
this.options.clearError.call(this);
|
||||
} else {
|
||||
if (!this.options.form) {
|
||||
this.options.form = this.element.closest('form').length ? $(this.element.closest('form')[0]) : null;
|
||||
}
|
||||
|
||||
this.options.form = $(this.options.form);
|
||||
|
||||
this.options.form && this.options.form.data('validator') &&
|
||||
this.options.form.validation.apply(this.options.form, _.compact(args));
|
||||
|
||||
// Clean up errors on region & zip fix
|
||||
$(this.options.regionInputId).removeClass('mage-error').parent().find('[generated]').remove();
|
||||
$(this.options.subListId).removeClass('mage-error').parent().find('[generated]').remove();
|
||||
$(this.options.postcodeId).removeClass('mage-error').parent().find('[generated]').remove();
|
||||
}
|
||||
},
|
||||
|
||||
_getSubAdministrative: function(administrativeId) {
|
||||
let data = [];
|
||||
let self = this;
|
||||
data.push({"name": "administrativeId", "value": administrativeId});
|
||||
data.push({"name": "type", "value": this.options.type});
|
||||
data.push({"name": "form_key", "value": $('input[name="form_key"]').val()});
|
||||
return $.ajax({
|
||||
url: self.options.requestUrl,
|
||||
type: 'get',
|
||||
async: false,
|
||||
dataType: 'json',
|
||||
context: self,
|
||||
data: data
|
||||
});
|
||||
},
|
||||
/**
|
||||
* Update dropdown list based on the country selected
|
||||
*
|
||||
* @param {String} country - 2 uppercase letter for country code
|
||||
* @private
|
||||
*/
|
||||
_updateRegion: function (country) {
|
||||
// Clear validation error messages
|
||||
var regionList = $(this.options.subListId),
|
||||
regionInput = $(this.options.regionInputId),
|
||||
label = regionList.parent().siblings('label'),
|
||||
container = regionList.parents('div.field'),
|
||||
regionsEntries,
|
||||
regionId,
|
||||
regionData;
|
||||
|
||||
this._clearError();
|
||||
// this._checkRegionRequired(country);
|
||||
|
||||
$.when(this._getSubAdministrative(this.element.find('option:selected').val()))
|
||||
.done(function (data) {
|
||||
let administrative = data.data.administrative;
|
||||
this._removeSelectOptions(regionList);
|
||||
regionsEntries = _.pairs(administrative);
|
||||
|
||||
$.each(regionsEntries, $.proxy(function (key, value) {
|
||||
regionId = value[0];
|
||||
regionData = value[1];
|
||||
this._renderSelectOption(regionList, regionId, regionData);
|
||||
}, this));
|
||||
|
||||
if (this.currentRegionOption) {
|
||||
regionList.val(this.currentRegionOption);
|
||||
}
|
||||
|
||||
if (this.setOption) {
|
||||
regionList.find('option').filter(function () {
|
||||
return this.text === regionInput.val();
|
||||
}).attr('selected', true);
|
||||
}
|
||||
})
|
||||
|
||||
// Add defaultvalue attribute to state/province select element
|
||||
regionList.attr('defaultvalue', this.options.selectedValue);
|
||||
this.options.form.find('[type="submit"]').removeAttr('disabled').show();
|
||||
},
|
||||
|
||||
/**
|
||||
* Check if the selected country has a mandatory region selection
|
||||
*
|
||||
* @param {String} country - Code of the country - 2 uppercase letter for country code
|
||||
* @private
|
||||
*/
|
||||
_checkRegionRequired: function (country) {
|
||||
var self = this;
|
||||
|
||||
this.options.isRegionRequired = false;
|
||||
$.each(this.options.regionJson.config['regions_required'], function (index, elem) {
|
||||
if (elem === country) {
|
||||
self.options.isRegionRequired = true;
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
return $.stemcell.administrativeUpdater;
|
||||
});
|
||||
Reference in New Issue
Block a user