Files
module-vendor-notes/Setup/UpgradeData.php

74 lines
2.3 KiB
PHP
Raw Normal View History

<?php
namespace Shopkeeper\VendorNotes\Setup;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\UpgradeDataInterface;
use Magento\Framework\App\Config\Storage\WriterInterface;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\Serialize\Serializer\Json;
class UpgradeData implements UpgradeDataInterface
{
protected $configWriter;
protected $scopeConfig;
protected $json;
public function __construct(
WriterInterface $configWriter,
ScopeConfigInterface $scopeConfig,
Json $json
) {
$this->configWriter = $configWriter;
$this->scopeConfig = $scopeConfig;
$this->json = $json;
}
public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
$setup->startSetup();
// Migrate from old individual fields to new dynamic rows format
if (version_compare($context->getVersion(), '1.1.0', '<')) {
$this->migrateToVendorMapping();
}
$setup->endSetup();
}
/**
* Migrate old configuration to new vendor mapping format
*/
protected function migrateToVendorMapping()
{
// Define the old configuration paths and their corresponding vendor names
$oldConfigs = [
'vendor_notes/general/thermo_fisher_note' => 'Thermo Fisher',
'vendor_notes/general/orion_note' => 'Orion',
'vendor_notes/general/nalgene_note' => 'Nalgene',
'vendor_notes/general/thermo_fisher_parts_note' => 'Thermo Fisher Parts'
];
$vendorMapping = [];
foreach ($oldConfigs as $configPath => $vendorName) {
$note = $this->scopeConfig->getValue($configPath);
if ($note) {
$vendorMapping[] = [
'vendor_name' => $vendorName,
'note' => $note
];
}
}
// Save the new mapping if we have data
if (!empty($vendorMapping)) {
$serializedMapping = $this->json->serialize($vendorMapping);
$this->configWriter->save(
'vendor_notes/general/vendor_mapping',
$serializedMapping
);
}
}
}