UpdateAllowedMethods.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Usps\Setup\Patch\Data;
  7. use Magento\Framework\App\ResourceConnection;
  8. use Magento\Framework\Setup\Patch\DataPatchInterface;
  9. use Magento\Framework\Setup\Patch\PatchVersionInterface;
  10. /**
  11. * Class UpdateAllowedMethods
  12. * @package Magento\Usps\Setup\Patch
  13. */
  14. class UpdateAllowedMethods implements DataPatchInterface, PatchVersionInterface
  15. {
  16. /**
  17. * @var \Magento\Framework\Setup\ModuleDataSetupInterface
  18. */
  19. private $moduleDataSetup;
  20. /**
  21. * UpdateAllowedMethods constructor.
  22. * @param \Magento\Framework\Setup\ModuleDataSetupInterface $moduleDataSetup
  23. */
  24. public function __construct(
  25. \Magento\Framework\Setup\ModuleDataSetupInterface $moduleDataSetup
  26. ) {
  27. $this->moduleDataSetup = $moduleDataSetup;
  28. }
  29. /**
  30. * {@inheritdoc}
  31. */
  32. public function apply()
  33. {
  34. $connection = $this->moduleDataSetup->getConnection();
  35. $configDataTable = $this->moduleDataSetup->getTable('core_config_data');
  36. $oldToNewMethodCodesMap = [
  37. 'First-Class' => '0_FCLE',
  38. 'First-Class Mail International Large Envelope' => 'INT_14',
  39. 'First-Class Mail International Letter' => 'INT_13',
  40. 'First-Class Mail International Letters' => 'INT_13',
  41. 'First-Class Mail International Package' => 'INT_15',
  42. 'First-Class Mail International Parcel' => 'INT_13',
  43. 'First-Class Package International Service' => 'INT_15',
  44. 'First-Class Mail' => '0_FCLE',
  45. 'First-Class Mail Flat' => '0_FCLE',
  46. 'First-Class Mail Large Envelope' => '0_FCLE',
  47. 'First-Class Mail International' => 'INT_14',
  48. 'First-Class Mail Letter' => '0_FCL',
  49. 'First-Class Mail Parcel' => '0_FCP',
  50. 'First-Class Mail Package' => '0_FCP',
  51. 'First-Class Package Service - Retail' => '0_FCP',
  52. 'Parcel Post' => '4',
  53. 'Retail Ground' => '4',
  54. 'Media Mail' => '6',
  55. 'Library Mail' => '7',
  56. 'Express Mail' => '3',
  57. 'Express Mail PO to PO' => '3',
  58. 'Express Mail Flat Rate Envelope' => '13',
  59. 'Express Mail Flat-Rate Envelope Sunday/Holiday Guarantee' => '25',
  60. 'Express Mail Sunday/Holiday Guarantee' => '23',
  61. 'Express Mail Flat Rate Envelope Hold For Pickup' => '27',
  62. 'Express Mail Hold For Pickup' => '2',
  63. 'Global Express Guaranteed (GXG)' => 'INT_4',
  64. 'Global Express Guaranteed Non-Document Rectangular' => 'INT_6',
  65. 'Global Express Guaranteed Non-Document Non-Rectangular' => 'INT_7',
  66. 'USPS GXG Envelopes' => 'INT_12',
  67. 'Express Mail International' => 'INT_1',
  68. 'Express Mail International Flat Rate Envelope' => 'INT_10',
  69. 'Priority Mail' => '1',
  70. 'Priority Mail Small Flat Rate Box' => '28',
  71. 'Priority Mail Medium Flat Rate Box' => '17',
  72. 'Priority Mail Large Flat Rate Box' => '22',
  73. 'Priority Mail Flat Rate Envelope' => '16',
  74. 'Priority Mail International' => 'INT_2',
  75. 'Priority Mail International Flat Rate Envelope' => 'INT_8',
  76. 'Priority Mail International Small Flat Rate Box' => 'INT_16',
  77. 'Priority Mail International Medium Flat Rate Box' => 'INT_9',
  78. 'Priority Mail International Large Flat Rate Box' => 'INT_11',
  79. ];
  80. $select = $connection->select()
  81. ->from($configDataTable)
  82. ->where(
  83. 'path IN (?)',
  84. ['carriers/usps/free_method', 'carriers/usps/allowed_methods']
  85. );
  86. $oldConfigValues = $connection->fetchAll($select);
  87. foreach ($oldConfigValues as $oldValue) {
  88. if (stripos($oldValue['path'], 'free_method') !== false
  89. && isset($oldToNewMethodCodesMap[$oldValue['value']])
  90. ) {
  91. $newValue = $oldToNewMethodCodesMap[$oldValue['value']];
  92. } elseif (stripos($oldValue['path'], 'allowed_methods') !== false) {
  93. $newValuesList = [];
  94. foreach (explode(',', $oldValue['value']) as $shippingMethod) {
  95. if (isset($oldToNewMethodCodesMap[$shippingMethod])) {
  96. $newValuesList[] = $oldToNewMethodCodesMap[$shippingMethod];
  97. }
  98. }
  99. $newValue = implode(',', $newValuesList);
  100. } else {
  101. continue;
  102. }
  103. if ($newValue && $newValue != $oldValue['value']) {
  104. $whereConfigId = $connection->quoteInto('config_id = ?', $oldValue['config_id']);
  105. $connection->update($configDataTable, ['value' => $newValue], $whereConfigId);
  106. }
  107. }
  108. }
  109. /**
  110. * {@inheritdoc}
  111. */
  112. public static function getDependencies()
  113. {
  114. return [];
  115. }
  116. /**
  117. * {@inheritdoc}
  118. */
  119. public static function getVersion()
  120. {
  121. return '2.0.1';
  122. }
  123. /**
  124. * {@inheritdoc}
  125. */
  126. public function getAliases()
  127. {
  128. return [];
  129. }
  130. }