Ciao sono nuovo di Magento2
Voglio ottenere il nome della categoria dall'ID della categoria specificata Qualcuno può aiutare?
Grazie in anticipo
risposte:
7 per risposta № 1Prova a seguire il codice per ottenere l'oggetto categoria in Magento2:
$categoryId = 3;
$_objectManager = MagentoFrameworkAppObjectManager::getInstance();
$category = $_objectManager->create("MagentoCatalogModelCategory")
->load($categoryId);
echo $category->getName();
9 per risposta № 2
Non si dovrebbe mai usare il ObjectManager
.
È possibile inserire questo nel blocco e chiamare la funzione getCategoryName()
nel 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;
}
}