Skip to content

Commit

Permalink
20.51.8 SHQ23-46 Correct the datatype of freight_class attribute from…
Browse files Browse the repository at this point in the history
… int to text
  • Loading branch information
wsajosh committed Jan 30, 2023
1 parent a222813 commit 6781f98
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG-PUBLIC.md
Expand Up @@ -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


4 changes: 4 additions & 0 deletions CHANGELOG.MD
Expand Up @@ -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


2 changes: 1 addition & 1 deletion composer.json
Expand Up @@ -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"
Expand Down
5 changes: 3 additions & 2 deletions src/Setup/Patch/Data/InstallFreightAttributes.php
Expand Up @@ -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
Expand Down Expand Up @@ -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',
Expand Down
110 changes: 110 additions & 0 deletions src/Setup/Patch/Data/UpdateFreightClassAttribute.php
@@ -0,0 +1,110 @@
<?php
/*
* ShipperHQ
*
* @category ShipperHQ
* @package ShipperHQ_Shipper
* @copyright Copyright (c) 2023 Zowta LTD and Zowta LLC (http://www.ShipperHQ.com)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* @author ShipperHQ Team sales@shipperhq.com
*/
declare(strict_types=1);

namespace ShipperHQ\Shipper\Setup\Patch\Data;

use Magento\Catalog\Model\Product;
use Magento\Eav\Model\Entity\Attribute\AbstractAttribute;
use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\Patch\DataPatchInterface;

class UpdateFreightClassAttribute implements DataPatchInterface
{
/**
* @var ModuleDataSetupInterface $moduleDataSetup
*/
private $moduleDataSetup;
/**
* @var EavSetupFactory
*/
private $eavSetupFactory;

/**
* @param ModuleDataSetupInterface $moduleDataSetup
* @param EavSetupFactory $eavSetupFactory
*/
public function __construct(
ModuleDataSetupInterface $moduleDataSetup,
EavSetupFactory $eavSetupFactory
) {
$this->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 [];
}
}
2 changes: 1 addition & 1 deletion src/composer.json
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion src/etc/config.xml
Expand Up @@ -67,7 +67,7 @@
<ws_timeout>30</ws_timeout>
<use_cache>0</use_cache>
<always_use_cache>1</always_use_cache>
<extension_version>20.51.7</extension_version>
<extension_version>20.51.8</extension_version>
<allowed_methods></allowed_methods>
<magento_version></magento_version>
<cache_timeout>300</cache_timeout>
Expand Down

0 comments on commit 6781f98

Please sign in to comment.