系统学习magento二次开发,推荐小册:《Magento中文全栈二次开发 》
本小册面向Magento2以上版本,书代码及示例兼容magento2.0-2.4版本。涵盖了magento前端开发,后端开发,magento2主题,magento2重写,magento2 layout,magento2控制器,magento2 block等相关内容,带领您成为magento开发技术专家。
在Magento 2中舍入所有价格的步骤:
第1步: 首先, 在我们的扩展名中,在下面的路径上创建一个“di.xml”文件。
app\code\Vendor\Extension\etc\di.xml
现在,添加此代码
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> <preference for="Magento\Framework\Pricing\Render\Amount" type="Vendor\Extension\Pricing\Render\Amount"/> </config>
步骤2: 之后, 在以下扩展名文件夹路径内创建一个“金额.php”文件。
app\code\Vendor\Extension\Pricing\Render\Amount.php
并且,添加代码,如下所述
<?php namespace Vendor\Extension\Pricing\Render; use Magento\Framework\Pricing\Amount\AmountInterface; use Magento\Framework\Pricing\SaleableInterface; use Magento\Framework\Pricing\Price\PriceInterface; use Magento\Framework\Pricing\PriceCurrencyInterface; use Magento\Framework\View\Element\Template; class Amount extends Template implements \Magento\Framework\Pricing\Render\AmountRenderInterface { protected $saleableItem; protected $price; protected $adjustmentRenders; protected $priceCurrency; protected $rendererPool; protected $amount; protected $displayValue; protected $adjustmentsHtml = []; public function __construct( Template\Context $context, AmountInterface $amount, PriceCurrencyInterface $priceCurrency, \Magento\Framework\Pricing\Render\RendererPool $rendererPool, SaleableInterface $saleableItem = null, PriceInterface $price = null, array $data = []) { parent::__construct($context, $data); $this->amount = $amount; $this->saleableItem = $saleableItem; $this->price = $price; $this->priceCurrency = $priceCurrency; $this->rendererPool = $rendererPool; } public function setDisplayValue($value) { $this->displayValue = $value; } public function getDisplayValue() { if ($this->displayValue !== null) { return round($this->displayValue); } else { return round($this->getAmount()->getValue()); } } public function getAmount() { return $this->amount; } public function getSaleableItem() { return $this->saleableItem; } public function getPrice() { return $this->price; } public function getDisplayCurrencyCode() { return $this->priceCurrency->getCurrency()->getCurrencyCode(); } public function getDisplayCurrencySymbol() { return $this->priceCurrency->getCurrencySymbol(); } public function hasAdjustmentsHtml() { return (bool) count($this->adjustmentsHtml); } public function getAdjustmentsHtml() { return implode('', $this->adjustmentsHtml); } protected function _toHtml() { $adjustmentRenders = $this->getAdjustmentRenders(); if ($adjustmentRenders) { $adjustmentHtml = $this->getAdjustments($adjustmentRenders); if (!$this->hasSkipAdjustments() || ($this->hasSkipAdjustments() && $this->getSkipAdjustments() == false)) { $this->adjustmentsHtml = $adjustmentHtml; } } $html = parent::_toHtml(); return $html; } protected function getAdjustmentRenders() { return $this->rendererPool->getAdjustmentRenders($this->saleableItem, $this->price); } protected function getAdjustments($adjustmentRenders) { $this->setAdjustmentCssClasses($adjustmentRenders); $data = $this->getData(); $adjustments = []; foreach ($adjustmentRenders as $adjustmentRender) { if ($this->getAmount()->getAdjustmentAmount($adjustmentRender->getAdjustmentCode()) !== false) { $html = $adjustmentRender->render($this, $data); if (trim($html)) { $adjustments[$adjustmentRender->getAdjustmentCode()] = $html; } } } return $adjustments; } public function formatCurrency( $amount, $includeContainer = true, $precision = PriceCurrencyInterface::DEFAULT_PRECISION) { return $this->priceCurrency->format($amount, $includeContainer, $precision); } protected function setAdjustmentCssClasses($adjustmentRenders) { $cssClasses = $this->hasData('css_classes') ? explode(' ', $this->getData('css_classes')) : []; $cssClasses = array_merge($cssClasses, array_keys($adjustmentRenders)); $this->setData('adjustment_css_classes', join(' ', $cssClasses)); return $this; } }
结论:
通过这种方式,您可以四舍五入Magento 2中的所有价格。