UpdateAdminTextSwatchValues.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Swatches\Setup\Patch\Data;
  7. use Magento\Store\Model\Store;
  8. use Magento\Swatches\Model\Swatch;
  9. use Zend_Db;
  10. use Zend_Db_Expr;
  11. use Magento\Framework\App\ResourceConnection;
  12. use Magento\Framework\Setup\Patch\DataPatchInterface;
  13. use Magento\Framework\Setup\Patch\PatchVersionInterface;
  14. /**
  15. * Class UpdateAdminTextSwatchValues
  16. * @package Magento\Swatches\Setup\Patch
  17. */
  18. class UpdateAdminTextSwatchValues implements DataPatchInterface, PatchVersionInterface
  19. {
  20. /**
  21. * @var \Magento\Framework\Setup\ModuleDataSetupInterface
  22. */
  23. private $moduleDataSetup;
  24. /**
  25. * UpdateAdminTextSwatchValues constructor.
  26. * @param \Magento\Framework\Setup\ModuleDataSetupInterface $moduleDataSetup
  27. */
  28. public function __construct(
  29. \Magento\Framework\Setup\ModuleDataSetupInterface $moduleDataSetup
  30. ) {
  31. $this->moduleDataSetup = $moduleDataSetup;
  32. }
  33. /**
  34. * {@inheritdoc}
  35. */
  36. public function apply()
  37. {
  38. $this->moduleDataSetup->getConnection()->startSetup();
  39. $this->updateAdminTextSwatchValues();
  40. $this->moduleDataSetup->getConnection()->endSetup();
  41. }
  42. /**
  43. * {@inheritdoc}
  44. */
  45. public static function getDependencies()
  46. {
  47. return [
  48. AddSwatchImageToDefaultAttribtueSet::class
  49. ];
  50. }
  51. /**
  52. * {@inheritdoc}
  53. */
  54. public static function getVersion()
  55. {
  56. return '2.0.2';
  57. }
  58. /**
  59. * {@inheritdoc}
  60. */
  61. public function getAliases()
  62. {
  63. return [];
  64. }
  65. /**
  66. * Update text swatch values for admin panel.
  67. */
  68. private function updateAdminTextSwatchValues()
  69. {
  70. $connection = $this->moduleDataSetup->getConnection();
  71. $storeData = $connection
  72. ->select()
  73. ->from($this->moduleDataSetup->getTable('store'))
  74. ->where(Store::STORE_ID . "<> ? ", Store::DEFAULT_STORE_ID)
  75. ->order("sort_order desc")
  76. ->limit(1)
  77. ->query(Zend_Db::FETCH_ASSOC)
  78. ->fetch();
  79. if (is_array($storeData)) {
  80. /**
  81. * update eav_attribute_option_swatch as s
  82. * left join eav_attribute_option_swatch as ls on ls.option_id = s.option_id and ls.store_id = 1
  83. * set
  84. *
  85. * s.value = ls.value
  86. * where s.store_id = 0 and s.`type` = 0 and s.value = ""
  87. */
  88. /** @var \Magento\Framework\DB\Select $select */
  89. $select = $connection
  90. ->select()
  91. ->joinLeft(
  92. ["ls" => $this->moduleDataSetup->getTable('eav_attribute_option_swatch')],
  93. new Zend_Db_Expr("ls.option_id = s.option_id AND ls.store_id = " . $storeData[Store::STORE_ID]),
  94. ["value"]
  95. )
  96. ->where("s.store_id = ? ", Store::DEFAULT_STORE_ID)
  97. ->where("s.type = ? ", Swatch::SWATCH_TYPE_TEXTUAL)
  98. ->where("s.value = ? or s.value is null", "");
  99. $connection->query(
  100. $connection->updateFromSelect(
  101. $select,
  102. ["s" => $this->moduleDataSetup->getTable('eav_attribute_option_swatch')]
  103. )
  104. );
  105. }
  106. }
  107. }