Last active
July 8, 2022 18:09
-
-
Save markshust/812af157fc948c20fb1df80dce13a311 to your computer and use it in GitHub Desktop.
Get specific attribute data for the current product with a ViewModel
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?xml version="1.0"?> | |
| <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd"> | |
| <body> | |
| <referenceContainer name="content"> | |
| <block name="my_block" template="Macademy_ProductInfo::my-attribute.phtml"> | |
| <arguments> | |
| <argument name="filtered_attribute_view_model" xsi:type="object">Macademy\ProductInfo\ViewModel\FilteredAttribute</argument> | |
| </arguments> | |
| </block> | |
| </referenceContainer> | |
| </body> | |
| </page> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** @var \Magento\Framework\View\Element\Template $block */ | |
| /** @var \Macademy\ProductInfo\ViewModel\FilteredAttribute $filteredAttributeViewModel */ | |
| $filteredAttributeViewModel = $block->getData('filtered_attribute_view_model'); | |
| ?> | |
| <div>My Attribute</div> | |
| <p><?= $filteredAttributeViewModel->getMyAttributeLabel() ?></p> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php declare(strict_types=1); | |
| namespace Macademy\ProductInfo\ViewModel; | |
| use Magento\Catalog\Model\ProductRepository; | |
| use Magento\Framework\App\RequestInterface; | |
| use Magento\Framework\Exception\NoSuchEntityException; | |
| use Magento\Framework\View\Element\Block\ArgumentInterface; | |
| class FilteredAttribute implements ArgumentInterface | |
| { | |
| public function __construct( | |
| private RequestInterface $request, | |
| private ProductRepository $productRepository, | |
| ) {} | |
| /** | |
| * @throws NoSuchEntityException | |
| */ | |
| public function getMyAttributeLabel(): ?string | |
| { | |
| $productId = $this->request->getParam('id'); | |
| $product = $this->productRepository->getById($productId); | |
| $attributes = $product->getAttributes(); | |
| $myAttribute = null; | |
| foreach ($attributes as $attribute) { | |
| if ($attribute->getName() === 'name') { | |
| $myAttribute = $attribute; | |
| } | |
| } | |
| return $myAttribute->getFrontendLabel(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment