Trong bài viết này tôi sẽ chỉ cho bạn cách để format price với currency trong Magento 2.
Bạn có thể format price với các class sau:
- Magento\Framework\Pricing\PriceCurrencyInterface
- Magento\Framework\Pricing\Helper\Data
1. Format price bằng cách sử dụng class Magento\Framework\Pricing\Helper\Data
Bạn có thể sử dụng class Magento\Framework\Pricing\Helper\Data
như một dependency.
Class này có 2 method currency
và currencyByStore
. Bạn có thể sử dụng currency method để get price với currency theo store hiện tại. Để get price ở một store khác, bạn có thể sử dụng currencyByStore method.
Giải thích về method:
1 |
currency(<strong>$value,</strong> <strong>$format</strong> = <strong>true</strong>, <strong>$includeContainer</strong> = <strong>true</strong>) |
Dưới đây là mô tả các tham số của currency method:
- $value(float) truyền vào price value.
- $format(bool ) : xác định có format price hay không.
- $includeContainer (bool): Có sử dụng price html hay không.
1 |
currencyByStore(<strong>$value,</strong> <strong>$store</strong> = <strong>null</strong>, <strong>$format</strong> = <strong>true</strong>, <strong>$includeContainer</strong> = <strong>true</strong>) |
Đây là mô tả các tham số của currencyByStore method:
- $value(float) truyền vào price value.
- $store(int or \Magento\Store\Model\Store): Truyền vào store mà bạn muốn get currency
- $format(bool ) : xác định có format price hay không.
- $includeContainer (bool): Có sử dụng price html hay không.
Format price trong phtml thông qua helper:
1 |
<strong>$this</strong>->helper('Magento\Framework\Pricing\Helper\Data')->currency(number_format(50,2),<strong>true</strong>,<strong>false</strong>); |
Sử dụng ObjectManager:
1 2 3 4 |
<strong>$objectManager</strong> = \Magento\Framework\App\ObjectManager::getInstance(); <strong>$priceHelper</strong> = <strong>$objectManager->create</strong>('Magento\Framework\Pricing\Helper\Data'); <strong>$price</strong> = 100; <strong>$formattedPrice</strong> = <strong>$priceHelper->currency</strong>(<strong>$price,</strong> <strong>true</strong>, <strong>false</strong>); |
Lưu ý rằng sử dụng objectManager không phải là một cách được khuyến khích. Xem thêm ObjectManager – Magento 2 Developer Documentation
Sử dụng dependency injection:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
<?php <strong>namespace</strong> Namespace\MyModule\Model; <strong>class</strong> MyClass { /** * @var Magento\Framework\Pricing\Helper\Data */ <strong>private</strong> <strong>$pricingHelper</strong>; /** * MyClass constructor. * @param Magento\Framework\Pricing\Helper\Data $pricingHelper */ <strong>public</strong> <strong>function</strong> __construct(\Magento\Framework\Pricing\Helper\Data <strong>$pricingHelper</strong>) { <strong>$this</strong>->pricingHelper = <strong>$pricingHelper</strong>; } /** * Convert and format price value for current application store * * @return float|string */ <strong>public</strong> <strong>function</strong> getFormattedPrice() { <strong>$price</strong> = 100; <strong>return</strong> <strong>$this</strong>->pricingHelper->currency(<strong>$price,</strong> <strong>true</strong>, <strong>false</strong>); } /** * Convert and format price value for specified store * * @return float|string */ <strong>public</strong> <strong>function</strong> getFormattedPriceByStore() { <strong>$price</strong> = 100; <strong>$storeId</strong> = 1; <strong>return</strong> <strong>$this</strong>->pricingHelper->currencyByStore(<strong>$price,</strong> <strong>$storeId,</strong> <strong>true</strong>, <strong>false</strong>) } } |
2. Format price sử dụng class Magento\Framework\Pricing\PriceCurrencyInterface
Interface này có 3 phương thức chính:
- convertAndFormat($price) : Chuyển simple price value thành price được định dạng.
- round($price) : Làm tròn price.
- getCurrencySymbol() : Get đơn vị tiền tệ theo store view hiện tại.
Sử dụng Object Manager:
1 2 3 4 5 6 |
<strong>$objectManager</strong> = \Magento\Framework\App\ObjectManager::getInstance(); <strong>$priceInterface</strong> = <strong>$objectManager->create</strong>('Magento\Framework\Pricing\PriceCurrencyInterface'); <strong>$price</strong> = 70.0000; //Your Price <strong>$priceInterface->convertAndFormat</strong>(<strong>$price</strong>); // Output: $70.00 <strong>$priceInterface->round</strong>(<strong>$price</strong>); // Output: 70 <strong>$priceInterface->getCurrencySymbol</strong>(); // Output: $ |
Sử dụng Dependency Injection:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
<?php <strong>namespace</strong> Namespace\MyModule\Model; <strong>class</strong> MyClass { /** * @var \Magento\Framework\Pricing\PriceCurrencyInterface */ <strong>protected</strong> <strong>$priceCurrency</strong>; /** * @param \Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency */ <strong>public</strong> <strong>function</strong> __construct( \Magento\Framework\Pricing\PriceCurrencyInterface <strong>$priceCurrency</strong> ) { <strong>$this</strong>->priceCurrency = <strong>$priceCurrency</strong>; } /** * FGet current store currency symbol with price * * @param float $amount * @param bool $includeContainer * @param int $precision * @param null|string|bool|int|\Magento\Framework\App\ScopeInterface $scope * @param \Magento\Framework\Model\AbstractModel|string|null $currency * @return float */ <strong>public</strong> <strong>function</strong> getCurrencyFormat(<strong>$price,</strong> <strong>$includeContainer</strong> = <strong>false</strong>, <strong>$precision</strong>) { //Example: $this->priceCurrency->format($price,true,2); <strong>return</strong> <strong>$this</strong>->priceCurrency->format( <strong>$price,</strong> <strong>$includeContainer</strong> = <strong>true</strong>, <strong>$precision</strong> = <strong>self</strong>::DEFAULT_PRECISION, <strong>$scope</strong> = <strong>null</strong>, <strong>$currency</strong> = <strong>null</strong> ); } /** * Round price * * @deprecated 102.0.1 * @param float $price * @return float */ <strong>public</strong> <strong>function</strong> getRoundPrice(<strong>$price</strong>) { <strong>return</strong> <strong>$this</strong>->priceCurrency->round(<strong>$price</strong>); } /** * Get CurrencySymbol of current store * @return string */ <strong>public</strong> <strong>function</strong> getCurrencySymbol() { <strong>return</strong> <strong>$this</strong>->priceCurrency->getCurrencySymbol(); } } |
Dưới đây là ví dụ về kết quả bạn sẽ nhận được khi sử dụng các phương thức trên:
1 2 3 |
<strong>echo</strong> <strong>$this</strong>->getCurrencyFormat(70.0000); //output $70.00 <strong>echo</strong> <strong>$this</strong>->getRoundPrice(70.0000); //output 70 (without currency symbol) <strong>echo</strong> <strong>$this</strong>->getCurrencySymbol(); //output $ |
Hy vọng thông qua bài viết có thể giúp bạn hiểu về các cách để format price với currency trong Magento 2. Nếu có bất kỳ thắc mắc gì về bài viết hãy để lại bình luận bên dưới.