UpdateAllowedMethods.php 5.13 KB
Newer Older
Ketan's avatar
Ketan committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

namespace Magento\Usps\Setup\Patch\Data;

use Magento\Framework\App\ResourceConnection;
use Magento\Framework\Setup\Patch\DataPatchInterface;
use Magento\Framework\Setup\Patch\PatchVersionInterface;

/**
 * Class UpdateAllowedMethods
 * @package Magento\Usps\Setup\Patch
 */
class UpdateAllowedMethods implements DataPatchInterface, PatchVersionInterface
{
    /**
     * @var \Magento\Framework\Setup\ModuleDataSetupInterface
     */
    private $moduleDataSetup;

    /**
     * UpdateAllowedMethods constructor.
     * @param \Magento\Framework\Setup\ModuleDataSetupInterface $moduleDataSetup
     */
    public function __construct(
        \Magento\Framework\Setup\ModuleDataSetupInterface $moduleDataSetup
    ) {
        $this->moduleDataSetup = $moduleDataSetup;
    }

    /**
     * {@inheritdoc}
     */
    public function apply()
    {
        $connection = $this->moduleDataSetup->getConnection();
        $configDataTable = $this->moduleDataSetup->getTable('core_config_data');
        $oldToNewMethodCodesMap = [
            'First-Class' => '0_FCLE',
            'First-Class Mail International Large Envelope' => 'INT_14',
            'First-Class Mail International Letter' => 'INT_13',
            'First-Class Mail International Letters' => 'INT_13',
            'First-Class Mail International Package' => 'INT_15',
            'First-Class Mail International Parcel' => 'INT_13',
            'First-Class Package International Service' => 'INT_15',
            'First-Class Mail' => '0_FCLE',
            'First-Class Mail Flat' => '0_FCLE',
            'First-Class Mail Large Envelope' => '0_FCLE',
            'First-Class Mail International' => 'INT_14',
            'First-Class Mail Letter' => '0_FCL',
            'First-Class Mail Parcel' => '0_FCP',
            'First-Class Mail Package' => '0_FCP',
            'First-Class Package Service - Retail' => '0_FCP',
            'Parcel Post' => '4',
            'Retail Ground' => '4',
            'Media Mail' => '6',
            'Library Mail' => '7',
            'Express Mail' => '3',
            'Express Mail PO to PO' => '3',
            'Express Mail Flat Rate Envelope' => '13',
            'Express Mail Flat-Rate Envelope Sunday/Holiday Guarantee' => '25',
            'Express Mail Sunday/Holiday Guarantee' => '23',
            'Express Mail Flat Rate Envelope Hold For Pickup' => '27',
            'Express Mail Hold For Pickup' => '2',
            'Global Express Guaranteed (GXG)' => 'INT_4',
            'Global Express Guaranteed Non-Document Rectangular' => 'INT_6',
            'Global Express Guaranteed Non-Document Non-Rectangular' => 'INT_7',
            'USPS GXG Envelopes' => 'INT_12',
            'Express Mail International' => 'INT_1',
            'Express Mail International Flat Rate Envelope' => 'INT_10',
            'Priority Mail' => '1',
            'Priority Mail Small Flat Rate Box' => '28',
            'Priority Mail Medium Flat Rate Box' => '17',
            'Priority Mail Large Flat Rate Box' => '22',
            'Priority Mail Flat Rate Envelope' => '16',
            'Priority Mail International' => 'INT_2',
            'Priority Mail International Flat Rate Envelope' => 'INT_8',
            'Priority Mail International Small Flat Rate Box' => 'INT_16',
            'Priority Mail International Medium Flat Rate Box' => 'INT_9',
            'Priority Mail International Large Flat Rate Box' => 'INT_11',
        ];

        $select = $connection->select()
            ->from($configDataTable)
            ->where(
                'path IN (?)',
                ['carriers/usps/free_method', 'carriers/usps/allowed_methods']
            );
        $oldConfigValues = $connection->fetchAll($select);

        foreach ($oldConfigValues as $oldValue) {
            if (stripos($oldValue['path'], 'free_method') !== false
                && isset($oldToNewMethodCodesMap[$oldValue['value']])
            ) {
                $newValue = $oldToNewMethodCodesMap[$oldValue['value']];
            } elseif (stripos($oldValue['path'], 'allowed_methods') !== false) {
                $newValuesList = [];
                foreach (explode(',', $oldValue['value']) as $shippingMethod) {
                    if (isset($oldToNewMethodCodesMap[$shippingMethod])) {
                        $newValuesList[] = $oldToNewMethodCodesMap[$shippingMethod];
                    }
                }
                $newValue = implode(',', $newValuesList);
            } else {
                continue;
            }

            if ($newValue && $newValue != $oldValue['value']) {
                $whereConfigId = $connection->quoteInto('config_id = ?', $oldValue['config_id']);
                $connection->update($configDataTable, ['value' => $newValue], $whereConfigId);
            }
        }
    }

    /**
     * {@inheritdoc}
     */
    public static function getDependencies()
    {
        return [];
    }

    /**
     * {@inheritdoc}
     */
    public static function getVersion()
    {
        return '2.0.1';
    }

    /**
     * {@inheritdoc}
     */
    public function getAliases()
    {
        return [];
    }
}