Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Protected Member Functions | Protected Attributes
PriceCurrency Class Reference
Inheritance diagram for PriceCurrency:
PriceCurrencyInterface

Public Member Functions

 __construct (StoreManagerInterface $storeManager, CurrencyFactory $currencyFactory, Logger $logger)
 
 convert ($amount, $scope=null, $currency=null)
 
 convertAndRound ($amount, $scope=null, $currency=null, $precision=self::DEFAULT_PRECISION)
 
 format ( $amount, $includeContainer=true, $precision=self::DEFAULT_PRECISION, $scope=null, $currency=null)
 
 convertAndFormat ( $amount, $includeContainer=true, $precision=self::DEFAULT_PRECISION, $scope=null, $currency=null)
 
 getCurrency ($scope=null, $currency=null)
 
 getCurrencySymbol ($scope=null, $currency=null)
 
 round ($price)
 

Protected Member Functions

 getStore ($scope=null)
 

Protected Attributes

 $storeManager
 
 $currencyFactory
 
 $logger
 

Additional Inherited Members

- Data Fields inherited from PriceCurrencyInterface
const DEFAULT_PRECISION = 2
 

Detailed Description

Class PriceCurrency model for convert and format price value

Definition at line 16 of file PriceCurrency.php.

Constructor & Destructor Documentation

◆ __construct()

__construct ( StoreManagerInterface  $storeManager,
CurrencyFactory  $currencyFactory,
Logger  $logger 
)
Parameters
\Magento\Store\Model\StoreManagerInterface$storeManager
CurrencyFactory$currencyFactory
Logger$logger

Definition at line 38 of file PriceCurrency.php.

42  {
43  $this->storeManager = $storeManager;
44  $this->currencyFactory = $currencyFactory;
45  $this->logger = $logger;
46  }

Member Function Documentation

◆ convert()

convert (   $amount,
  $scope = null,
  $currency = null 
)

{Convert price value

Parameters
float$amount
null | string | bool | int | \Magento\Framework\App\ScopeInterface$scope
\Magento\Framework\Model\AbstractModel | string | null$currency
Returns
float
}

Implements PriceCurrencyInterface.

Definition at line 51 of file PriceCurrency.php.

52  {
53  $currentCurrency = $this->getCurrency($scope, $currency);
54 
55  return $this->getStore($scope)
56  ->getBaseCurrency()
57  ->convert($amount, $currentCurrency);
58  }
$amount
Definition: order.php:14
getCurrency($scope=null, $currency=null)

◆ convertAndFormat()

convertAndFormat (   $amount,
  $includeContainer = true,
  $precision = self::DEFAULT_PRECISION,
  $scope = null,
  $currency = null 
)

{Convert and format price value

Parameters
float$amount
bool$includeContainer
int$precision
null | string | bool | int | \Magento\Framework\App\ScopeInterface$scope
\Magento\Framework\Model\AbstractModel | string | null$currency
Returns
string
}

Implements PriceCurrencyInterface.

Definition at line 87 of file PriceCurrency.php.

93  {
94  $amount = $this->convert($amount, $scope, $currency);
95 
96  return $this->format($amount, $includeContainer, $precision, $scope, $currency);
97  }
format( $amount, $includeContainer=true, $precision=self::DEFAULT_PRECISION, $scope=null, $currency=null)
$amount
Definition: order.php:14
convert($amount, $scope=null, $currency=null)

◆ convertAndRound()

convertAndRound (   $amount,
  $scope = null,
  $currency = null,
  $precision = self::DEFAULT_PRECISION 
)

{Convert and round price value

Parameters
float$amount
null | string | bool | int | \Magento\Framework\App\ScopeInterface$scope
\Magento\Framework\Model\AbstractModel | string | null$currency
int$precision
Returns
float
}

Implements PriceCurrencyInterface.

Definition at line 63 of file PriceCurrency.php.

64  {
65  $currentCurrency = $this->getCurrency($scope, $currency);
66  $convertedValue = $this->getStore($scope)->getBaseCurrency()->convert($amount, $currentCurrency);
67  return round($convertedValue, $precision);
68  }
$amount
Definition: order.php:14
getCurrency($scope=null, $currency=null)

◆ format()

format (   $amount,
  $includeContainer = true,
  $precision = self::DEFAULT_PRECISION,
  $scope = null,
  $currency = null 
)

{Format price value

Parameters
float$amount
bool$includeContainer
int$precision
null | string | bool | int | \Magento\Framework\App\ScopeInterface$scope
\Magento\Framework\Model\AbstractModel | string | null$currency
Returns
float
}

Implements PriceCurrencyInterface.

Definition at line 73 of file PriceCurrency.php.

79  {
80  return $this->getCurrency($scope, $currency)
81  ->formatPrecision($amount, $precision, [], $includeContainer);
82  }
$amount
Definition: order.php:14
getCurrency($scope=null, $currency=null)

◆ getCurrency()

getCurrency (   $scope = null,
  $currency = null 
)

{Get currency model

Parameters
null | string | bool | int | \Magento\Framework\App\ScopeInterface$scope
\Magento\Framework\Model\AbstractModel | string | null$currency
Returns
\Magento\Framework\Model\AbstractModel
}

Implements PriceCurrencyInterface.

Definition at line 102 of file PriceCurrency.php.

103  {
104  if ($currency instanceof Currency) {
105  $currentCurrency = $currency;
106  } elseif (is_string($currency)) {
107  $currency = $this->currencyFactory->create()
108  ->load($currency);
109  $baseCurrency = $this->getStore($scope)
110  ->getBaseCurrency();
111  $currentCurrency = $baseCurrency->getRate($currency) ? $currency : $baseCurrency;
112  } else {
113  $currentCurrency = $this->getStore($scope)
114  ->getCurrentCurrency();
115  }
116 
117  return $currentCurrency;
118  }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17

◆ getCurrencySymbol()

getCurrencySymbol (   $scope = null,
  $currency = null 
)
Parameters
null | string | bool | int | \Magento\Framework\App\ScopeInterface$scope
\Magento\Framework\Model\AbstractModel | string | null$currency
Returns
string

Implements PriceCurrencyInterface.

Definition at line 125 of file PriceCurrency.php.

126  {
127  return $this->getCurrency($scope, $currency)->getCurrencySymbol();
128  }
getCurrency($scope=null, $currency=null)

◆ getStore()

getStore (   $scope = null)
protected

Get store model

Parameters
null | string | bool | int | ScopeInterface$scope
Returns
Store

Definition at line 136 of file PriceCurrency.php.

137  {
138  try {
139  if (!$scope instanceof Store) {
140  $scope = $this->storeManager->getStore($scope);
141  }
142  } catch (\Exception $e) {
143  $this->logger->critical($e);
144  $scope = $this->storeManager->getStore();
145  }
146 
147  return $scope;
148  }

◆ round()

round (   $price)

Round price

Parameters
float$price
Returns
float

Implements PriceCurrencyInterface.

Definition at line 156 of file PriceCurrency.php.

157  {
158  return round($price, 2);
159  }
$price

Field Documentation

◆ $currencyFactory

$currencyFactory
protected

Definition at line 26 of file PriceCurrency.php.

◆ $logger

$logger
protected

Definition at line 31 of file PriceCurrency.php.

◆ $storeManager

$storeManager
protected

Definition at line 21 of file PriceCurrency.php.


The documentation for this class was generated from the following file: