如何通过ID获取自定义类别属性:Magento 2



我有类别ID,需要获取所有自定义属性,例如缩略图。

我的代码不返回所有属性

$category = $this->categoryRepository->get($childId, $this->_storeManager->getStore()->getId());
$category->getData();

您可以使用类别的CollectionFactory类,并在addAttributeToSelect方法中使用star (*)符号选择所有属性。您可以在下面的类中使用此代码示例。

protected $_categoryFactory;
public function __construct(
// ...
MagentoCatalogModelResourceModelCategoryCollectionFactory $collecionFactory,
) {
// ...
$this->_categoryFactory = $collecionFactory;
}
public function yourFunctionName()
{
$catId = 3; // your category id        
$collection = $this->_categoryFactory
->create()
->addAttributeToSelect('*')
->addAttributeToFilter('entity_id',['eq'=>$catId])
->setPageSize(1);
$catObj = $collection->getFirstItem();
$thumbnail = $catObj->getThumbnail(); // it should return value if attribute name is thumbnail
$catData = $catObj->getData(); // dump this line to check all data
// ...
}

尝试以下代码:

$categoryId = 5;
$_objectManager = MagentoFrameworkAppObjectManager::getInstance();
$object_manager = $_objectManager->create('MagentoCatalogModelCategory')->load($categoryId);
echo "<pre>";
print_r($object_manager->getData());
die('shasha test');

我希望它会有所帮助!!

最新更新