I have created a simple phtml file to add a 3D viewer in the product page using the following code. I have also created a custom attribute for the product, called model_file_name
in which I type the name of the glb
file I want to display.
If I replace FILENAMEHERE
with an existing file name, the 3D model is shown in the page, the only problem I have is passing a file name specific for every product.
I have found some examples about how to read an attribute, but none of them refers to the code included in a .phtml file. The sample below is using the ProductRepository, but the code does not work, the error is: Notice: Undefined variable: productRepository
The file is stored in the folder ./app/design/frontend/[CUSTOM_THEME]/theme/Magento_Catalog/templates/view/customtext.phtml
How do I read a custom attribute from a .phtml file ?
<?php
/*
* Some code here! to read the custom product attribute name model_file_name
* The following DOES NOT works:
* @var $productRepository MagentoCatalogModelProductRepository
*/
?>
<?php
$product = $productRepository->get("PRODUCTSKU");
$attributes = $product->getAttributes();
foreach($attributes as $a)
{
echo $a->getName()."n";
}
?>
<?php #echo $modelFileName ?>
<!-- Import the component -->
<script type="module" src="https://unpkg.com/@google/model-viewer/dist/model-viewer.min.js"></script>
<style>
.3d-model {
height: 100%;
width: 100%;
}
</style>
<!-- Use it like any other HTML element -->
<p>3D Model: </p>
<model-viewer style="width: 100%; height: 600px; top: 0px; background-color: 0xeeefff;"
id="3d-model" class="3d-model" alt="3d model"
src="http://vrmur.local/media/models/FILENAMEHERE" ar environment-image=""
poster="" shadow-intensity="1" camera-controls>
</model-viewer>