Здравствуйте, я новачок у magento2
Я хочу отримати назву категорії з вказаного ідентифікатора категорії Чи може хто-небудь допомогти?
Спасибі заздалегідь
Відповіді:
7 для відповіді № 1Спробуйте скористатися цим кодом для отримання об'єкта категорій у Magento2:
$categoryId = 3;
$_objectManager = MagentoFrameworkAppObjectManager::getInstance();
$category = $_objectManager->create("MagentoCatalogModelCategory")
->load($categoryId);
echo $category->getName();
9 для відповіді № 2
Ви ніколи не повинні використовувати ObjectManager
.
Ви можете помістити це в блок і викликати функцію getCategoryName()
у phtml:
namespace CompanyModuleBlock;
class CustomBlock extends MagentoFrameworkViewElementTemplate
{
protected $_categoryFactory;
public function __construct(
MagentoCatalogModelCategoryFactory $categoryFactory,
MagentoFrameworkViewElementTemplateContext $context,
) {
$this->_categoryFactory = $categoryFactory;
parent::__construct($context);
}
public function getCategoryName()
{
$categoryId = "43";
$category = $this->_categoryFactory->create()->load($categoryId);
$categoryName = $category->getName();
return $categoryName;
}
}