1

How to override Product View Attributes block:

vendor/magento/module-catalog/Block/Product/View/Attributes.php in Magento 2?

Jsp/Attributes/etc/module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Jsp_Attributes" setup_version="2.0.0">
        <sequence>
            <module name="Magento_Catalog"/>
        </sequence>
    </module>
</config>

Jsp/Attributes/etc/frontend/di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Magento\Catalog\Block\Product\View\Attributes"
                type="Jsp\Attributes\Block\Product\View\Attributes" />
</config>

Jsp/Attributes/registration.php

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
  \Magento\Framework\Component\ComponentRegistrar::MODULE,
  'Jsp_Attributes',
  __DIR__
);

Jsp/Attributes/Block/Product/View/Attributes.php

<?php

namespace Jsp\Attributes\Block\Product\View;

class Attributes extends \Magento\Catalog\Block\Product\View\Attributes
{
    public function getAdditionalData(array $excludeAttr = ['colorprueba'])
    {
        $data = [];
        $product = $this->getProduct();
        $attributes = $product->getAttributes();
        foreach ($attributes as $attribute) {
            if ($attribute->getIsVisibleOnFront() && !in_array($attribute->getAttributeCode(), $excludeAttr)) {
                $value = $attribute->getFrontend()->getValue($product);

                if (!$product->hasData($attribute->getAttributeCode())) {
                    $value = __('N/A');
                } elseif ((string)$value == '') {
                    $value = __('No');
                } elseif ($attribute->getFrontendInput() == 'price' && is_string($value)) {
                    $value = $this->priceCurrency->convertAndFormat($value);
                }

                if (is_string($value) && strlen($value)) {
                    $data[$attribute->getAttributeCode()] = [
                        'label' => __($attribute->getStoreLabel()),
                        'value' => $value,
                        'code' => $attribute->getAttributeCode(),
                    ];
                }
            }
        }
        return $data;
    }
}
3
1

In your custom module:

app/code/Vendor/Module/etc/frontend/di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Magento\Catalog\Block\Product\View\Attributes"
                type="Vendor\Module\Block\Product\View\Attributes" />
</config>

app/code/Vendor/Module/Block/Product/View/Attributes.php

<?php

namespace Vendor\Module\Block\Product\View;

class Attributes extends \Magento\Catalog\Block\Product\View\Attributes
{
    //Override here...
}

Remember to add the sequence module:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Vendor_Module" setup_version="1.0.0">
        <sequence>
            <module name="Magento_Catalog"/>
        </sequence>
    </module>
</config>
9
  • This is working solution, but some problems: 1. use scope-specific di.xml (frontend/di.xml) 2. preference does not good idea for reliable extension.
    – Max
    Jan 7 '17 at 16:41
  • @MaxStsepantsevich 1) I'm not test yet, but I agree. May need to test later 2) we will use Plugin or any idea? Jan 7 '17 at 16:47
  • 2
    2. Yes, i think that using plugins or layout modification are better solutions :)
    – Max
    Jan 8 '17 at 15:40
  • I tried your code, however now the "More Information" tab dissappears from product pages Jan 9 '17 at 17:45
  • @LuisGarcia I update my answer. Remember to add the sequence module. Jan 10 '17 at 0:13

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.