diff --git a/vendor/magento/module-catalog/Block/Product/View/Attributes.php b/vendor/magento/module-catalog/Block/Product/View/Attributes.php index 9fd840b264085..0fad5a86d80bf 100644 --- a/vendor/magento/module-catalog/Block/Product/View/Attributes.php +++ b/vendor/magento/module-catalog/Block/Product/View/Attributes.php @@ -12,6 +12,8 @@ namespace Magento\Catalog\Block\Product\View; use Magento\Catalog\Model\Product; +use Magento\Directory\Helper\Data as DirectoryHelper; +use Magento\Eav\Model\Entity\Attribute\AbstractAttribute; use Magento\Framework\Phrase; use Magento\Framework\Pricing\PriceCurrencyInterface; @@ -29,8 +31,6 @@ class Attributes extends \Magento\Framework\View\Element\Template protected $_product = null; /** - * Core registry - * * @var \Magento\Framework\Registry */ protected $_coreRegistry = null; @@ -40,20 +40,29 @@ class Attributes extends \Magento\Framework\View\Element\Template */ protected $priceCurrency; + /** + * @var DirectoryHelper + */ + private DirectoryHelper $directoryHelper; + /** * @param \Magento\Framework\View\Element\Template\Context $context * @param \Magento\Framework\Registry $registry * @param PriceCurrencyInterface $priceCurrency * @param array $data + * @param DirectoryHelper|null $directoryHelper */ public function __construct( \Magento\Framework\View\Element\Template\Context $context, \Magento\Framework\Registry $registry, PriceCurrencyInterface $priceCurrency, - array $data = [] + array $data = [], + ?DirectoryHelper $directoryHelper = null ) { $this->priceCurrency = $priceCurrency; $this->_coreRegistry = $registry; + $this->directoryHelper = $directoryHelper + ?? \Magento\Framework\App\ObjectManager::getInstance()->get(DirectoryHelper::class); parent::__construct($context, $data); } @@ -86,11 +95,7 @@ public function getAdditionalData(array $excludeAttr = []) if ($this->isVisibleOnFrontend($attribute, $excludeAttr)) { $value = $attribute->getFrontend()->getValue($product); - if ($value instanceof Phrase) { - $value = (string)$value; - } elseif ($attribute->getFrontendInput() == 'price' && is_string($value)) { - $value = $this->priceCurrency->convertAndFormat($value); - } + $value = $this->formatAttributeValue($attribute, $value); if (is_string($value) && strlen(trim($value))) { $data[$attribute->getAttributeCode()] = [ @@ -104,16 +109,42 @@ public function getAdditionalData(array $excludeAttr = []) return $data; } + /** + * Format attribute value for frontend display + * + * @param AbstractAttribute $attribute + * @param mixed $value + * @return mixed + */ + private function formatAttributeValue( + AbstractAttribute $attribute, + mixed $value + ): mixed { + if ($value instanceof Phrase) { + return (string)$value; + } + if ($attribute->getFrontendInput() == 'price' && is_string($value)) { + return $this->priceCurrency->convertAndFormat($value); + } + if ($attribute->getAttributeCode() === 'weight' && is_string($value)) { + $weightUnit = $this->directoryHelper->getWeightUnit(); + if ($weightUnit) { + return $value . ' ' . $weightUnit; + } + } + return $value; + } + /** * Determine if we should display the attribute on the front-end * - * @param \Magento\Eav\Model\Entity\Attribute\AbstractAttribute $attribute + * @param AbstractAttribute $attribute * @param array $excludeAttr * @return bool * @since 103.0.0 */ protected function isVisibleOnFrontend( - \Magento\Eav\Model\Entity\Attribute\AbstractAttribute $attribute, + AbstractAttribute $attribute, array $excludeAttr ) { return ($attribute->getIsVisibleOnFront() && !in_array($attribute->getAttributeCode(), $excludeAttr));