0

I have try to get wysiwyg attribute value in frontend

this is my code :-

InstallData.php

<?php


namespace Attribute\Categorybanner\Setup;

use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;

class InstallData implements InstallDataInterface
{

    private $eavSetupFactory;

    /**
     * Constructor
     *
     * @param \Magento\Eav\Setup\EavSetupFactory $eavSetupFactory
     */
    public function __construct(EavSetupFactory $eavSetupFactory)
    {
        $this->eavSetupFactory = $eavSetupFactory;
    }

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

        $eavSetup->addAttribute(
            \Magento\Catalog\Model\Category::ENTITY,
            'cat_offer_info',
                [

                    'type' => 'text',
                    'label' => 'Category Extra Offer Infomation',
                    'input' => 'textarea',
                    'required' => false,
                    'sort_order' => 333,
                    'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE,
                    'wysiwyg_enabled' => true,
                    'is_html_allowed_on_front' => true,
                    'group' => 'General Information',
                    'used_in_product_listing' => true, // for category pages
                    'visible_on_front' => true, // for frontend??
                    'is_used_in_grid' => true, // for category pages
                    'is_visible_in_grid' => true // for category pages 

                ]
        );
    }
}

category_form.xml

<?xml version="1.0" encoding="UTF-8"?>
<!-- file: app/code/Atwix/CategoryAttribute/view/adminhtml/ui_component/category_form.xml -->
<form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
    <fieldset name="general">
        <field name="cat_offer_info">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="class" xsi:type="string">Magento\Catalog\Ui\Component\Category\Form\Element\Wysiwyg</item>
                <item name="formElement" xsi:type="string">wysiwyg</item>
            <item name="label" xsi:type="string" translate="true">Category Extra Offer Infomation</item>
                <item name="wysiwygConfigData" xsi:type="array">
                    <item name="settings" xsi:type="array">
                        <item name="theme_advanced_buttons1" xsi:type="string">bold,italic,|,justifyleft,justifycenter,justifyright,|,fontselect,fontsizeselect,|,forecolor,backcolor,|,link,unlink,image,|,bullist,numlist,|,code</item>
                        <item name="theme_advanced_buttons2" xsi:type="boolean">false</item>
                        <item name="theme_advanced_buttons3" xsi:type="boolean">false</item>
                        <item name="theme_advanced_buttons4" xsi:type="boolean">false</item>
                        <item name="theme_advanced_statusbar_location" xsi:type="boolean">false</item>
                    </item>
                    <item name="files_browser_window_url" xsi:type="boolean">false</item>
                    <item name="height" xsi:type="string">250px</item>
                    <item name="toggle_button" xsi:type="boolean">true</item>
                    <item name="add_variables" xsi:type="boolean">false</item>
                    <item name="add_widgets" xsi:type="boolean">true</item>
                    <item name="add_images" xsi:type="boolean">false</item>
                </item>
                <item name="template" xsi:type="string">ui/form/field</item>
                <item name="source" xsi:type="string">category</item>
                <item name="wysiwyg" xsi:type="boolean">true</item>
                <item name="dataScope" xsi:type="string">cat_offer_info</item>
                <item name="sortOrder" xsi:type="number">20</item>
                <item name="rows" xsi:type="number">8</item>
            </item>
        </argument>
    </field>
    </fieldset>
</form>

catalog_category_view.xml

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="2columns-left" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceContainer name="columns.top">
            <block class="Magento\Catalog\Block\Category\View" name="category.extra.offer.info" template="Attribute_Categorybanner::product/list.phtml" before="category.image"/>
        </referenceContainer>
    </body>
</page>

list.phtml

<?php if ($_bottomDescription = $block->getCurrentCategory()->getCatOfferInfo()): ?>
    <div class="category-bottom-description">
        <?= /* @escapeNotVerified */ $this->helper('Magento\Catalog\Helper\Output')->categoryAttribute($block->getCurrentCategory(), $_bottomDescription, 'cat_offer_info') ?>
    </div>
<?php endif; ?>

Please advice me what's wrong. Please suggest me.

THANKS.

1 Answer 1

0

put the below code in your list.phtml file,

<?php 
       $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
       $category = $objectManager->get('Magento\Framework\Registry')->registry('current_category');//get current category
       $customAttribute = $objectManager->get('Magento\Catalog\Model\CategoryRepository')->load($category->getId());
       $customAttributeVal= $this->helper('Magento\Catalog\Helper\Output')->categoryAttribute($category, $customAttribute->getCatOfferInfo(), 'cat_offer_info');
                                                

       echo $customAttributeVal;
       
    ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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