Guest User

Untitled

a guest
Jan 12th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.42 KB | None | 0 0
  1. use MagentoCustomerModelCustomer;
  2. use MagentoEavModelConfig;
  3. use MagentoEavModelEntityAttributeSetFactory as AttributeSetFactory;
  4. use MagentoEavSetupEavSetupFactory;
  5. use MagentoFrameworkSetupInstallDataInterface;
  6. use MagentoFrameworkSetupModuleContextInterface;
  7. use MagentoFrameworkSetupModuleDataSetupInterface;
  8.  
  9. /**
  10. * Class InstallData
  11. */
  12. class InstallData implements InstallDataInterface
  13. {
  14. /**
  15. * @var EavSetupFactory
  16. */
  17. protected $eavSetupFactory;
  18.  
  19. /**
  20. * @var Config
  21. */
  22. protected $eavConfig;
  23.  
  24. /**
  25. * @var AttributeSetFactory
  26. */
  27. protected $attributeSetFactory;
  28.  
  29. /**
  30. * InstallData constructor.
  31. *
  32. * @param EavSetupFactory $eavSetupFactory
  33. * @param Config $eavConfig
  34. * @param AttributeSetFactory $attributeSetFactory
  35. */
  36. public function __construct(
  37. EavSetupFactory $eavSetupFactory,
  38. Config $eavConfig,
  39. AttributeSetFactory $attributeSetFactory
  40. ) {
  41. $this->eavSetupFactory = $eavSetupFactory;
  42. $this->eavConfig = $eavConfig;
  43. $this->attributeSetFactory = $attributeSetFactory;
  44. }
  45.  
  46. /**
  47. * Install Data
  48. *
  49. * @param ModuleDataSetupInterface $setup
  50. * @param ModuleContextInterface $context
  51. * @throws Exception
  52. */
  53. public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
  54. {
  55. $setup->startSetup();
  56.  
  57. $this->addCustomerAttributes($setup);
  58.  
  59. $setup->endSetup();
  60. }
  61.  
  62. /**
  63. * Add Customer Attributes
  64. *
  65. * @param $setup
  66. */
  67. private function addCustomerAttributes(ModuleDataSetupInterface $setup)
  68. {
  69. $customerAttributes = [
  70. 'sample_attribute' => [
  71. 'properties' => [
  72. 'type' => 'varchar',
  73. 'label' => 'Sample Attribute',
  74. 'input' => 'text',
  75. 'required' => false,
  76. 'sort_order' => 100,
  77. 'system' => false,
  78. 'position' => 1000,
  79. ],
  80. 'used_in_forms' => [
  81. 'adminhtml_customer',
  82. 'adminhtml_checkout',
  83. 'customer_account_create',
  84. 'customer_account_edit',
  85. 'checkout_register',
  86. ]
  87. ],
  88. ];
  89.  
  90. /** @var MagentoEavSetupEavSetup $eavSetup */
  91. $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
  92.  
  93. $customerEntity = $this->eavConfig->getEntityType(Customer::ENTITY);
  94. $attributeSetId = $customerEntity->getDefaultAttributeSetId();
  95.  
  96. /** @var MagentoEavModelEntityAttributeSet $attributeSet */
  97. $attributeSet = $this->attributeSetFactory->create();
  98. $attributeSetGroupId = $attributeSet->getDefaultGroupId($attributeSetId);
  99.  
  100. foreach ($customerAttributes as $attributeCode => $attributeData) {
  101. // Create Attribute
  102. $eavSetup->addAttribute(Customer::ENTITY, $attributeCode, $attributeData['properties']);
  103.  
  104. // Add Attribute to Set, Group, and Forms
  105. $attribute = $this->eavConfig->getAttribute(Customer::ENTITY, $attributeCode);
  106. $attribute->addData([
  107. 'attribute_set_id' => $attributeSetId,
  108. 'attribute_group_id' => $attributeSetGroupId,
  109. 'used_in_forms' => $attributeData['used_in_forms']
  110. ]);
  111.  
  112. $attribute->save();
  113. }
  114. }
  115. }
Add Comment
Please, Sign In to add comment