Magento 2 श्रेणी शीर्षक का उपयोग कर श्रेणी आईडी प्राप्त करें


11

मैं इस प्रकार के फंक्शन का उपयोग करके केवल श्रेणी शीर्षक का उपयोग करके श्रेणी आईडी प्राप्त करना चाहूंगा।

->load($categoryTitle, 'title')
->getId();

मामले का उपयोग करें: श्रेणी आईडी शीर्षक से प्राप्त करें और माइग्रेशन स्क्रिप्ट में सरणी के लिए आईडी डेटा डालें।

जवाबों:


18

आप इसे संग्रह के माध्यम से कर सकते हैं:

सबसे पहले आपको CategoryFactoryअपने क्लास कंस्ट्रक्टर में एक इंजेक्ट करना होगा ।

Magento 2.0 और 2.1:

public function __construct(
    ...
    \Magento\Catalog\Model\CategoryFactory $categoryFactory
) {
    $this->_categoryFactory = $categoryFactory;
    parent::__construct(...);
}

फिर अपनी कक्षा में कहीं भी आप कर सकते हैं:

$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(...);
}

फिर अपनी कक्षा में कहीं भी आप कर सकते हैं:

$collection = $this->collecionFactory
                ->create()
                ->addAttributeToFilter('name',$categoryTitle)
                ->setPageSize(1);

if ($collection->getSize()) {
    $categoryId = $collection->getFirstItem()->getId();
}

एक त्रुटि हो गई PHP घातक त्रुटि: अपरिभाषित विधि पर कॉल करें Magento \ कैटालॉग \ मॉडल \ श्रेणीफैक्टरी :: getCollection ()
— kg

1
@ किलिस मेरा अपडेट देखें, आपको उस वर्ग को इंजेक्ट करने के लिए DI का उपयोग करने की आवश्यकता है;)
— डिजिटल पियानोवाद पर राफेल

मेरे उपयोग के मामले के लिए मुझे इस तरह के फ़ंक्शन $ संग्रह = $ इस की आवश्यकता है -> _ objectManager-> create ('\ Magento \ कैटलॉग \ Model \ CategoryFactory') -> getCollection () -> addAttributeToililter ('शीर्षक', $ categoryTitle) - > setPageSize (1);
— किलीस

1
@kilis अच्छी तरह से सीधे वस्तु प्रबंधक का उपयोग करने के लिए यह बुरा अभ्यास है, आपको हमेशा निर्भरता इंजेक्शन का उपयोग करना चाहिए
— राफेल डिजिटल पियानिज़्म में

हाँ मुझे पता हे। हमारा प्रोजेक्ट अपग्रेड स्क्रिप्ट इस तरह डिज़ाइन किया गया है: /
— kg

4

यह सेवा अनुबंधों का उपयोग करके किया जा सकता है जिन्हें सबसे अच्छा अभ्यास माना जाता है।

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;
    }

+1 सर्वश्रेष्ठ अभ्यास भाग के लिए
— अक़ीफ़

3

आप इसे प्रयोग करके सरल कर सकते हैं name,

$title = 'womens';
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$collection = $_categoryFactory->create()->getCollection()->addFieldToFilter('name',$title);
echo "<pre>";
print_r($collection->getData());
exit;

FE उपयोग के लिए नहीं, आवश्यक स्क्रिप्ट कार्यक्षमता को अपग्रेड करें।
— किसी

आपने सिर्फ शीर्षक कहा है, मैंने अपना उत्तर अपडेट कर दिया है।
— राकेश जेसादिया

2

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();
}

1

मुझे अपने कोलाज से मदद मिली

$this->_objectManager->get('Magento\Catalog\Model\CategoryFactory')->create()->getCollection()
        ->addFieldToSelect('name')
        ->addFieldToFilter('name', ['in' => $categoryTitle]);

:) चूंकि संग्रह केवल उस रिकॉर्ड को वापस करेगा जिसे आप चाहते हैं कि आप ->getFirstItem()उपरोक्त कोड पर एकमात्र परिणाम ले सकते हैं


0

एक कार्यशील लिपि में परावर्तन करने के लिए मैं निम्नलिखित का उपयोग करने का सुझाव देता हूं

$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();
}

संपादित करें: मैंने एक स्क्रिप्ट बनाई और परीक्षण किया। मैंने /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; 
}

अपना कोड आज़माया, लेकिन पहली पंक्ति को $ obj = $ इस में बदल दिया -> _ objectManager; एक [Magento \ फ्रेमवर्क \ अपवाद \ LocalizedException] मिला अमान्य विशेषता का नाम: शीर्षक त्रुटि
— किलो

जब आपको वह त्रुटि मिली तो आपने मेरी स्क्रिप्ट का उपयोग नहीं किया।
— काय इंट वीन

मैंने अभी पूरी तरह से परीक्षित स्क्रिप्ट जोड़ी है। कृपया जाँच करें। यह निश्चित रूप से काम करेगा!
— काय इंट वीन

0

मैं अपनी खुद की (अधिक कुशल) विधि लिखने में कामयाब रहा:

$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;
}

यह कोड सभी शीर्षक को कैश करता है: एक सरणी में आईडी, और केवल 2 बार क्वेरी करता है।

मेरे लिए काम किया। उपयोग करने में आसान!


0

सबसे पहले, आपको संग्रह कारखाना वर्ग को इंजेक्ट करने की आवश्यकता है

public function __construct(
    ...
    \Magento\Catalog\Model\ResourceModel\Category\CollectionFactory $collecionFactory ) {
    $this->_collectionFactory = $collecionFactory;
    parent::__construct(...); }

उसके बाद अपनी विधि के अंदर, आप यह कर सकते हैं,

$categoryTitle = 'Men';
$collection = $this->_categoryCollectionFactory->create()->addAttributeToFilter('name',$categoryTitle)->setPageSize(1);

if ($collection->getSize()) {
    $categoryId = $collection->getFirstItem()->getId();
}
हमारी साइट का प्रयोग करके, आप स्वीकार करते हैं कि आपने हमारी Cookie Policy और निजता नीति को पढ़ और समझा लिया है।
Licensed under cc by-sa 3.0 with attribution required.