Jawaban:
Anda dapat melakukannya melalui koleksi:
Pertama, Anda perlu menyuntikkan CategoryFactory
konstruktor kelas Anda.
Magento 2.0 & 2.1:
public function __construct(
...
\Magento\Catalog\Model\CategoryFactory $categoryFactory
) {
$this->_categoryFactory = $categoryFactory;
parent::__construct(...);
}
Kemudian di tempat lain di kelas Anda yang dapat Anda lakukan:
$collection = $this->_categoryFactory->create()->getCollection()->addAttributeToFilter('name',$categoryTitle)->setPageSize(1);
if ($collection->getSize()) {
$categoryId = $collection->getFirstItem()->getId();
}
Magento 2.2:
public function __construct(
...
\Magento\Catalog\Model\ResourceModel\Category\CollectionFactory $collecionFactory
) {
$this->_collectionFactory = $collecionFactory;
parent::__construct(...);
}
Kemudian di tempat lain di kelas Anda yang dapat Anda lakukan:
$collection = $this->collecionFactory
->create()
->addAttributeToFilter('name',$categoryTitle)
->setPageSize(1);
if ($collection->getSize()) {
$categoryId = $collection->getFirstItem()->getId();
}
Ini dapat dilakukan dengan menggunakan kontrak layanan yang dianggap sebagai praktik terbaik.
protected $categoryList;
/**
* @var SearchCriteriaBuilder
*/
protected $searchCriteriaBuilder;
/**
* @var FilterBuilder
*/
protected $filterBuilder;
public function __construct(
------------
CategoryListInterface $categoryList,
SearchCriteriaBuilder $searchCriteriaBuilder,
FilterBuilder $filterBuilder,
-----------------
)
{
$this->categoryList = $categoryList;
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
$this->filterBuilder = $filterBuilder;
parent::__construct(----------);
}
public function getNameCategory()
{
$enableFilter[] = $this->filterBuilder
->setField(\Magento\Catalog\Model\Category::KEY_NAME)
->setConditionType('like')
->setValue(self::CATEGORY_NAME_HELP) // name of the categroy on const
->create();
$searchCriteria = $this->searchCriteriaBuilder
->addFilters($enableFilter)
->create();
$items = $this->categoryList->getList($searchCriteria)->getItems();
if(count($items) == 0)
{
return FALSE;
}
foreach ($items as $helpCategory)
{
$CategoryId = $helpCategory->getId()
}
return $CategoryId;
}
Anda dapat melakukannya dengan sederhana name
,
$title = 'womens';
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$collection = $_categoryFactory->create()->getCollection()->addFieldToFilter('name',$title);
echo "<pre>";
print_r($collection->getData());
exit;
Coba Kode Di Bawah Ini Untuk File Phtml:
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$_categoryFactory = $objectManager->get('Magento\Catalog\Model\CategoryFactory');
$categoryTitle = 'Outdoor'; // Category Name
$collection = $_categoryFactory->create()->getCollection()->addFieldToFilter('name', ['in' => $categoryTitle]);
if ($collection->getSize()) {
$categoryId = $collection->getFirstItem()->getId();
}
Saya mendapatkannya dengan bantuan dari kolase saya
$this->_objectManager->get('Magento\Catalog\Model\CategoryFactory')->create()->getCollection()
->addFieldToSelect('name')
->addFieldToFilter('name', ['in' => $categoryTitle]);
:) Karena koleksi hanya akan mengembalikan catatan yang Anda inginkan, Anda dapat mengambil satu-satunya hasil dengan ->getFirstItem()
pada kode di atas
Untuk memperbaiki itu dalam skrip yang berfungsi saya sarankan menggunakan yang berikut ini
$obj = $bootstrap->getObjectManager();
$_categoryFactory = $obj->get('Magento\Catalog\Model\CategoryFactory');
$collection = $_categoryFactory->create()->getCollection()->addAttributeToFilter('title',$categoryTitle)->setPageSize(1);
if ($collection->getSize()) {
$categoryId = $collection->getFirstItem()->getCategoryId();
}
Sunting: Saya membuat dan menguji sebuah skrip. Saya membuat file di /scripts/file.php
<?php
use Magento\Framework\App\Bootstrap;
require __DIR__ . '/../app/bootstrap.php';
$bootstrap = Bootstrap::create(BP, $_SERVER);
$obj = $bootstrap->getObjectManager();
// Set the state (not sure if this is neccessary)
$obj = $bootstrap->getObjectManager();
$_categoryFactory = $obj->get('Magento\Catalog\Model\CategoryFactory');
$categoryTitle = 'Test';
$collection = $_categoryFactory->create()->getCollection()->addAttributeToFilter('name',$categoryTitle)->setPageSize(1);
if ($collection->getSize()) {
$categoryId = $collection->getFirstItem()->getId();
echo $categoryId;
}
Saya berhasil menulis metode saya sendiri (lebih efisien):
$entityTypeId = \Magento\Catalog\Setup\CategorySetup::CATEGORY_ENTITY_TYPE_ID;
$row = $this->queryF("SELECT * FROM `eav_attribute` WHERE `entity_type_id` = $entityTypeId AND `attribute_code` = 'name'", 1);
$nameAttributeId = $row['attribute_id'];
$categoryNames = $this->queryF("SELECT * FROM `catalog_category_entity_varchar` WHERE `attribute_id` = '$nameAttributeId'");
$this->categoryNameIdMap = [];
foreach ($categoryNames as $item) {
$id = $item['entity_id'];
$title = $item['value'];
$this->categoryNameIdMap[$title] = $id;
}
Kode ini cache semua judul: id ke dalam array, dan hanya kueri 2 kali.
Bekerja untukku. Lebih mudah digunakan!
Pertama, Anda perlu menyuntikkan kelas koleksi pabrik
public function __construct(
...
\Magento\Catalog\Model\ResourceModel\Category\CollectionFactory $collecionFactory ) {
$this->_collectionFactory = $collecionFactory;
parent::__construct(...); }
Setelah itu di dalam metode Anda, Anda bisa melakukan ini,
$categoryTitle = 'Men';
$collection = $this->_categoryCollectionFactory->create()->addAttributeToFilter('name',$categoryTitle)->setPageSize(1);
if ($collection->getSize()) {
$categoryId = $collection->getFirstItem()->getId();
}