ChangePriceAttributeDefaultScope.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Catalog\Setup\Patch\Data;
  7. use Magento\Catalog\Setup\CategorySetup;
  8. use Magento\Catalog\Setup\CategorySetupFactory;
  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 ChangePriceAttributeDefaultScope
  15. * @package Magento\Catalog\Setup\Patch
  16. */
  17. class ChangePriceAttributeDefaultScope implements DataPatchInterface, PatchVersionInterface
  18. {
  19. /**
  20. * @var ModuleDataSetupInterface
  21. */
  22. private $moduleDataSetup;
  23. /**
  24. * @var CategorySetupFactory
  25. */
  26. private $categorySetupFactory;
  27. /**
  28. * ChangePriceAttributeDefaultScope constructor.
  29. * @param ModuleDataSetupInterface $moduleDataSetup
  30. * @param CategorySetupFactory $categorySetupFactory
  31. */
  32. public function __construct(
  33. ModuleDataSetupInterface $moduleDataSetup,
  34. CategorySetupFactory $categorySetupFactory
  35. ) {
  36. $this->moduleDataSetup = $moduleDataSetup;
  37. $this->categorySetupFactory = $categorySetupFactory;
  38. }
  39. /**
  40. * {@inheritdoc}
  41. */
  42. public function apply()
  43. {
  44. /** @var CategorySetup $categorySetup */
  45. $categorySetup = $this->categorySetupFactory->create(['setup' => $this->moduleDataSetup]);
  46. $this->changePriceAttributeDefaultScope($categorySetup);
  47. }
  48. /**
  49. * @param CategorySetup $categorySetup
  50. * @return void
  51. */
  52. private function changePriceAttributeDefaultScope($categorySetup)
  53. {
  54. $entityTypeId = $categorySetup->getEntityTypeId(\Magento\Catalog\Model\Product::ENTITY);
  55. foreach (['price', 'cost', 'special_price'] as $attributeCode) {
  56. $attribute = $categorySetup->getAttribute($entityTypeId, $attributeCode);
  57. if (isset($attribute['attribute_id'])) {
  58. $categorySetup->updateAttribute(
  59. $entityTypeId,
  60. $attribute['attribute_id'],
  61. 'is_global',
  62. \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL
  63. );
  64. }
  65. }
  66. }
  67. /**
  68. * {@inheritdoc}
  69. */
  70. public static function getDependencies()
  71. {
  72. return [
  73. UpdateProductMetaDescription::class,
  74. ];
  75. }
  76. /**
  77. * {@inheritdoc}
  78. */
  79. public static function getVersion()
  80. {
  81. return '2.1.3';
  82. }
  83. /**
  84. * {@inheritdoc}
  85. */
  86. public function getAliases()
  87. {
  88. return [];
  89. }
  90. }