22 votes

Magento 2 - Get Subcategories of Specific Parent Category

Check below example to list of all subcategories of specific parent category using parent category ID using the repository. First of all add CategoryRepository in construct: <?php protected $...
Aasim Goriya's user avatar
  • 5,424
19 votes

Magento 2 - Get Subcategories of Specific Parent Category

You need to add a dependency to your class \Magento\Catalog\Model\ResourceModel\Category\CollectionFactory. Like this: protected $categoryCollectionFactory; public function __construct( ... \...
Marius's user avatar
  • 198k
13 votes

Magento 2 : How to display the category image in the topmenu

Look at vendor/magento/module-catalog/etc/di.xml <type name="Magento\Theme\Block\Html\Topmenu"> <plugin name="catalogTopmenu" type="Magento\Catalog\Plugin\Block\Topmenu" /> </...
Khoa TruongDinh's user avatar
12 votes

Magento 2 - Get Subcategories of Specific Parent Category

Always try to use repository. Here is an example. Inject CategoryRepository by construct protected $categoryRepository; public function __construct( \Magento\Catalog\Model\CategoryRepository $...
Sohel Rana's user avatar
  • 35.7k
11 votes
Accepted

Magento 2 REST API returns only default category and its children

Well you can easily get all your categories by passing "all" keyword in your categories get API URL It will give you all the categories including all your root categories. Try below Steps: Your Api ...
Manthan Dave's user avatar
  • 9,806
9 votes
Accepted

How to get category tree in Magento 2?

I searched over internet for solution to this but got nothing useful. Then I diverted my research towards Magento core where I found \Magento\Catalog\Block\Adminhtml\Category\Tree class where I found ...
P S's user avatar
  • 1,983
7 votes
Accepted

Magento 2 : Selected categories are not visible in edit form

In that case you need to modify getData function. For edit category_ids value must be an array.Suppose category_ids value save in db as comma separated that means '7,9,22'. So when you edit this ...
Sohel Rana's user avatar
  • 35.7k
7 votes
Accepted

How to add multi-select category tree at admin end in magento 2?

please follow the below logic to get the category collection as tree structure. please let me know if u need any help. <?php namespace Hello\ConfigurableSku\Model\Config\Source; use Magento\...
Kavya cheerala's user avatar
6 votes
Accepted

Magento 1.9.2.4 - SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '0' for key 'PRIMARY' while saving categories

You have an issue in magento log tables. first Take backup of below tables log_customer log_visitor log_visitor_info log_url log_url_info log_quote Then truncate the tables. This will resolved ...
Amit Bera's user avatar
  • 77.3k
6 votes
Accepted

Magento 2 Add category chooser without UI component

I have solved the issue by following codes: Adding field by following code: $fieldset->addField( 'category_ids', '\Vendor\Module\Block\Adminhtml\Chooser', [ 'name' => '...
Sukumar Gorai's user avatar
5 votes

Get child categories tree when Flat Catalog Category is enabled in admin in Magento 2

You can use getChildren to get child category data in your block even Use Flat Catalog Category is enabled. For example namespace Custom\Module\Block\World; class Index extends \Magento\Framework\...
Krishna ijjada's user avatar
5 votes
Accepted

Magento 2 : How to add category selector on admin form?

If you just want to add tree input renderer you can add this code in your ui component form xml <field name="parent"> <argument name="data" xsi:type="array"> <item name="...
Priyank's user avatar
  • 7,672
5 votes

Magento 2 : Show admin category tree on frontend

1) go to "app" from root directory of Magento 2 and create new directory code. Then create two more directories in app/code, Namespace and Module Name. The final directory will look like this: app/...
Nilesh Gosai's user avatar
  • 1,125
4 votes
Accepted

Magento 2 - top menu show from 2nd sub category

Let's create a new module. We'll call it StackExchange_Topmenu. you will need the following files. app/code/StackExchange/Topmenu/registration.php - the registration file <?php \Magento\...
Marius's user avatar
  • 198k
4 votes
Accepted

Add custom css class to each item in Topmenu - Magento 2

Try following code: app/code/[VendorName]/[ModuleName]/registration.php \Magento\Framework\Component\ComponentRegistrar::register( \Magento\Framework\Component\ComponentRegistrar::MODULE,...
Pratik Oza's user avatar
  • 3,972
4 votes
Accepted

Magento 2.3 sort categories alphabetically

In your module di.xml (app\code\vender\module\etc\di.xml) place this line <preference for="Magento\Catalog\Model\ResourceModel\Category" type="Vendor\Module\Model\Category"/> ...
Joao71's user avatar
  • 1,187
3 votes

Magento 2 - Get Subcategories of Specific Parent Category

Use below code to get all active child categories of a specific category. Function getChildCategories($categoryId) gives all child categories. Where $categoryId - is parent category id <?php ...
Pandurang Babar's user avatar
3 votes

Multi vendor + add product - force to choose only 1 category

Yes, you’re on the right track, you can add Java Script and restrict the user to one category. After that you need to apply restriction in product save function in controller or model.
Altamash Khan's user avatar
3 votes
Accepted

How to add this category in marketing menu?

In your extension you have to create a new file menu.xml Foo/Bar/etc/adminhtml/menu.xml <?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:...
Matthéo Geoffray's user avatar
3 votes
Accepted

Get products directly assigned to a category

This is how is_anchor works in Magento. I had problems understanding the reason also. I still do, but I don't care anymore. is_anchor, as you already stated, serves 2 purposes. to include the ...
Marius's user avatar
  • 198k
3 votes

Issue - Multi-Store Category Disable Not Working

Well No one can answer this question until and unless they see your code. What logic theme/or menu extension has written for fetching categories. But I can give you a hint where you start debug this ...
Murtuza Zabuawala's user avatar
3 votes

creating categories and subcategories using the api catalogCategoryRepositoryV1

I created a category under deafult category using following request { "category": { "parent_id": 2, "name": "test", "is_active": true } } To create a sub category for your new category change ...
Sairam Sigirisetty's user avatar
3 votes
Accepted

Magento 2. Diffrent menu options for different store views

Just set the category-setting "show in navigation" to "yes" or "no" per store view depending on your needs.
simonthesorcerer's user avatar
3 votes
Accepted

Get all 3rd level categories

Note that there is no CategoryRepository, but there is a CategoryListInterface that you can use in conjunction with a SearchCriteriaInterface Something like this should work: /** @var \Magento\...
Jorge1o1's user avatar
  • 331
3 votes

Unlink and link product to category

Use below code to assign: $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $CategoryLinkRepository = $objectManager->get('\Magento\Catalog\Api\...
Mohit chauhan's user avatar
3 votes
Accepted

Magento 2 - How to get category name and category tree for product or for category id?

If you want to get a particular category tree you have to use below method Magento\Catalog\Model\ResourceModel\Category\Tree::loadBreadcrumbsArray($path, $addCollectionData = true, $withRootNode ...
Amit Bera's user avatar
  • 77.3k
3 votes

Magento 2 : Add custom array in multiselect UI dropdown in custom UI form

You can add below code for to add in your UI XML file: <field name="category_id"> <argument name="data" xsi:type="array"> <item name="options" xsi:type="object">...
Dasharath Patel's user avatar
3 votes

Magento 2.3 sort categories alphabetically

There are many solutions to this problem, Joao71's solution is simple to write and should do the trick. In terms of best practice, preference are to be used if plugin is not possible. The reason is ...
Herve Tribouilloy's user avatar
2 votes
Accepted

All the possible ways to export/import categories with ids

If you want to keep you 1.7.0.2 information you can do in a simple way: Dump you database from 1.7.0.2 Import your dump to the new database Point the 1.9.2.4 installation to the new database by ...
Phoenix128_RiccardoT's user avatar
2 votes

Display all the sub-categories in multiple <ul> tag

There are 2 ways to do it. You will need test where is your header navigation content coming from. For instance, in Magento 1.9.2.2 the content comes from: app/design/frontend/rwd/default/template/...
Shivani's user avatar
  • 521

Only top scored, non community-wiki answers of a minimum length are eligible