0

How to override function getAdditionalData() of Attributes.php file in magento 2.

di.xml file code,

<?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="Test\Details\Block\Attribute"/>
</config>

Block file code,

<?php
namespace Test\Details\Block;


class Attribute extends \Magento\Catalog\Block\Product\View\Attributes
{

    public function getAdditionalData(array $excludeAttr = [])
    {
        $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);
                }

//i have to remove if condtion from this function here....
                    $data[$attribute->getAttributeCode()] = [
                        'label' => __($attribute->getStoreLabel()),
                        'value' => $value,
                        'code' => $attribute->getAttributeCode(),
                    ];
            }
        }
        return $data;
    }    
}

After override block, Main information tab is remove automatically, I have to work in main information tab of product detail page.

i have checked log file and log error are display like this,

main.CRITICAL: Invalid template file: 'product/view/attributes.phtml' in module: 'Test_Details' block's name: 'product.attributes' [] [].

Any suggestion for issue?

2

Magento 2 uses the current module to get a template path. When overriding a block in this way but still want it to use the template from the original module you have to add a _toHtml function to your block to reset the module name like so...

protected function _toHtml()
{
    $this->setModuleName($this->extractModuleName('Magento\Catalog\Block\Product\View\Attributes'));
    return parent::_toHtml();
}

I found this from another thread that i cannot locate at the moment. If i do i will add the link.

|improve this answer|||||

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.