39 votes
Accepted

Magento2 - Get category URL by ID

In order to get the category url you need to use the \Magento\Catalog\Model\Category function getUrl() like so: $category->getUrl() Also, you can get url by CategoryRepositoryInterface ...
Amit Bera's user avatar
  • 77.3k
21 votes
Accepted

Magento 2 Get category id using category title

You can do it via collections: First you need to inject a CategoryFactory in your class constructor. Magento 2.0 & 2.1: public function __construct( ... \Magento\Catalog\Model\...
Raphael at Digital Pianism's user avatar
16 votes
Accepted

Magento 2 create category attribute thumbnail and upload image using File Uploader Component

you need to add this in the di.xml of your module. <type name="Company\Categorylist\Controller\Adminhtml\Category\Thumbnailimage\Upload"> <arguments> <argument name="...
Marius's user avatar
  • 198k
13 votes
Accepted

How to create category attribute programmatically in Magento2

From Magento 2.1 and upper Version, You can also refer blog for create attribute programmatically in detais by, Create Custom category attribute in Magento 2, link to official docs You have to just ...
Rakesh Jesadiya's user avatar
13 votes

Magento2 - Get category URL by ID

Always try to use repository. You need to inject following way: /** * @var \Magento\Catalog\Helper\Category */ protected $categoryHelper; /** * @var \Magento\Catalog\Model\CategoryRepository */ ...
Sohel Rana's user avatar
  • 35.7k
10 votes
Accepted

How to add serializable attribute to category?

Unfortunately this doesn't work as easy as you thought (and I'd hoped). The first reason why this doesn't work is because you are extending a class that belongs to the system configuration part of the ...
Peter Jaap Blaakmeer's user avatar
10 votes

Create Custom category Yes/No attribute magento2

Add Following Code in ui_component xml file to Add Yes/No Toggle Option in Your admin Category Attribute. File Path :view/adminhtml/ui_component/category_form.xml <?xml version="1.0" ?> <...
Vinu's user avatar
  • 101
8 votes

Magento 2 How to get custom category attribute on frontend

