75 lines
2.8 KiB
PHP
75 lines
2.8 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;
|
||
|
|
|
||
|
|
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,
|
||
|
|
'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) {
|
||
|
|
$groupId = $eavSetup->getAttributeGroupId($entityTypeId, $attributeSet->getId(), 'General');
|
||
|
|
$eavSetup->addAttributeToGroup($entityTypeId, $attributeSet->getId(), $groupId, $attributeId, null);
|
||
|
|
}
|
||
|
|
|
||
|
|
$setup->endSetup();
|
||
|
|
}
|
||
|
|
}
|