1 回答
TA贡献1815条经验 获得超10个赞
好像你的属性参数没有完成。尝试改用此代码。
<?php
namespace JKM\CustomModule\Patch\Data;
use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\Patch\DataPatchInterface;
class AddShopAttribute implements DataPatchInterface
{
/** @var ModuleDataSetupInterface */
private $moduleDataSetup;
/** @var EavSetupFactory */
private $eavSetupFactory;
/**
* @param ModuleDataSetupInterface $moduleDataSetup
* @param EavSetupFactory $eavSetupFactory
*/
public function __construct(
ModuleDataSetupInterface $moduleDataSetup,
EavSetupFactory $eavSetupFactory
) {
$this->moduleDataSetup = $moduleDataSetup;
$this->eavSetupFactory = $eavSetupFactory;
}
/**
* {@inheritdoc}
*/
public function apply()
{
/** @var EavSetup $eavSetup */
$eavSetup = $this->eavSetupFactory->create(['setup' => $this->moduleDataSetup]);
$eavSetup->addAttribute(\Magento\Catalog\Model\Product::ENTITY, 'shop', [
'type' => 'int',
'backend' => '',
'frontend' => '',
'label' => 'Shop',
'input' => 'select',
'class' => '',
'source' => \Magento\Catalog\Model\Product\Attribute\Source\Boolean::class,
'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
'visible' => true,
'required' => true,
'user_defined' => false,
'default' => '',
'searchable' => false,
'filterable' => false,
'comparable' => false,
'visible_on_front' => false,
'used_in_product_listing' => true,
'unique' => false,
]);
$groupName = 'Autosettings';
$entityTypeId = $catalogSetup->getEntityTypeId(\Magento\Catalog\Model\Product::ENTITY);
$attributeSetId = $catalogSetup->getAttributeSetId($entityTypeId, 'Default');
$attribute = $catalogSetup->getAttribute($entityTypeId, 'shop');
if ($attribute) {
$catalogSetup->addAttributeToGroup(
$entityTypeId,
$attributeSetId,
$groupName,
$attribute['attribute_id'],
60
);
}
}
/**
* {@inheritdoc}
*/
public static function getDependencies()
{
return [];
}
/**
* {@inheritdoc}
*/
public function getAliases()
{
return [];
}
}
- 1 回答
- 0 关注
- 171 浏览
添加回答
举报