Proper way is to in the Install/Upgrade script: $eavSetup->addAttribute( \Magento\Catalog\Model\Category::ENTITY, 'my_custom_attribute', [ // ... '...
OZZIE's user avatar
  • 552
8 votes

Magento2 - Get category URL by ID

You can try below code. $categoryId = 5; $_objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $object_manager = $_objectManager->create('Magento\Catalog\Model\Category')->load(...
Suresh Chikani's user avatar
7 votes

How to create category attribute programmatically in Magento2

As of magento 2.1 you will also need to create a UI components before the field will show up in admin. Create: app/code/Vendor/Categoryattr/view/adminhtml/ui_component/category_form.xml <?xml ...
MagePal Extensions's user avatar
6 votes

Magento 2 Get category id using category title

This can be done using service contracts which are considered as best practice. Edit: The original answer did not show the full paths to the included classes, I had to find the relevant namespaces, so ...
Yogesh Agarwal's user avatar
6 votes
Accepted

Magento 2.1: how to create category Attribute programmatically?

Magento2.1, all attribute load via UI_Component. So you need to assign as a form element following way: Create Vendor/Module/view/adminhtml/ui_component/category_form.xml <?xml version="1.0" ...
Sohel Rana's user avatar
  • 35.7k
6 votes

UpgradeSchema for category attribute M2

Try below code. You should created UpgradeData.php instead of creating UpgradeSchema.php. <?php namespace Hello\Catattribute\Setup; use Magento\Framework\Setup\UpgradeDataInterface; use Magento\...
Abhishek Panchal's user avatar
6 votes

How to create custom category attribute with wysiwyg editor in magento2?

Please follow the below steps to create a custom Category attribute and add it as WYSIWYG Editor in Category form. Step 1: Create custom attribute through Install or Upgrade Script. Below is an ...
Agnes's user avatar
  • 787
6 votes
Accepted

Set Use Default Value for all categories attributes Magento 2

For categories i ran the following query and it worked. DELETE FROM `catalog_category_entity_text` where store_id = 1; DELETE FROM `catalog_category_entity_datetime` where store_id = 1; DELETE FROM `...
Jaimin's user avatar
  • 2,146
5 votes
Accepted

Add checkbox field with catalog category

Use below code: <?php $installer = $this; $installer->startSetup(); $installer->addAttribute("catalog_category", "test", array( "type" => "int", "backend" => "", "...
Prashant Valanda's user avatar
5 votes
Accepted

create category programmatically if not exist - magento 2

We need to identify the id of category tree root. Then, we created an instance of the category, set its path, parent_id, name, etc. /** * Id of category tree root */ $parentId = \Magento\Catalog\...
Khoa TruongDinh's user avatar
5 votes

create category programmatically if not exist - magento 2

Consider using the Factory pattern for this instead of the object manager (unless you're putting this function in a factory class itself) class MyClass { protected $storeManager; protected ...
paul_c's user avatar
  • 51
5 votes

How add category programmatically magento 2

Further to Laor's answer, the following code works on Magento 2.1 and uses: \Magento\Catalog\Model\CategoryFactory for create \Magento\Catalog\Api\CategoryRepositoryInterface for saving <?php use ...
Jun Hsieh's user avatar
  • 571
5 votes
Accepted

Create multiselect with source model in UI component

Found it, thanks to GitHub issue #5438. The source model has to be specified in category_form.xml as well: <field name="solr_remove_filters"> <argument name="data" xsi:type="...
Fabian Schmengler's user avatar
5 votes
Accepted

How to use "button" in UI component form - Magento 2?

Okay I figured out a way to use a button element as a category attribute. What I've done is used the ElementTmpl property to render a category attribute as a Media browser. Let's get into the code: ...
Shathish's user avatar
  • 2,689
5 votes
Accepted

Custom category attribute is not showing in magento2

Setup/InstallData.php <?php namespace Vendor\Module\Setup; use Magento\Framework\Setup\InstallDataInterface; use Magento\Framework\Setup\ModuleContextInterface; use Magento\Framework\Setup\...
Hassan Ali Shahzad's user avatar
4 votes

Remove Custom Category From Database (Query)

Simply Run this Query:- For Disable the category from Database First Method For setting is_active = Yes UPDATE `catalog_category_entity_int` SET `value` = '1' WHERE `catalog_category_entity_int`.`...
Vishal Thakur's user avatar
4 votes
Accepted

Magento Category collection missing custom attributes

Its been a while since i have asked this and by now i know the answer of my own question. The answer is that Magento requires a default value of an attribute in the current collection to add the ...
Jerry's user avatar
  • 71
4 votes

Magento2 - Get category URL by ID

If you care about performance, this is a light Model that you can inject and use. <?php declare(strict_types=1); namespace ModuleNamespace\ModuleName\Model; use Magento\CatalogUrlRewrite\Model\...
Danilo Argentiero's user avatar
4 votes

Magento2: Category Select Attribute Not Saving?

Have you checked if your Custom Category Attribute has actually been added to the database because if it hasn't you should update your version number in the module.xml and run setup:upgrade Example ...
Mr. Lewis's user avatar
  • 1,921
4 votes

MAGENTO 2 How to Get Current page layout

I wanted to achieve the exact same thing. I ended up creating a helper class that injects both \Magento\Framework\View\Result\Page and \Magento\Framework\View\Layout. The Page object can retrieve the ...
Vernon Grant's user avatar
4 votes
Accepted

Custom image upload attribute for category not working in Magento 2.2.3

Create module using below code: create registration.php file at app/code/Prashant/CustomImage/registration.php <?php \Magento\Framework\Component\ComponentRegistrar::register( \Magento\...
Prashant Valanda's user avatar
4 votes
Accepted

Magento 2 : How to create custom Fieldset and Add custom attribute In It?

I think, this will help you. <?xml version="1.0" encoding="UTF-8"?> <form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:...
Amit Maurya's user avatar
  • 2,871
4 votes
Accepted

Magento2 : Add Category Attribute Dropdown

We have to customize convert the below functionality toOptionArray and add in Catalog Configuration. Vendor\Config\Model\Config\Source\ProductAttributes public function __construct( \Magento\...
Vishnunath's user avatar

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