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

149 lines
4.5 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 AddWechatAvatarAttribute implements DataPatchInterface, PatchRevertableInterface
{
/**
* AddWechatAvatarAttribute 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);
// 检查wechat_avatar属性是否已存在
$attribute = $customerSetup->getAttribute(
\Magento\Customer\Model\Customer::ENTITY,
'wechat_avatar'
);
if (!$attribute) {
// 添加wechat_avatar属性
$customerSetup->addAttribute(
\Magento\Customer\Model\Customer::ENTITY,
'wechat_avatar',
[
'type' => 'varchar',
'label' => '微信头像',
'input' => 'text',
'required' => false,
'visible' => false,
'user_defined' => true,
'position' => 1000,
'system' => 0,
'is_used_in_grid' => true,
'is_visible_in_grid' => false,
'is_filterable_in_grid' => true, //建立普通索引 (INDEX)
//'is_searchable_in_grid' => true, //将生成 FULLTEXT 索引
]
);
// 将属性添加到表单
$attribute = $customerSetup->getEavConfig()->getAttribute(
\Magento\Customer\Model\Customer::ENTITY,
'wechat_avatar'
);
$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 [
AddWechatOpenidAttribute::class
];
}
/**
* {@inheritdoc}
*/
public function getAliases()
{
return [];
}
/**
* {@inheritdoc}
*/
public function revert()
{
$this->moduleDataSetup->getConnection()->startSetup();
/** @var CustomerSetup $customerSetup */
$customerSetup = $this->customerSetupFactory->create(['setup' => $this->moduleDataSetup]);
// 删除wechat_avatar属性
$customerSetup->removeAttribute(
\Magento\Customer\Model\Customer::ENTITY,
'wechat_avatar'
);
$this->moduleDataSetup->getConnection()->endSetup();
}
}