97 lines
3.5 KiB
PHP
97 lines
3.5 KiB
PHP
<?php
|
|
namespace Shopkeeper\VendorNotes\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;
|
|
use Magento\Catalog\Model\Product;
|
|
use Magento\Eav\Model\Entity\Attribute\SetFactory as AttributeSetFactory;
|
|
use Magento\Eav\Model\ResourceModel\Entity\Attribute\Set\CollectionFactory as AttributeSetCollectionFactory;
|
|
use Magento\Catalog\Model\Product\Attribute\Backend\Sku;
|
|
|
|
class InstallData implements InstallDataInterface
|
|
{
|
|
private $eavSetupFactory;
|
|
private $attributeSetCollectionFactory;
|
|
private $attributeSetFactory;
|
|
|
|
public function __construct(
|
|
EavSetupFactory $eavSetupFactory,
|
|
AttributeSetCollectionFactory $attributeSetCollectionFactory,
|
|
AttributeSetFactory $attributeSetFactory
|
|
) {
|
|
$this->eavSetupFactory = $eavSetupFactory;
|
|
$this->attributeSetCollectionFactory = $attributeSetCollectionFactory;
|
|
$this->attributeSetFactory = $attributeSetFactory;
|
|
}
|
|
|
|
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
|
|
{
|
|
$setup->startSetup();
|
|
$eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
|
|
|
|
// Create the attribute
|
|
$eavSetup->addAttribute(
|
|
Product::ENTITY,
|
|
'vendor',
|
|
[
|
|
'type' => 'int',
|
|
'label' => 'Vendor',
|
|
'input' => 'select',
|
|
'source' => \Magento\Eav\Model\Entity\Attribute\Source\Table::class,
|
|
'required' => false,
|
|
'sort_order' => 50,
|
|
'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
|
|
'group' => 'General',
|
|
'used_in_product_listing' => true,
|
|
'visible_on_front' => false,
|
|
'is_user_defined' => true,
|
|
'option' => [
|
|
'values' => [
|
|
'Thermo Fisher',
|
|
'Orion',
|
|
'Nalgene',
|
|
'Thermo Fisher Parts'
|
|
]
|
|
]
|
|
]
|
|
);
|
|
|
|
// Assign the attribute to all existing attribute sets
|
|
$attributeId = $eavSetup->getAttributeId(Product::ENTITY, 'vendor');
|
|
$entityTypeId = $eavSetup->getEntityTypeId(Product::ENTITY);
|
|
|
|
$attributeSetCollection = $this->attributeSetCollectionFactory->create();
|
|
$attributeSetCollection->addFieldToFilter('entity_type_id', $entityTypeId);
|
|
|
|
foreach ($attributeSetCollection as $attributeSet) {
|
|
// Get the General group ID, or fallback to the first available group
|
|
try {
|
|
$groupId = $eavSetup->getAttributeGroupId(
|
|
$entityTypeId,
|
|
$attributeSet->getId(),
|
|
'General'
|
|
);
|
|
} catch (\Exception $e) {
|
|
// If General group doesn't exist, get the default group
|
|
$groupId = $eavSetup->getDefaultAttributeGroupId(
|
|
$entityTypeId,
|
|
$attributeSet->getId()
|
|
);
|
|
}
|
|
|
|
// Add attribute to the set
|
|
$eavSetup->addAttributeToGroup(
|
|
$entityTypeId,
|
|
$attributeSet->getId(),
|
|
$groupId,
|
|
$attributeId,
|
|
null // Sort order
|
|
);
|
|
}
|
|
|
|
$setup->endSetup();
|
|
}
|
|
} |