From 6781f9850170187cd82d901eb7728f397491ee23 Mon Sep 17 00:00:00 2001 From: wsajosh Date: Mon, 30 Jan 2023 10:45:31 +0000 Subject: [PATCH] 20.51.8 SHQ23-46 Correct the datatype of freight_class attribute from int to text --- CHANGELOG-PUBLIC.md | 4 + CHANGELOG.MD | 4 + composer.json | 2 +- .../Patch/Data/InstallFreightAttributes.php | 5 +- .../Data/UpdateFreightClassAttribute.php | 110 ++++++++++++++++++ src/composer.json | 2 +- src/etc/config.xml | 2 +- 7 files changed, 124 insertions(+), 5 deletions(-) create mode 100644 src/Setup/Patch/Data/UpdateFreightClassAttribute.php diff --git a/CHANGELOG-PUBLIC.md b/CHANGELOG-PUBLIC.md index 18d101e..03f9b95 100644 --- a/CHANGELOG-PUBLIC.md +++ b/CHANGELOG-PUBLIC.md @@ -744,3 +744,7 @@ MNB-2930 Fix order notes with generic carrier MNB-3173 Fix to ensure AdobeMSI stock handler is only used when MSI enabled and configured. MNB-3339 Update readme. +## 20.51.8 (2023-01-30) +SHQ23-46 Correct the datatype of freight_class attribute from int to text + + diff --git a/CHANGELOG.MD b/CHANGELOG.MD index 18d101e..03f9b95 100755 --- a/CHANGELOG.MD +++ b/CHANGELOG.MD @@ -744,3 +744,7 @@ MNB-2930 Fix order notes with generic carrier MNB-3173 Fix to ensure AdobeMSI stock handler is only used when MSI enabled and configured. MNB-3339 Update readme. +## 20.51.8 (2023-01-30) +SHQ23-46 Correct the datatype of freight_class attribute from int to text + + diff --git a/composer.json b/composer.json index 16d94a4..f8378bd 100755 --- a/composer.json +++ b/composer.json @@ -2,7 +2,7 @@ "name": "shipperhq/module-shipper", "description": "Magento Shipping integration with ShipperHQ", "type": "magento2-module", - "version": "20.51.7", + "version": "20.51.8", "license": [ "OSL-3.0", "AFL-3.0" diff --git a/src/Setup/Patch/Data/InstallFreightAttributes.php b/src/Setup/Patch/Data/InstallFreightAttributes.php index e565e73..f484cc0 100644 --- a/src/Setup/Patch/Data/InstallFreightAttributes.php +++ b/src/Setup/Patch/Data/InstallFreightAttributes.php @@ -13,11 +13,11 @@ namespace ShipperHQ\Shipper\Setup\Patch\Data; use Magento\Catalog\Model\Product; +use Magento\Catalog\Setup\CategorySetupFactory; use Magento\Eav\Model\ResourceModel\Entity\Attribute\CollectionFactory as AttributeCollectionFactory; use Magento\Framework\App\ObjectManager; use Magento\Framework\Setup\ModuleDataSetupInterface; use Magento\Framework\Setup\Patch\DataPatchInterface; -use Magento\Catalog\Setup\CategorySetupFactory; use Magento\Framework\Setup\Patch\PatchRevertableInterface; class InstallFreightAttributes implements DataPatchInterface, PatchRevertableInterface @@ -72,10 +72,11 @@ public static function getVersion() */ public function apply() { + /** @var \Magento\Catalog\Setup\CategorySetup $catalogSetup */ $catalogSetup = $this->categorySetupFactory->create(['setup' => $this->moduleDataSetup]); /* ------ freight_class -------- */ $catalogSetup->addAttribute(Product::ENTITY, 'freight_class', [ - 'type' => 'int', + 'type' => 'text', 'source' => 'ShipperHQ\Shipper\Model\Product\Attribute\Source\FreightClass', 'input' => 'select', 'label' => 'Freight Class', diff --git a/src/Setup/Patch/Data/UpdateFreightClassAttribute.php b/src/Setup/Patch/Data/UpdateFreightClassAttribute.php new file mode 100644 index 0000000..3aa6b6a --- /dev/null +++ b/src/Setup/Patch/Data/UpdateFreightClassAttribute.php @@ -0,0 +1,110 @@ +moduleDataSetup = $moduleDataSetup; + $this->eavSetupFactory = $eavSetupFactory; + } + + /** + * @inheritdoc + */ + public static function getDependencies() + { + return [InstallFreightAttributes::class]; + } + + /** + * SHQ23-46 Change freight_class attribute from type int to type text + * Based on \Magento\Catalog\Setup\Patch\Data\UpdateMultiselectAttributesBackendTypes + * + * @return void + * @throws LocalizedException + */ + public function apply() + { + /** @var EavSetup $eavSetup */ + $eavSetup = $this->eavSetupFactory->create(['setup' => $this->moduleDataSetup]); + $entityTypeId = $eavSetup->getEntityTypeId(Product::ENTITY); + + /** @var AbstractAttribute $freightClassAttribute */ + $freightClassAttribute = $eavSetup->getAttribute($entityTypeId, "freight_class"); + + if (is_array($freightClassAttribute) && $freightClassAttribute['backend_type'] == 'int') { + $this->moduleDataSetup->startSetup(); + $connection = $this->moduleDataSetup->getConnection(); + $intTable = $this->moduleDataSetup->getTable('catalog_product_entity_int'); + $textTable = $this->moduleDataSetup->getTable('catalog_product_entity_text'); + $intTableDataSql = $connection + ->select() + ->from($intTable) + ->where('attribute_id = ?', $freightClassAttribute['attribute_id']); + $dataToMigrate = array_map(static function ($row) { + $row['value_id'] = null; + + return $row; + }, $connection->fetchAll($intTableDataSql)); + + // Clean up data. In some instances the .5 is missing + foreach ($dataToMigrate as $key=>$data) { + if ($data['value'] == "92") { + $dataToMigrate[$key]['value'] = "92.5"; + } elseif ($data['value'] == "77") { + $dataToMigrate[$key]['value'] = "77.5"; + } + } + + foreach (array_chunk($dataToMigrate, 2000) as $dataChunk) { + $connection->insertMultiple($textTable, $dataChunk); + } + $connection->query($connection->deleteFromSelect($intTableDataSql, $intTable)); + $eavSetup->updateAttribute($entityTypeId, $freightClassAttribute['attribute_id'], 'backend_type', 'text'); + $this->moduleDataSetup->endSetup(); + } + } + + /** + * @inheritdoc + */ + public function getAliases() + { + return []; + } +} diff --git a/src/composer.json b/src/composer.json index 86206b0..24f31e7 100644 --- a/src/composer.json +++ b/src/composer.json @@ -2,7 +2,7 @@ "name": "shipperhq/module-shipper", "description": "Magento Shipping integration with ShipperHQ", "type": "magento2-module", - "version": "20.51.7", + "version": "20.51.8", "license": [ "OSL-3.0", "AFL-3.0" diff --git a/src/etc/config.xml b/src/etc/config.xml index 3977956..dd37964 100755 --- a/src/etc/config.xml +++ b/src/etc/config.xml @@ -67,7 +67,7 @@ 30 0 1 - 20.51.7 + 20.51.8 300