![]() Server : Apache System : Linux server2.corals.io 4.18.0-348.2.1.el8_5.x86_64 #1 SMP Mon Nov 15 09:17:08 EST 2021 x86_64 User : corals ( 1002) PHP Version : 7.4.33 Disable Function : exec,passthru,shell_exec,system Directory : /home/corals/old/app/code/Cnc/Catalog/Plugin/Block/Product/View/ |
<?php /** * Copyright (c) 2020 Kaliop Digital Commerce (https://digitalcommerce.kaliop.com) All Rights Reserved. * https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) * Cnc * Radosław Stępień <[email protected]> <[email protected]> */ namespace Cnc\Catalog\Plugin\Block\Product\View; use Magento\Catalog\Block\Product\View\Attributes as BaseAttributes; use Magento\Catalog\Model\Product; use Magento\Eav\Api\AttributeRepositoryInterface; use Magento\Framework\App\Config\ScopeConfigInterface; use Magento\Framework\Exception\NoSuchEntityException; use Magento\Store\Model\ScopeInterface; class Attributes { /** * Attributes codes to get from children of current grouped product, * to assign them as grouped product additional data */ const ATTRIBUTES_TO_GET_FROM_CHILDREN = [ 'weight' ]; /** @var AttributeRepositoryInterface */ protected $attributeRepository; /** @var ScopeConfigInterface */ protected $scopeConfig; /** * Attributes constructor. * @param AttributeRepositoryInterface $attributeRepository * @param ScopeConfigInterface $scopeConfig */ public function __construct( AttributeRepositoryInterface $attributeRepository, ScopeConfigInterface $scopeConfig ) { $this->attributeRepository = $attributeRepository; $this->scopeConfig = $scopeConfig; } /** * @param BaseAttributes $subject * @param array $result * @param array $excludeAttr * @return array * @throws NoSuchEntityException */ public function afterGetAdditionalData( BaseAttributes $subject, array $result, array $excludeAttr = [] ) { if ($subject->getProduct()->getTypeId() == 'grouped') { if (count($relatedProducts = $subject->getProduct()->getTypeInstance()->getAssociatedProducts($subject->getProduct()))) { /** @var Product $child */ $child = reset($relatedProducts); foreach (self::ATTRIBUTES_TO_GET_FROM_CHILDREN as $attributeCode) { $attribute = $this->attributeRepository->get(Product::ENTITY, $attributeCode); $value = $attribute->getFrontend()->getValue($child); if ($attributeCode == 'weight') { $value = number_format($value, 2) . ' ' . $this->scopeConfig->getValue( 'general/locale/weight_unit', ScopeInterface::SCOPE_STORE ); } $result[$attribute->getAttributeCode()] = [ 'label' => $attribute->getStoreLabel(), 'value' => $value, 'code' => $attribute->getAttributeCode(), ]; } } } return $result; } }