0

I hope someone can help me out with this one. I have Magento 1 code that needs to be converted to Magento 2 code. The function of the code is to display values from a custom product attribute on the product page. Each product uses one of many possible attribute sets. The Magento 1 layout code below references the template file:

<block type="catalog/product_view" name="product.info.key_features" as="product_info_key_features" template="catalog/product/view/key-features.phtml" />

And the key-features.phtml code is:

<?php /* @var Mage_Catalog_Block_Product_View $this */ ?>
<?php
/** @var Mage_Catalog_Model_Product $_product */
/** @var Mage_Eav_Model_Entity_Attribute_Set $attributeSet */
/** @var array $attributes */
$_product       = $this->getProduct();
$attributeSetId = $_product->getAttributeSetId();
$attributeSet   = Mage::getModel('eav/entity_attribute_set')->load($attributeSetId);
$attributes     = Mage::getModel('catalog/product_attribute_api')->items($attributeSetId);
$limit          = (is_null($this->getData('limit')) ? 999 : $this->getData('limit'));
?>
<?php foreach ($attributes as $attribute) : ?>
<?php /** @var array $attribute */ ?>
<?php $attribute_code = $attribute['code']; ?>
 <?php if ($_product->getResource()->getAttribute($attribute_code)->getStoreLabel() == 'Key Features') : ?>
<?php
$key_features = $_product->getAttributeText($attribute_code);
if (!empty($key_features) && !is_array($key_features)) {
$key_features = array($key_features);
?>
<?php if (!empty($key_features) && count($key_features) > 0) : ?
<?php $key_features = array_slice($key_features, 0, $limit); ?>
<div class="product-attribute product-attribute--key-features">
<ul class="product-attribute__list">
<?php foreach ($key_features as $key_feature) : ?>
<li class="product-attribute__item">
<span class="product-attribute__text">
<?php echo $key_feature; ?>
</span>
</li>
<?php endforeach; // $key_features as $key_feature ?>
</ul>
</div>
<?php break; ?>
<?php endif; // is_array($key_features) s?>
<?php endif; // $_product->getResource()->getAttribute($attribute_code)->getStoreLabel() == 'Key Features' ?>
<?php endforeach; // $attributes as $attribute ?>

So now I need the equivalent code but for Magento 2. The custom attribute values display on the product page in the same place.

Thank you for reading and hopefully someone can help me.

Andy

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.