2

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.

9
  • why you do not extension attribute?
    – Amit Bera
    Commented Apr 8, 2019 at 10:20
  • Can you please elobarate more? Commented Apr 8, 2019 at 10:27
  • seems Magento\Catalog\Model\CatalogFactory wrong ` it should be Magento\Catalog\Model\CatagoryFactory
    – Amit Bera
    Commented Apr 8, 2019 at 10:27
  • Sorry. It's my mistake. It's typo Commented Apr 8, 2019 at 10:28
  • In v2.3.1, this value not display. I migrate data from 1.9.2.1 to 2.3.1 Commented Apr 8, 2019 at 10:29

3 Answers 3

0

I think you should get data like this,

$category->getData('attribute_code');

 OR

$category->getAttributeCode();
8
  • Not working,... Commented Apr 8, 2019 at 10:36
  • Is the attributed created in eav_attribute table?
    – Avesh Naik
    Commented Apr 8, 2019 at 10:38
  • Yes............. Commented Apr 8, 2019 at 10:40
  • If still not work run reindex. @EmiproTechnologiesPvt.Ltd. Commented Apr 8, 2019 at 10:45
  • Try this $atts= $category->getAttributes(); var_dump($atts);
    – Avesh Naik
    Commented Apr 8, 2019 at 10:56
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) {

}
6
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!

1
  • 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? Commented Apr 9, 2019 at 5:18

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.