ChangePriceAttributeDefaultScope.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Msrp\Setup\Patch\Data;
  7. use Magento\Catalog\Setup\CategorySetupFactory;
  8. use Magento\Framework\Setup\ModuleContextInterface;
  9. use Magento\Framework\Setup\ModuleDataSetupInterface;
  10. use Magento\Framework\Setup\UpgradeDataInterface;
  11. use Magento\Framework\App\ResourceConnection;
  12. use Magento\Framework\Setup\Patch\DataPatchInterface;
  13. use Magento\Framework\Setup\Patch\PatchVersionInterface;
  14. class ChangePriceAttributeDefaultScope implements DataPatchInterface, PatchVersionInterface
  15. {
  16. /**
  17. * @var \Magento\Framework\Setup\ModuleDataSetupInterface
  18. */
  19. private $moduleDataSetup;
  20. /**
  21. * @var CategorySetupFactory
  22. */
  23. private $categorySetupFactory;
  24. /**
  25. * PatchInitial constructor.
  26. * @param \Magento\Framework\Setup\ModuleDataSetupInterface $moduleDataSetup
  27. */
  28. public function __construct(
  29. \Magento\Framework\Setup\ModuleDataSetupInterface $moduleDataSetup,
  30. CategorySetupFactory $categorySetupFactory
  31. ) {
  32. $this->moduleDataSetup = $moduleDataSetup;
  33. $this->categorySetupFactory = $categorySetupFactory;
  34. }
  35. /**
  36. * {@inheritdoc}
  37. */
  38. public function apply()
  39. {
  40. /** @var \Magento\Catalog\Setup\CategorySetup $categorySetup */
  41. $categorySetup = $this->categorySetupFactory->create();
  42. $this->moduleDataSetup->getConnection()->startSetup();
  43. $entityTypeId = $categorySetup->getEntityTypeId(\Magento\Catalog\Model\Product::ENTITY);
  44. $this->changePriceAttributeDefaultScope($categorySetup, $entityTypeId);
  45. $this->moduleDataSetup->getConnection()->endSetup();
  46. }
  47. /**
  48. * {@inheritdoc}
  49. */
  50. public static function getDependencies()
  51. {
  52. return [
  53. InitializeMsrpAttributes::class
  54. ];
  55. }
  56. /**
  57. * {@inheritdoc}
  58. */
  59. public static function getVersion()
  60. {
  61. return '2.1.3';
  62. }
  63. /**
  64. * {@inheritdoc}
  65. */
  66. public function getAliases()
  67. {
  68. return [];
  69. }
  70. /**
  71. * Change default scope for price attribute.
  72. *
  73. * @param \Magento\Catalog\Setup\CategorySetup $categorySetup
  74. * @param int $entityTypeId
  75. */
  76. private function changePriceAttributeDefaultScope($categorySetup, $entityTypeId)
  77. {
  78. $attribute = $categorySetup->getAttribute($entityTypeId, 'msrp');
  79. $categorySetup->updateAttribute(
  80. $entityTypeId,
  81. $attribute['attribute_id'],
  82. 'is_global',
  83. \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL
  84. );
  85. }
  86. }