Skip to content

Commit

Permalink
Add more default units - Magenerds#22 Magenerds#38
Browse files Browse the repository at this point in the history
  • Loading branch information
DanieliMi committed Jan 25, 2019
1 parent e042e15 commit baf700d
Show file tree
Hide file tree
Showing 2 changed files with 192 additions and 1 deletion.
191 changes: 191 additions & 0 deletions Setup/UpgradeData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
<?php
/**
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is available through the world-wide-web at this URL:
* http://opensource.org/licenses/osl-3.0.php
*/

/**
* @category Magenerds
* @package Magenerds_BasePrice
* @subpackage Setup
* @copyright Copyright (c) 2017 TechDivision GmbH (http://www.techdivision.com)
* @link http://www.techdivision.com/
*/
namespace Magenerds\BasePrice\Setup;


use Magenerds\BasePrice\Helper\Data;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Catalog\Api\ProductAttributeOptionManagementInterface;
use Magento\Catalog\Model\Product;
use Magento\Config\Model\ResourceModel\Config as ResourceConfig;
use Magento\Framework\Serialize\SerializerInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\UpgradeDataInterface;
use Magento\Eav\Setup\EavSetupFactory;

class UpgradeData implements UpgradeDataInterface
{
const UNIT_ATTRIBUTE_CODES = ['baseprice_product_unit', 'baseprice_reference_unit'];

/**
* @var EavSetupFactory
*/
public $eavSetupFactory;

/**
* @var ProductAttributeOptionManagementInterface
*/
private $productAttributeOptionManagementInterface;

/**
* @var ResourceConfig
*/
private $configResource;

/**
* @var SerializerInterface
*/
private $serializer;

/**
* @var ScopeConfigInterface
*/
private $scopeConfig;

public function __construct(
EavSetupFactory $eavSetupFactory,
ProductAttributeOptionManagementInterface $productAttributeOptionManagementInterface,
ResourceConfig $configResource,
SerializerInterface $serializer,
ScopeConfigInterface $scopeConfig
) {
$this->eavSetupFactory = $eavSetupFactory;
$this->productAttributeOptionManagementInterface = $productAttributeOptionManagementInterface;
$this->configResource = $configResource;
$this->serializer = $serializer;
$this->scopeConfig = $scopeConfig;
}

/**
* Upgrades data for a module
*
* @param ModuleDataSetupInterface $setup
* @param ModuleContextInterface $context
*
* @throws \Magento\Framework\Exception\InputException
* @throws \Magento\Framework\Exception\StateException
*/
public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
$setup->startSetup();
$eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
if (version_compare($context->getVersion(), '1.1.0', '<')) {
$options = ['km', 'qm', 'cbm', 'pc'];

// holds the conversion rates
$dataTemplate = [
'km' => [
[
'reference_unit' => 'm',
'conversion_rate' => '0.001',
],
[
'reference_unit' => 'cm',
'conversion_rate' => '0.00001',
],
[
'reference_unit' => 'mm',
'conversion_rate' => '0.000001',
],
],
'm' => [
[
'reference_unit' => 'km',
'conversion_rate' => '1000',
],
],
'cm' => [
[
'reference_unit' => 'km',
'conversion_rate' => '100000',
],
],
'mm' => [
[
'reference_unit' => 'km',
'conversion_rate' => '1000000',
],
],

];

$this->addOption($eavSetup, $options);
$this->addToSystemConfiguration($dataTemplate);
}
$setup->endSetup();
}

/**
* @param $eavSetup
* @param array $options
*/
protected function addOption($eavSetup, array $options)
{
foreach (self::UNIT_ATTRIBUTE_CODES as $attributeCode) {
$attributeId = $eavSetup->getAttribute(Product::ENTITY, $attributeCode, 'attribute_id');

$eavSetup->addAttributeOption([
'attribute_id' => $attributeId,
'values' => $options,
]);
}
}

/**
* Sets conversations for added default units
*
* @param array $dataTemplate
*
* @throws \Magento\Framework\Exception\InputException
* @throws \Magento\Framework\Exception\StateException
*/
protected function addToSystemConfiguration(array $dataTemplate)
{
// get all attribute options for product unit
$productUnitOptions = [];
foreach ($this->productAttributeOptionManagementInterface->getItems('baseprice_product_unit') as $option) {
$productUnitOptions[$option->getLabel()] = $option->getValue();
}

// get all attribute options for reference unit
$referenceUnitOptions = [];
foreach ($this->productAttributeOptionManagementInterface->getItems('baseprice_reference_unit') as $option) {
$referenceUnitOptions[$option->getLabel()] = $option->getValue();
}

// iterate over attribute options in order to replace labels with option ids
$data = $this->serializer->unserialize($this->scopeConfig->getValue(Data::CONVERSION_CONFIG_PATH));
foreach ($dataTemplate as $unit => $unitData) {
foreach ($unitData as $key => $unitDataEntry) {
$data[] = [
'product_unit' => $productUnitOptions[$unit],
'reference_unit' => $referenceUnitOptions[$unitDataEntry['reference_unit']],
'conversion_rate' => $unitDataEntry['conversion_rate'],
];
}
}

//save system configuration
$this->configResource->saveConfig(
Data::CONVERSION_CONFIG_PATH,
$this->serializer->serialize($data),
ScopeConfigInterface::SCOPE_TYPE_DEFAULT,
0
);
}
}
2 changes: 1 addition & 1 deletion etc/module.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Magenerds_BasePrice" setup_version="1.0.4">
<module name="Magenerds_BasePrice" setup_version="1.1.0">
<sequence>
<module name="Magento_Catalog"/>
<module name="Magento_Eav"/>
Expand Down

0 comments on commit baf700d

Please sign in to comment.