Skip to content

Commit

Permalink
Add currency helper (#10)
Browse files Browse the repository at this point in the history
* Add currency helper

* Cleanup currency helper
  • Loading branch information
k-k-spiilmusic authored and luckyraul committed Jun 4, 2018
1 parent 439f978 commit ea6b0c3
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 2 deletions.
94 changes: 94 additions & 0 deletions Helper/Currency.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php
/**
* @author Mygento
* @copyright See COPYING.txt for license details.
* @package Mygento_Base
*/

namespace Mygento\Base\Helper;

use Magento\Directory\Model\CurrencyFactory;
use Magento\Framework\App\Helper\AbstractHelper;
use Magento\Framework\App\Helper\Context;
use Magento\Store\Model\StoreManagerInterface;

/**
* Base Data helper
*/
class Currency extends AbstractHelper
{
/**
* @var StoreManagerInterface
*/
private $storeManager;

/**
* @var CurrencyFactory
*/
private $currencyFactory;

/**
* Currency constructor.
* @param Context $context
* @param StoreManagerInterface $storeManager
* @param CurrencyFactory $currencyFactory
*/
public function __construct(
Context $context,
StoreManagerInterface $storeManager,
CurrencyFactory $currencyFactory
) {
parent::__construct($context);
$this->storeManager = $storeManager;
$this->currencyFactory = $currencyFactory;
}

/**
* @param $amountValue
* @param null $currencyCodeFrom
* @param null $currencyCodeTo
* @return float|int
* @throws \Magento\Framework\Exception\NoSuchEntityException
*/
public function convert($amountValue, $currencyCodeFrom = null, $currencyCodeTo = null)
{
/**
* If is not specified the currency code from which we want to convert
* - use current currency
*/
if (!$currencyCodeFrom) {
$currencyCodeFrom = $this->storeManager->getStore()
->getCurrentCurrency()->getCode();
}

/**
* If is not specified the currency code to which we want to convert
* - use base currency
*/
if (!$currencyCodeTo) {
$currencyCodeTo = $this->storeManager->getStore()
->getBaseCurrency()->getCode();
}

/**
* Do not convert if currency is same
*/
if ($currencyCodeFrom == $currencyCodeTo) {
return $amountValue;
}

/** @var float $rate */
// Get rate
$rate = $this->currencyFactory->create()
->load($currencyCodeFrom)->getAnyRate($currencyCodeTo);

if (!$rate) {
throw new \Exception(__('Cannot find currency rate'));
}

// Get amount in new currency
$amountValue = $amountValue * $rate;

return $amountValue;
}
}
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "mygento/base",
"type": "magento2-module",
"version": "2.2.11",
"version": "2.2.12",
"license": "OSL-3.0",
"homepage": "https://github.com/mygento/base",
"description": "Mygento Base",
Expand Down
2 changes: 1 addition & 1 deletion etc/module.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Mygento_Base" setup_version="2.2.11"></module>
<module name="Mygento_Base" setup_version="2.2.12"></module>
</config>

0 comments on commit ea6b0c3

Please sign in to comment.