回答:
デフォルトでは、属性設定配列で「グループ」の値を指定すると、属性はすべての属性セットのそのグループに追加されます。
$eavSetup->addAttribute('catalog_product', 'my_attribute', [
...
'group' => 'General', // or some other group name
...
]);
これは役に立ちます:-
SetupフォルダーにInstallData.phpを作成し、このコードをファイルに貼り付けます
<?php
namespace [Vendor]\[Module]\Setup;
use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
class InstallData implements InstallDataInterface
{
private $eavSetupFactory;
public function __construct(EavSetupFactory $eavSetupFactory)
{
$this->eavSetupFactory = $eavSetupFactory;
}
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
/** @var EavSetup $eavSetup */
$eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
/**
* Add attributes to the eav/attribute
*/
$eavSetup->removeAttribute(\Magento\Catalog\Model\Product::ENTITY,'background_image');
$eavSetup->addAttribute(
\Magento\Catalog\Model\Product::ENTITY,
'background_image',/* Custom Attribute Code */
[
'type' => 'varchar',/* Data type in which formate your value save in database*/
'backend' => '',
'frontend' => '',
'label' => 'Background Image', /* lablel of your attribute*/
'input' => 'media_image',
'class' => '',
'source' => '',
/* Source of your select type custom attribute options*/
'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE,
/*Scope of your attribute */
'visible' => false,
'required' => false,
'user_defined' => false,
'default' => '',
'searchable' => false,
'filterable' => false,
'comparable' => false,
'visible_on_front' => false,
'used_in_product_listing' => false,
'unique' => false
]
);
$entityTypeId = $eavSetup->getEntityTypeId(\Magento\Catalog\Model\Product::ENTITY);
$attributeSetIds = $eavSetup->getAllAttributeSetIds($entityTypeId);
foreach ($attributeSetIds as $attributeSetId) {
/**
* getAttributeGroupId($entityTypeId, $attributeSetId, "Group_Code");
*
*/
$groupId = $eavSetup->getAttributeGroupId($entityTypeId, $attributeSetId, "image-management");
$eavSetup->addAttributeToGroup(
$entityTypeId,
$attributeSetId,
$groupId,
'background_image',
null
);
}
}
}
これは、すべての属性セットに属性を追加できるスクリプトです。
手順1:-SetupフォルダーにInstallData.phpを作成し、このコードをファイルに貼り付けます。
namespace Ktpl\BrandManagement\Setup;
use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
class InstallData implements InstallDataInterface
{
/**
* EAV setup factory
*
* @var EavSetupFactory
*/
private $eavSetupFactory;
/**
* Init
*
* @param EavSetupFactory $eavSetupFactory
*/
public function __construct(EavSetupFactory $eavSetupFactory)
{
$this->eavSetupFactory = $eavSetupFactory;
}
/**
* {@inheritdoc}
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*/
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
/** @var EavSetup $eavSetup */
$eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
/**
* Add attributes to the eav/attribute
*/
$eavSetup->addAttribute(
\Magento\Catalog\Model\Product::ENTITY,
'shop_by_brand',
[
'type' => 'varchar',
'backend' => '',
'frontend' => '',
'label' => 'Brand',
'input' => 'multiselect',
'class' => '',
'source' => '',
'backend' => 'Magento\Eav\Model\Entity\Attribute\Backend\ArrayBackend',
'global' => \Magento\Catalog\Model\ResourceModel\Eav\Attribute::SCOPE_GLOBAL,
'visible' => true,
'required' => false,
'user_defined' => false,
'default' => 0,
'searchable' => false,
'filterable' => true,
'comparable' => false,
'visible_on_front' => true,
'used_in_product_listing' => true,
'unique' => false,
'apply_to' => ''
]
);
}
}
ご不明な点がありましたらお知らせください。必要に応じて余分なスペースを削除してください。