Files
stemcell/app/code/Stemcell/Commerce/Setup/Patch/Data/AddCompanyAttribute.php
T
2026-09-15 10:26:34 +08:00

147 lines
4.3 KiB
PHP

<?php
/**
* 2025 D1m Group
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0) that is available
* through the world-wide-web at this URL: http://www.opensource.org/licenses/OSL-3.0
*
* @copyright 2025 D1m Group
* @license http://www.opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
*/
namespace Stemcell\Commerce\Setup\Patch\Data;
use Magento\Customer\Model\Customer;
use Magento\Customer\Setup\CustomerSetup;
use Magento\Customer\Setup\CustomerSetupFactory as MagentoCustomerSetupFactory;
use Magento\Eav\Model\Entity\Attribute\SetFactory;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\Patch\DataPatchInterface;
use Magento\Framework\Setup\Patch\PatchRevertableInterface;
/**
* 添加用户公司属性的数据
*/
class AddCompanyAttribute implements DataPatchInterface, PatchRevertableInterface
{
/**
* InitializationData constructor.
*
* @param ModuleDataSetupInterface $moduleDataSetup
* @param MagentoCustomerSetupFactory $customerSetupFactory
*/
public function __construct(
private ModuleDataSetupInterface $moduleDataSetup,
private MagentoCustomerSetupFactory $customerSetupFactory,
private SetFactory $attributeSetFactory
) {
// 使用PHP 7.4+构造函数属性提升,无需单独赋值
}
/**
* {@inheritdoc}
*/
public function apply()
{
$this->moduleDataSetup->getConnection()->startSetup();
/** @var CustomerSetup $customerSetup */
$customerSetup = $this->customerSetupFactory->create(['setup' => $this->moduleDataSetup]);
$customerEntity = $customerSetup->getEavConfig()->getEntityType(Customer::ENTITY);
$attributeSetId = $customerEntity->getDefaultAttributeSetId();
$attributeSet = $this->attributeSetFactory->create();
$attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);
// 检查公司属性是否已存在
$attribute = $customerSetup->getAttribute(
\Magento\Customer\Model\Customer::ENTITY,
'company'
);
if (!$attribute) {
// 添加公司属性
$customerSetup->addAttribute(
\Magento\Customer\Model\Customer::ENTITY,
'company',
[
'type' => 'varchar',
'label' => '公司/单位',
'input' => 'text',
'required' => false,
'visible' => true,
'user_defined' => true,
'position' => 999,
'system' => 0,
'is_used_in_grid' => true,
'is_visible_in_grid' => true,
'is_filterable_in_grid' => true,
'is_searchable_in_grid' => true,
]
);
// 将属性添加到表单
$attribute = $customerSetup->getEavConfig()->getAttribute(
\Magento\Customer\Model\Customer::ENTITY,
'company'
);
$attribute->addData([
'used_in_forms' => [
'adminhtml_customer',
'customer_account_create',
'customer_account_edit'
]
]);
$attribute->addData([
'attribute_set_id' => $attributeSetId,
'attribute_group_id' => $attributeGroupId
]);
$attribute->save();
}
$this->moduleDataSetup->getConnection()->endSetup();
}
/**
* {@inheritdoc}
*/
public static function getDependencies()
{
return [];
}
/**
* {@inheritdoc}
*/
public function getAliases()
{
return [];
}
/**
* {@inheritdoc}
*/
public function revert()
{
$this->moduleDataSetup->getConnection()->startSetup();
/** @var CustomerSetup $customerSetup */
$customerSetup = $this->customerSetupFactory->create(['setup' => $this->moduleDataSetup]);
// 删除公司属性
$customerSetup->removeAttribute(
\Magento\Customer\Model\Customer::ENTITY,
'company'
);
$this->moduleDataSetup->getConnection()->endSetup();
}
}