0

In Magento2 , I need to get eav attribute value in category data. I used this below code. But, it's not working.

/** \Magento\Catalog\Model\CategoryFactory $categoryFactory**/
$category = $this->categoryFactory->create()->load(12); //12 is category id
$category->getData('attribute-code');

This attribute code value available in catalog_category_entity_varchar table.

M1 Array :

Array
(
    [entity_id] => 12
    [entity_type_id] => 3
    [attribute_set_id] => 3
    [parent_id] => 3
    [created_at] => 2014-04-29T00:25:55+02:00
    [updated_at] => 2017-09-20 11:40:47
    [path] => 1/2/3/12
    [position] => 2
    [level] => 3
    [children_count] => 0
    [request_path] => requestpath
    [is_active] => 1
    [name] => name
    [url_key] => url_key
    [is_anchor] => 0
    [meta_title] => 
    [display_mode] => PRODUCTS
    [custom_design] => 
    [page_layout] => one_column
    [url_path] => url
    [style] => 
    [faq_list_position] => 1
    [include_in_menu] => 1
    [landing_page] => 
    [custom_use_parent_settings] => 0
    [custom_apply_to_products] => 0
    [description] => 
    [meta_keywords] => meta
    [meta_description] => meta1
    [custom_layout_update] => 
    [available_sort_by] => Array
        (
            [0] => position
        )

    [custom_design_from] => 
    [custom_design_to] => 
    [filter_price_range] => 
)

M2 Array :

Array
(
    [entity_id] => 12
    [attribute_set_id] => 46
    [parent_id] => 3
    [created_at] => 2014-04-28 22:25:55
    [updated_at] => 2019-04-05 10:23:19
    [path] => 1/2/3/12
    [position] => 2
    [level] => 3
    [children_count] => 0
    [name] => name
    [url_key] => urlkey
    [image] => 
    [meta_title] => title
    [display_mode] => PRODUCTS
    [url_path] => urlpath
    [custom_design] => 
    [page_layout] => 1column
    [is_active] => 1
    [include_in_menu] => 1
    [landing_page] => 
    [is_anchor] => 0
    [custom_use_parent_settings] => 0
    [custom_apply_to_products] => 0
    [description] => 
    [meta_keywords] => meta
    [meta_description] => meta1
    [custom_layout_update] => 
    [available_sort_by] => Array
        (
            [0] => position
        )

    [custom_design_from] => 
    [custom_design_to] => 
)

How i get value ?

Please help me.

Thanks.

0

I think you should get data like this,

$category->getData('attribute_code');

 OR

$category->getAttributeCode();
0

As this attribute are not magento default attribute and there are not setter and getter method are not defined at the data interface Magento\Catalog\Api\Data\CategoryInterface So, $category->getData('attribute-code'); you cannot get this attribute value.

If you want then you have to use $category->getCustomAttribute($attributeCode)

Like:

    $category = $this->categoryFactory->create();
    $category->load(12);
    $customAttributeValue = $category->getCustomAttribute($attributeCode);
   if(null !== $customAttributeValue){
       $myAttributeValue = $customAttributeValue->getValue();
   }

I do not support or use this type of work.If will be better to use Extension attribute using Service contact class Magento\Catalog\Api\CategoryRepositoryInterface

BUT, Extension attribute is not working for Category entity for the blog https://github.com/magento/magento2/issues/10847,So you can get getCustomAttribute or getCustomAttributes()

Forgetting the data, you can use service contact interface

$storeId = 1;
try{
    /// $this->categoryRepository is instean of Magento\Catalog\Api\CategoryRepositoryInterface
   $category = $this->categoryRepository->get(12, $storeId);
   $customAttributeValue = $category->getCustomAttribute($attributeCode);
   if (null !== $customAttributeValue) {
       $myAttributeValue = $customAttributeValue->getValue();
   }           
} catch (\Magento\Framework\Exception\NoSuchEntityException $ex) {

}
0

In front, You can get the value of Category attribute by load category object and call the function like below.

$getCategory = CATEGORY_OBJECT;
echo $getCategory->getCustomCategoryField();

Take reference from below link.

I hope it helps!

  • This is not working. You can check M1 and M2 array. There are entity_type_id is missing in M2 array. That's why i can't get attribute value or? – Emipro Technologies Pvt. Ltd. Apr 9 at 5:18

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.