0

Magenot 2 How to get Category attribute value label.

This code is magenot 1 and this code work to get Category attribute value label:-

<?php $title = Mage::getSingleton('categorylandingpage/entity_attribute_source_landingpageproducts')->getOptionText($mojoHelper->getCurrentCategory()->getCategoryLpageProducts()); ?>

I have Source Model File :-

<?php

namespace GetSomeMojo\CategoryLandingPage\Model\Entity\Attribute\Source;

use Magento\Framework\Option\ArrayInterface;

class Landingpageproducts extends \Magento\Eav\Model\Entity\Attribute\Source\Boolean implements ArrayInterface
{

    const VALUE_NO = 'lpage_no';
    const VALUE_NEW = 'lpage_new';
    const VALUE_FEATURED = 'lpage_featured';
    const VALUE_SALE = 'lpage_sale';

    protected $_options;

    public function getAllOptions()
    {
        return [
            ['value' => self::VALUE_NO, 'label' => __('No')],
            ['value' => self::VALUE_NEW, 'label' => __('New Products')],
            ['value' => self::VALUE_FEATURED, 'label' => __('Featured Products')],
            ['value' => self::VALUE_SALE, 'label' => __('Sale Products')]
        ];
    }

    public function getOptionArray()
    {
        $_options = array();
        foreach ($this->getAllOptions() as $option) {
            $_options[$option['value']] = $option['label'];
        }
        return $_options;
    }

    
    public function getOptionText($value)
    {
        $options = $this->getAllOptions();
        foreach ($options as $option) {
            if ($option['value'] == $value) {
                return $option['label'];
            }
        }
        return false;
    }
}

But I want to convert above code in magenot 2.

Please Advice me.

THANKS.

2
  • Do you want attribute value in phtml file in Magento 2? Oct 10 '20 at 6:40
  • No,i want to get attribute value label Oct 10 '20 at 6:53
1

Try to use below line in your phtml file, and replace category_lpage_products with your attribute code.

<?php
    $title = $this->helper('Magento\Catalog\Helper\Output')->categoryAttribute($block->getCurrentCategory(), $block->getCurrentCategory()->getCategoryLpageProducts(), 'category_lpage_products')
?>

Update

You can create one Helper file to get options like below

app/code/GetSomeMojo/CategoryLandingPage/Helper/Data.php

Content for above file is..

<?php

namespace GetSomeMojo\CategoryLandingPage\Helper;

use Magento\Framework\App\Helper\AbstractHelper;

class Data extends AbstractHelper
{
    protected $_categoryLandingPageProducts;

    public function __construct(
        \GetSomeMojo\CategoryLandingPage\Model\Entity\Attribute\Source\Landingpageproducts $categoryLandingPageProducts
    ) {
        $this->_categoryLandingPageProducts = $categoryLandingPageProducts;
    }

    public function getAttributeOptionText($attributeOptionId)
    {
        return $this->_categoryLandingPageProducts->getOptionText($attributeOptionId);
    }
}

Now you can get attribute value in your phtml file using below code

<?php
    $categoryAttributeOptionId = $block->getCurrentCategory()->getData('category_lpage_products');
    $value = $this->helper('GetSomeMojo\CategoryLandingPage\Helper\Data')->getAttributeOptionText($categoryAttributeOptionId);
?>

Hope this will help you!

13
  • i have try but above get attribute value not a label Oct 10 '20 at 6:52
  • Do you have multi-select attribute? or simple text type? Oct 10 '20 at 7:40
  • i have Drop-down attribute. Oct 10 '20 at 7:42
  • 1
    I have update my question Please check @Kishan Savaliya Oct 11 '20 at 5:41
  • 1
    Yes, It is Work for me. Thank you. Oct 12 '20 at 5:15

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.