magento2 - 如何通过 id 获取自定义类别属性:Magento 2
问题描述
我有类别 ID,需要获取所有自定义属性,例如缩略图。
我的代码没有返回所有属性
$category = $this->categoryRepository->get($childId, $this->_storeManager->getStore()->getId());
$category->getData();
解决方案
您可以使用 Category 的类并通过在方法中CollectionFactory
使用star (*)
符号来选择所有属性。addAttributeToSelect
您可以在课堂上使用下面的代码示例。
protected $_categoryFactory;
public function __construct(
// ...
\Magento\Catalog\Model\ResourceModel\Category\CollectionFactory $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
// ...
}
推荐阅读
- ruby-on-rails - 关于 has_many 关联的 Searchkick 查询
- excel - 在 IF 语句中使用 Year(Date) 但没有输出
- c - 图像处理膨胀的C实现在结果图像上创建重复图案
- code-coverage - 如何在 Windows 机器上安装 lcov 工具
- python - 我可以在 kivy 和 python3 中使用 html&css 吗?
- elasticsearch - Elasticsearch:插入后数字被更改
- powershell - 使用 java processbuilder 打开 powershell 并保持打开状态
- dart - 根据条件添加小部件
- javascript - 使用排序时处理 strictNullChecks
- java - 运行正则表达式程序时遇到问题