ApplyAttributesUpdate.php 7.4 KB

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