Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use JSON for ArraySerialized in Magento 2.2 #11

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 16 additions & 2 deletions Helper/Data.php
Expand Up @@ -14,9 +14,13 @@
* @copyright Copyright (c) 2017 TechDivision GmbH (http://www.techdivision.com)
* @link http://www.techdivision.com/
* @author Florian Sydekum <f.sydekum@techdivision.com>
* @author Jürgen "Atlan" Schuch <juergen@atmage.de>
*/
namespace Magenerds\BasePrice\Helper;

use Magento\Framework\App\ObjectManager;
use Magento\Framework\Serialize\Serializer\Json;

/**
* Class Data
* @package Magenerds\BasePrice\Helper
Expand Down Expand Up @@ -58,10 +62,20 @@ public function getConversion($product)
$productUnit = $product->getData('baseprice_product_unit');
$referenceUnit = $product->getData('baseprice_reference_unit');

$configArray = unserialize($this->scopeConfig->getValue(
$scopeConfig = $this->scopeConfig->getValue(
self::CONVERSION_CONFIG_PATH,
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
));
);

$objectManager = ObjectManager::getInstance();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please inject necessary classes via di.xml and constructor instead of the object manager :-)

$productMetadata = $objectManager->get('Magento\Framework\App\ProductMetadataInterface');
$version = $productMetadata->getVersion();
$objectManager->get('Psr\Log\LoggerInterface')->info('Version('.var_export($version,true).')');
if($version < 2.2) {
$configArray = unserialize($scopeConfig);
} else {
$configArray = $objectManager->get(Json::class)->unserialize($scopeConfig);
}

foreach ($configArray as $config) {
if ($config['product_unit'] == $productUnit
Expand Down
15 changes: 14 additions & 1 deletion Setup/InstallData.php
Expand Up @@ -14,13 +14,16 @@
* @copyright Copyright (c) 2017 TechDivision GmbH (http://www.techdivision.com)
* @link http://www.techdivision.com/
* @author Florian Sydekum <f.sydekum@techdivision.com>
* @author Jürgen "Atlan" Schuch <juergen@atmage.de>
*/
namespace Magenerds\BasePrice\Setup;

use Magento\Eav\Setup\EavSetup;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\Serialize\Serializer\Json;

/**
* Class InstallData
Expand Down Expand Up @@ -265,10 +268,20 @@ protected function _setSystemConfiguration()
}
}

$objectManager = ObjectManager::getInstance();
$productMetadata = $objectManager->get('Magento\Framework\App\ProductMetadataInterface');
$version = $productMetadata->getVersion();

if($version < 2.2) {
$serializedData = serialize($data);
} else {
$serializedData = $objectManager->get(Json::class)->serialize($data);
}

// save system configuration
$this->_configResource->saveConfig(
\Magenerds\BasePrice\Helper\Data::CONVERSION_CONFIG_PATH,
serialize($data),
$serializedData,
\Magento\Framework\App\Config\ScopeConfigInterface::SCOPE_TYPE_DEFAULT,
0
);
Expand Down