init
This commit is contained in:
@@ -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 [
|
||||
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user