![]() 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/cartforge.co/app/code/CartForge/CustomWidgets/Block/Widget/ |
<?php namespace CartForge\CustomWidgets\Block\Widget; use Magento\Framework\View\Element\Template; use Magento\Framework\View\Element\Template\Context; use Magento\Framework\Registry; use Magento\Widget\Block\BlockInterface; use Magento\Framework\App\ResourceConnection; class TechnicalDetails extends Template implements BlockInterface { protected $_template = "widget/technical_details.phtml"; protected $registry; protected $resourceConnection; public function __construct( Context $context, Registry $registry, ResourceConnection $resourceConnection, array $data = [] ) { $this->registry = $registry; $this->resourceConnection = $resourceConnection; parent::__construct($context, $data); } public function getStandardsAttribute(): array { $product = $this->registry->registry('current_product'); $standardsAttribute = []; if ($product && $product->getId()) { $standardsValue = $this->fetchAttributeValue('standards', $product->getId()); if ($standardsValue !== null) { $standardsAttribute = [ 'label' => __('Standards'), 'value' => $standardsValue ]; } } return $standardsAttribute; } protected function fetchAttributeValue($attributeCode, $productId) { $connection = $this->resourceConnection->getConnection(); $attributeTable = $connection->getTableName('eav_attribute'); $valueTable = $connection->getTableName('catalog_product_entity_text'); $select = $connection->select() ->from(['cpet' => $valueTable], ['value']) ->joinInner( ['ea' => $attributeTable], 'ea.attribute_id = cpet.attribute_id', [] ) ->where('ea.attribute_code = ?', $attributeCode) ->where('cpet.entity_id = ?', $productId); return $connection->fetchOne($select); } public function getTitle() { return $this->getData('title'); } public function getProductDetails() { $product = $this->registry->registry('current_product'); $childsArray = []; if (!$product->getData('show_documents')) { return []; } if ($product && $product->getTypeId() === \Magento\ConfigurableProduct\Model\Product\Type\Configurable::TYPE_CODE) { $childProducts = $product->getTypeInstance()->getUsedProducts($product); $childsArray = []; // Initialize the array foreach ($childProducts as $child) { if ($child) { // Get the SKU and color of the child product $childSku = $child->getSku(); $childColor = $child->getAttributeText('color'); $parentSku = $product->getSku(); // Check if the color is already in the $childsArray $colorExists = false; foreach ($childsArray as $existingChild) { if ($existingChild['childColor'] === $childColor) { $colorExists = true; break; } } // If the color does not exist, add it to the array if (!$colorExists) { $childsArray[] = [ 'childSku' => $childSku, 'childColor' => $childColor, 'parentSku' => $parentSku ]; } } } } return $childsArray; } }