InstallDownloadableAttributes.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Downloadable\Setup\Patch\Data;
  7. use Magento\Eav\Setup\EavSetup;
  8. use Magento\Eav\Setup\EavSetupFactory;
  9. use Magento\Framework\App\ResourceConnection;
  10. use Magento\Framework\Setup\ModuleDataSetupInterface;
  11. use Magento\Framework\Setup\Patch\DataPatchInterface;
  12. use Magento\Framework\Setup\Patch\PatchVersionInterface;
  13. /**
  14. * Class InstallDownloadableAttributes
  15. * @package Magento\Downloadable\Setup\Patch
  16. */
  17. class InstallDownloadableAttributes implements DataPatchInterface, PatchVersionInterface
  18. {
  19. /**
  20. * @var \Magento\Framework\Setup\ModuleDataSetupInterface
  21. */
  22. private $moduleDataSetup;
  23. /**
  24. * @var EavSetupFactory
  25. */
  26. private $eavSetupFactory;
  27. /**
  28. * InstallDownloadableAttributes constructor.
  29. * @param ModuleDataSetupInterface $moduleDataSetup
  30. * @param EavSetupFactory $eavSetupFactory
  31. */
  32. public function __construct(
  33. ModuleDataSetupInterface $moduleDataSetup,
  34. EavSetupFactory $eavSetupFactory
  35. ) {
  36. $this->moduleDataSetup = $moduleDataSetup;
  37. $this->eavSetupFactory = $eavSetupFactory;
  38. }
  39. /**
  40. * {@inheritdoc}
  41. * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  42. */
  43. public function apply()
  44. {
  45. /** @var EavSetup $eavSetup */
  46. $eavSetup = $this->eavSetupFactory->create(['setup' => $this->moduleDataSetup]);
  47. /**
  48. * Add attributes to the eav/attribute table
  49. */
  50. $eavSetup->addAttribute(
  51. \Magento\Catalog\Model\Product::ENTITY,
  52. 'links_purchased_separately',
  53. [
  54. 'type' => 'int',
  55. 'backend' => '',
  56. 'frontend' => '',
  57. 'label' => 'Links can be purchased separately',
  58. 'input' => '',
  59. 'class' => '',
  60. 'source' => '',
  61. 'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
  62. 'visible' => false,
  63. 'required' => true,
  64. 'user_defined' => false,
  65. 'default' => '',
  66. 'searchable' => false,
  67. 'filterable' => false,
  68. 'comparable' => false,
  69. 'visible_on_front' => false,
  70. 'unique' => false,
  71. 'apply_to' => 'downloadable',
  72. 'used_in_product_listing' => true
  73. ]
  74. );
  75. $eavSetup->addAttribute(
  76. \Magento\Catalog\Model\Product::ENTITY,
  77. 'samples_title',
  78. [
  79. 'type' => 'varchar',
  80. 'backend' => '',
  81. 'frontend' => '',
  82. 'label' => 'Samples title',
  83. 'input' => '',
  84. 'class' => '',
  85. 'source' => '',
  86. 'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE,
  87. 'visible' => false,
  88. 'required' => true,
  89. 'user_defined' => false,
  90. 'default' => '',
  91. 'searchable' => false,
  92. 'filterable' => false,
  93. 'comparable' => false,
  94. 'visible_on_front' => false,
  95. 'unique' => false,
  96. 'apply_to' => 'downloadable'
  97. ]
  98. );
  99. $eavSetup->addAttribute(
  100. \Magento\Catalog\Model\Product::ENTITY,
  101. 'links_title',
  102. [
  103. 'type' => 'varchar',
  104. 'backend' => '',
  105. 'frontend' => '',
  106. 'label' => 'Links title',
  107. 'input' => '',
  108. 'class' => '',
  109. 'source' => '',
  110. 'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE,
  111. 'visible' => false,
  112. 'required' => true,
  113. 'user_defined' => false,
  114. 'default' => '',
  115. 'searchable' => false,
  116. 'filterable' => false,
  117. 'comparable' => false,
  118. 'visible_on_front' => false,
  119. 'unique' => false,
  120. 'apply_to' => 'downloadable'
  121. ]
  122. );
  123. $eavSetup->addAttribute(
  124. \Magento\Catalog\Model\Product::ENTITY,
  125. 'links_exist',
  126. [
  127. 'type' => 'int',
  128. 'backend' => '',
  129. 'frontend' => '',
  130. 'label' => '',
  131. 'input' => '',
  132. 'class' => '',
  133. 'source' => '',
  134. 'global' => true,
  135. 'visible' => false,
  136. 'required' => false,
  137. 'user_defined' => false,
  138. 'default' => '0',
  139. 'searchable' => false,
  140. 'filterable' => false,
  141. 'comparable' => false,
  142. 'visible_on_front' => false,
  143. 'unique' => false,
  144. 'apply_to' => 'downloadable',
  145. 'used_in_product_listing' => 1
  146. ]
  147. );
  148. $fieldList = [
  149. 'price',
  150. 'special_price',
  151. 'special_from_date',
  152. 'special_to_date',
  153. 'minimal_price',
  154. 'cost',
  155. 'tier_price',
  156. 'weight',
  157. ];
  158. // make these attributes applicable to downloadable products
  159. foreach ($fieldList as $field) {
  160. $applyTo = explode(
  161. ',',
  162. $eavSetup->getAttribute(\Magento\Catalog\Model\Product::ENTITY, $field, 'apply_to')
  163. );
  164. if (!in_array('downloadable', $applyTo)) {
  165. $applyTo[] = 'downloadable';
  166. $eavSetup->updateAttribute(
  167. \Magento\Catalog\Model\Product::ENTITY,
  168. $field,
  169. 'apply_to',
  170. implode(',', $applyTo)
  171. );
  172. }
  173. }
  174. }
  175. /**
  176. * {@inheritdoc}
  177. */
  178. public static function getDependencies()
  179. {
  180. return [];
  181. }
  182. /**
  183. * {@inheritdoc}
  184. */
  185. public static function getVersion()
  186. {
  187. return '2.0.0';
  188. }
  189. /**
  190. * {@inheritdoc}
  191. */
  192. public function getAliases()
  193. {
  194. return [];
  195. }
  196. }