0

How to Create custom attribute value replace into description attribute.

Ex :- My custom attribute all value replace product details tab means description attribute.

My Custom attribute is local_description.

Here is My code :-

<?php
$_helper = $this->helper(Magento\Catalog\Helper\Output::class);
$_product = $block->getProduct();

if (!$_product instanceof \Magento\Catalog\Model\Product) {
    return;
}

$_call = $block->getAtCall();
$_code = $block->getAtCode();
$_className = $block->getCssClass();
$_attributeLabel = $block->getAtLabel();
$_attributeType = $block->getAtType();
$_attributeAddAttribute = $block->getAddAttribute();

$renderLabel = true;
// if defined as 'none' in layout, do not render
if ($_attributeLabel == 'none') {
    $renderLabel = false;
}

if ($_attributeLabel && $_attributeLabel == 'default') {
    $_attributeLabel = $_product->getResource()->getAttribute($_code)->getStoreLabel();
}
if ($_attributeType && $_attributeType == 'text') {
    $_attributeValue = ($_helper->productAttribute($_product, $_product->$_call(), $_code))
        ? $_product->getAttributeText($_code)
        : '';
} else {
 if($_code == 'description')
{
   $_code = 'local_description';
}

    $_attributeValue = $_helper->productAttribute($_product, $_product->$_call(), $_code);
}
?>

<?php if ($_attributeValue) :?>
<div class="product attribute <?= $block->escapeHtmlAttr($_className) ?>">
    <?php if ($renderLabel) :?>
        <strong class="type"><?= $block->escapeHtml($_attributeLabel) ?></strong>
    <?php endif; ?>
    <div class="value" <?= /* @noEscape */ $_attributeAddAttribute ?>><?= /* @noEscape */ $_attributeValue ?></div>
</div>
<?php endif; ?>

THANKS.

11
  • where do you want to place your attribute
    – Pramod
    Aug 18 '20 at 5:22
  • Can you specify this in detail what you are looking for? Aug 18 '20 at 5:24
  • i think he wants to add and attribute inside description group
    – Pramod
    Aug 18 '20 at 5:25
  • @Dhiren Vasoya magento 2 product page details tab value remove and replace custom attribute value Aug 18 '20 at 5:29
  • @Dhiren Vasoya i dosen't want to new tab but i want replace value in detail tab. Please help Aug 18 '20 at 5:31
1

You can try the following.

Create catalog_product_view.xml in your module or theme.

And add the following code

<?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>
        <referenceBlock name="product.info.description">
            <arguments>
                <argument name="at_call" xsi:type="string">getLocalDescription</argument>
                <argument name="at_code" xsi:type="string">local_description</argument>
            </arguments>
        </referenceBlock>
    </body>
</page>

This is just the sample code, modify according to your requirement.

4
  • your code work in core file but add above code in custom module show this error :- prnt.sc/u1etj0 Aug 18 '20 at 6:41
  • @MohitPatel It was a tag mismatch issue. I have updated the tag in the answer. Aug 18 '20 at 6:43
  • Yes, i have add after show this error - prnt.sc/u1f320 Aug 18 '20 at 6:56
  • Remove the as attribute and try. I have updated it in the answer. Aug 18 '20 at 7:04
2

Hi without enough knowledge as per my understanding am posting this and which will add New Test Description field along with your description please let me know if this what you need or else please update your question with more details

Setup/InstallData.php

<?php
namespace Vendor\Module\Setup;

use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface;

class InstallData implements InstallDataInterface
{
    /**
     * EAV setup factory
     *
     * @var EavSetupFactory
     */
    private $eavSetupFactory;

    /**
     * Init
     *
     * @param EavSetupFactory $eavSetupFactory
     */
    public function __construct(EavSetupFactory $eavSetupFactory)
    {
        $this->eavSetupFactory = $eavSetupFactory;
    }

    /**
     * {@inheritdoc}
     * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
     */
    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        /** @var EavSetup $eavSetup */
        $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);

        /**
         * Add attributes to the eav_attribute
         */
        $eavSetup->removeAttribute(\Magento\Catalog\Model\Product::ENTITY, 'test_descp');
        $eavSetup->removeAttribute(\Magento\Catalog\Model\Product::ENTITY, 'test_descp');

        // $statusOptions = 'Rbj\ProductAttribute\Model\Config\Source\StatusOptions';
        $eavSetup->addAttribute(
            \Magento\Catalog\Model\Product::ENTITY,
            'test_descp',
            [
                'group' => 'Content',
                'type' => 'text',
                'backend' => '',
                'frontend' => '',
                'label' => 'New Test Description',
                'input' => 'text',
                'class' => '',
                'source' => '',
                'global' => ScopedAttributeInterface::SCOPE_GLOBAL,
                'visible' => true,
                'required' => false,
                'user_defined' => false,
                'default' => '',
                'searchable' => false,
                'filterable' => false,
                'comparable' => false,
                'is_used_in_grid' => true,
                'visible_on_front' => false,
                'used_in_product_listing' => false,
                'unique' => false
            ]
        );

        
    }
} 

Update:-

place this code inside you dafault.xml in custom module

to remove "More Information" tab:

<referenceBlock name="product.attributes" remove="true" />

If you want to remove the "Details" tab:

<referenceBlock name="product.info.details" remove="true" />

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.