DefaultCustomerGroupsAndAttributes.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Customer\Setup\Patch\Data;
  7. use Magento\Customer\Setup\CustomerSetup;
  8. use Magento\Customer\Setup\CustomerSetupFactory;
  9. use Magento\Framework\Module\Setup\Migration;
  10. use Magento\Framework\Setup\ModuleDataSetupInterface;
  11. use Magento\Framework\App\ResourceConnection;
  12. use Magento\Framework\Setup\Patch\DataPatchInterface;
  13. use Magento\Framework\Setup\Patch\PatchVersionInterface;
  14. /**
  15. * Class DefaultCustomerGroupsAndAttributes
  16. * @package Magento\Customer\Setup\Patch
  17. */
  18. class DefaultCustomerGroupsAndAttributes implements DataPatchInterface, PatchVersionInterface
  19. {
  20. /**
  21. * @var CustomerSetupFactory
  22. */
  23. private $customerSetupFactory;
  24. /**
  25. * @var ModuleDataSetupInterface
  26. */
  27. private $moduleDataSetup;
  28. /**
  29. * DefaultCustomerGroupsAndAttributes constructor.
  30. * @param CustomerSetupFactory $customerSetupFactory
  31. * @param ModuleDataSetupInterface $moduleDataSetup
  32. */
  33. public function __construct(
  34. CustomerSetupFactory $customerSetupFactory,
  35. \Magento\Framework\Setup\ModuleDataSetupInterface $moduleDataSetup
  36. ) {
  37. $this->customerSetupFactory = $customerSetupFactory;
  38. $this->moduleDataSetup = $moduleDataSetup;
  39. }
  40. /**
  41. * {@inheritdoc}
  42. * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  43. */
  44. public function apply()
  45. {
  46. /** @var CustomerSetup $customerSetup */
  47. $customerSetup = $this->customerSetupFactory->create(['setup' => $this->moduleDataSetup]);
  48. // insert default customer groups
  49. $this->moduleDataSetup->getConnection()->insertForce(
  50. $this->moduleDataSetup->getTable('customer_group'),
  51. ['customer_group_id' => 0, 'customer_group_code' => 'NOT LOGGED IN', 'tax_class_id' => 3]
  52. );
  53. $this->moduleDataSetup->getConnection()->insertForce(
  54. $this->moduleDataSetup->getTable('customer_group'),
  55. ['customer_group_id' => 1, 'customer_group_code' => 'General', 'tax_class_id' => 3]
  56. );
  57. $this->moduleDataSetup->getConnection()->insertForce(
  58. $this->moduleDataSetup->getTable('customer_group'),
  59. ['customer_group_id' => 2, 'customer_group_code' => 'Wholesale', 'tax_class_id' => 3]
  60. );
  61. $this->moduleDataSetup->getConnection()->insertForce(
  62. $this->moduleDataSetup->getTable('customer_group'),
  63. ['customer_group_id' => 3, 'customer_group_code' => 'Retailer', 'tax_class_id' => 3]
  64. );
  65. $customerSetup->installEntities();
  66. $customerSetup->installCustomerForms();
  67. $disableAGCAttribute = $customerSetup->getEavConfig()->getAttribute('customer', 'disable_auto_group_change');
  68. $disableAGCAttribute->setData('used_in_forms', ['adminhtml_customer']);
  69. $disableAGCAttribute->save();
  70. $attributesInfo = [
  71. 'vat_id' => [
  72. 'label' => 'VAT number',
  73. 'type' => 'static',
  74. 'input' => 'text',
  75. 'position' => 140,
  76. 'visible' => true,
  77. 'required' => false,
  78. ],
  79. 'vat_is_valid' => [
  80. 'label' => 'VAT number validity',
  81. 'visible' => false,
  82. 'required' => false,
  83. 'type' => 'static',
  84. ],
  85. 'vat_request_id' => [
  86. 'label' => 'VAT number validation request ID',
  87. 'type' => 'static',
  88. 'visible' => false,
  89. 'required' => false,
  90. ],
  91. 'vat_request_date' => [
  92. 'label' => 'VAT number validation request date',
  93. 'type' => 'static',
  94. 'visible' => false,
  95. 'required' => false,
  96. ],
  97. 'vat_request_success' => [
  98. 'label' => 'VAT number validation request success',
  99. 'visible' => false,
  100. 'required' => false,
  101. 'type' => 'static',
  102. ],
  103. ];
  104. foreach ($attributesInfo as $attributeCode => $attributeParams) {
  105. $customerSetup->addAttribute('customer_address', $attributeCode, $attributeParams);
  106. }
  107. $vatIdAttribute = $customerSetup->getEavConfig()->getAttribute('customer_address', 'vat_id');
  108. $vatIdAttribute->setData(
  109. 'used_in_forms',
  110. ['adminhtml_customer_address', 'customer_address_edit', 'customer_register_address']
  111. );
  112. $vatIdAttribute->save();
  113. $entities = $customerSetup->getDefaultEntities();
  114. foreach ($entities as $entityName => $entity) {
  115. $customerSetup->addEntityType($entityName, $entity);
  116. }
  117. $customerSetup->updateAttribute(
  118. 'customer_address',
  119. 'street',
  120. 'backend_model',
  121. \Magento\Eav\Model\Entity\Attribute\Backend\DefaultBackend::class
  122. );
  123. $migrationSetup = $this->moduleDataSetup->createMigrationSetup();
  124. $migrationSetup->appendClassAliasReplace(
  125. 'customer_eav_attribute',
  126. 'data_model',
  127. Migration::ENTITY_TYPE_MODEL,
  128. Migration::FIELD_CONTENT_TYPE_PLAIN,
  129. ['attribute_id']
  130. );
  131. $migrationSetup->doUpdateClassAliases();
  132. }
  133. /**
  134. * {@inheritdoc}
  135. */
  136. public static function getDependencies()
  137. {
  138. return [];
  139. }
  140. /**
  141. * {@inheritdoc}
  142. */
  143. public static function getVersion()
  144. {
  145. return '2.0.0';
  146. }
  147. /**
  148. * {@inheritdoc}
  149. */
  150. public function getAliases()
  151. {
  152. return [];
  153. }
  154. }