AddGiftMessageAttributes.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\GiftMessage\Setup\Patch\Data;
  7. use Magento\Catalog\Setup\CategorySetupFactory;
  8. use Magento\Quote\Setup\QuoteSetupFactory;
  9. use Magento\Sales\Setup\SalesSetupFactory;
  10. use Magento\Framework\App\ResourceConnection;
  11. use Magento\Framework\Setup\Patch\DataPatchInterface;
  12. use Magento\Framework\Setup\Patch\PatchVersionInterface;
  13. class AddGiftMessageAttributes implements DataPatchInterface, PatchVersionInterface
  14. {
  15. /**
  16. * @var \Magento\Framework\Setup\ModuleDataSetupInterface
  17. */
  18. private $moduleDataSetup;
  19. /**
  20. * @var CategorySetupFactory
  21. */
  22. private $categorySetupFactory;
  23. /**
  24. * @var QuoteSetupFactory
  25. */
  26. private $quoteSetupFactory;
  27. /**
  28. * @var SalesSetupFactory
  29. */
  30. private $salesSetupFactory;
  31. /**
  32. * AddGiftMessageAttributes constructor.
  33. * @param \Magento\Framework\Setup\ModuleDataSetupInterface $moduleDataSetup
  34. * @param CategorySetupFactory $categorySetupFactory
  35. * @param QuoteSetupFactory $quoteSetupFactory
  36. * @param SalesSetupFactory $salesSetupFactory
  37. */
  38. public function __construct(
  39. \Magento\Framework\Setup\ModuleDataSetupInterface $moduleDataSetup,
  40. CategorySetupFactory $categorySetupFactory,
  41. QuoteSetupFactory $quoteSetupFactory,
  42. SalesSetupFactory $salesSetupFactory
  43. ) {
  44. $this->moduleDataSetup = $moduleDataSetup;
  45. $this->categorySetupFactory = $categorySetupFactory;
  46. $this->quoteSetupFactory = $quoteSetupFactory;
  47. $this->salesSetupFactory = $salesSetupFactory;
  48. }
  49. /**
  50. * {@inheritdoc}
  51. */
  52. public function apply()
  53. {
  54. /**
  55. * Add 'gift_message_id' attributes for entities
  56. */
  57. $options = ['type' => \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER, 'visible' => false, 'required' => false];
  58. $entities = ['quote', 'quote_address', 'quote_item', 'quote_address_item'];
  59. /** @var \Magento\Quote\Setup\QuoteSetup $quoteSetup */
  60. $quoteSetup = $this->quoteSetupFactory->create(['setup' => $this->moduleDataSetup]);
  61. foreach ($entities as $entity) {
  62. $quoteSetup->addAttribute($entity, 'gift_message_id', $options);
  63. }
  64. /** @var \Magento\Sales\Setup\SalesSetup $salesSetup */
  65. $salesSetup = $this->salesSetupFactory->create(['setup' => $this->moduleDataSetup]);
  66. $salesSetup->addAttribute('order', 'gift_message_id', $options);
  67. $salesSetup->addAttribute('order_item', 'gift_message_id', $options);
  68. /**
  69. * Add 'gift_message_available' attributes for entities
  70. */
  71. $salesSetup->addAttribute('order_item', 'gift_message_available', $options);
  72. /** @var \Magento\Catalog\Setup\CategorySetup $catalogSetup */
  73. $catalogSetup = $this->categorySetupFactory->create(['setup' => $this->moduleDataSetup]);
  74. $catalogSetup->addAttribute(
  75. \Magento\Catalog\Model\Product::ENTITY,
  76. 'gift_message_available',
  77. [
  78. 'group' => 'Gift Options',
  79. 'backend' => \Magento\Catalog\Model\Product\Attribute\Backend\Boolean::class,
  80. 'frontend' => '',
  81. 'label' => 'Allow Gift Message',
  82. 'input' => 'select',
  83. 'class' => '',
  84. 'source' => \Magento\Catalog\Model\Product\Attribute\Source\Boolean::class,
  85. 'global' => true,
  86. 'visible' => true,
  87. 'required' => false,
  88. 'user_defined' => false,
  89. 'default' => '',
  90. 'apply_to' => '',
  91. 'input_renderer' => \Magento\GiftMessage\Block\Adminhtml\Product\Helper\Form\Config::class,
  92. 'visible_on_front' => false,
  93. 'is_used_in_grid' => true,
  94. 'is_visible_in_grid' => false,
  95. 'is_filterable_in_grid' => false,
  96. ]
  97. );
  98. $groupName = 'Autosettings';
  99. $entityTypeId = $catalogSetup->getEntityTypeId(\Magento\Catalog\Model\Product::ENTITY);
  100. $attributeSetId = $catalogSetup->getAttributeSetId($entityTypeId, 'Default');
  101. $attribute = $catalogSetup->getAttribute($entityTypeId, 'gift_message_available');
  102. if ($attribute) {
  103. $catalogSetup->addAttributeToGroup(
  104. $entityTypeId,
  105. $attributeSetId,
  106. $groupName,
  107. $attribute['attribute_id'],
  108. 60
  109. );
  110. }
  111. }
  112. /**
  113. * {@inheritdoc}
  114. */
  115. public static function getDependencies()
  116. {
  117. return [];
  118. }
  119. /**
  120. * {@inheritdoc}
  121. */
  122. public static function getVersion()
  123. {
  124. return '2.0.0';
  125. }
  126. /**
  127. * {@inheritdoc}
  128. */
  129. public function getAliases()
  130. {
  131. return [];
  132. }
  133. }