Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
PriceCurrency.php
Go to the documentation of this file.
1 <?php
7 
10 use Psr\Log\LoggerInterface as Logger;
12 
17 {
21  protected $storeManager;
22 
26  protected $currencyFactory;
27 
31  protected $logger;
32 
38  public function __construct(
40  CurrencyFactory $currencyFactory,
41  Logger $logger
42  ) {
43  $this->storeManager = $storeManager;
44  $this->currencyFactory = $currencyFactory;
45  $this->logger = $logger;
46  }
47 
51  public function convert($amount, $scope = null, $currency = null)
52  {
53  $currentCurrency = $this->getCurrency($scope, $currency);
54 
55  return $this->getStore($scope)
56  ->getBaseCurrency()
57  ->convert($amount, $currentCurrency);
58  }
59 
63  public function convertAndRound($amount, $scope = null, $currency = null, $precision = self::DEFAULT_PRECISION)
64  {
65  $currentCurrency = $this->getCurrency($scope, $currency);
66  $convertedValue = $this->getStore($scope)->getBaseCurrency()->convert($amount, $currentCurrency);
67  return round($convertedValue, $precision);
68  }
69 
73  public function format(
74  $amount,
75  $includeContainer = true,
76  $precision = self::DEFAULT_PRECISION,
77  $scope = null,
78  $currency = null
79  ) {
80  return $this->getCurrency($scope, $currency)
81  ->formatPrecision($amount, $precision, [], $includeContainer);
82  }
83 
87  public function convertAndFormat(
88  $amount,
89  $includeContainer = true,
90  $precision = self::DEFAULT_PRECISION,
91  $scope = null,
92  $currency = null
93  ) {
94  $amount = $this->convert($amount, $scope, $currency);
95 
96  return $this->format($amount, $includeContainer, $precision, $scope, $currency);
97  }
98 
102  public function getCurrency($scope = null, $currency = null)
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  }
119 
125  public function getCurrencySymbol($scope = null, $currency = null)
126  {
127  return $this->getCurrency($scope, $currency)->getCurrencySymbol();
128  }
129 
136  protected function getStore($scope = null)
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  }
149 
156  public function round($price)
157  {
158  return round($price, 2);
159  }
160 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
format( $amount, $includeContainer=true, $precision=self::DEFAULT_PRECISION, $scope=null, $currency=null)
convertAndRound($amount, $scope=null, $currency=null, $precision=self::DEFAULT_PRECISION)
$price
__construct(StoreManagerInterface $storeManager, CurrencyFactory $currencyFactory, Logger $logger)
$amount
Definition: order.php:14
convert($amount, $scope=null, $currency=null)
getCurrency($scope=null, $currency=null)
convertAndFormat( $amount, $includeContainer=true, $precision=self::DEFAULT_PRECISION, $scope=null, $currency=null)
getCurrencySymbol($scope=null, $currency=null)