From baf700d9e2148ac97220acc8842703a709593801 Mon Sep 17 00:00:00 2001 From: "Galla, Daniel" Date: Fri, 25 Jan 2019 14:21:59 +0100 Subject: [PATCH] Add more default units - #22 #38 --- Setup/UpgradeData.php | 191 ++++++++++++++++++++++++++++++++++++++++++ etc/module.xml | 2 +- 2 files changed, 192 insertions(+), 1 deletion(-) create mode 100644 Setup/UpgradeData.php diff --git a/Setup/UpgradeData.php b/Setup/UpgradeData.php new file mode 100644 index 0000000..7aa4b68 --- /dev/null +++ b/Setup/UpgradeData.php @@ -0,0 +1,191 @@ +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 + ); + } +} \ No newline at end of file diff --git a/etc/module.xml b/etc/module.xml index a7b8ecd..b1250b5 100644 --- a/etc/module.xml +++ b/etc/module.xml @@ -18,7 +18,7 @@ */ --> - +