首页 > 解决方案 > 如何在 Magento 2 中获取类别的自定义属性

问题描述

我正在尝试brand_label在 Magento2 中获得一个类别。这是我目前的代码:

$categoryObj = $this->categoryRepository->get($parent_category_id);
$Categories = $categoryObj->getChildrenCategories()->addAttributeToFilter('include_in_menu', array('eq' => 1));

$categoryArray = [];

foreach($Categories as $category){
    $categoryObject->brand_label = $category->getCustomAttribute('brand_label');
    array_push($categoryArray,$categoryObject); 
}

正在$categoryObject->brand_label返回 null

标签: magento2

解决方案


您可以使用以下方式从类别中检索数据:

$category = $this->categoryRepository->get($categoryId);
$category->getData('brand_label');

或者:

$category = $this->categoryRepository->get($categoryId);
$category->getBrandLabel();

确保您的类别属性代码是brand_label


推荐阅读