37
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
...
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\...
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="...
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 ...
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
*/
...
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 ...
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" ?>
<...
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',
[
// ...
'...
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(...
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 ...
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 ...
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" ...
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\...
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 ...
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 `...
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\...
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 ...
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 ...
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" => "",
"...
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="...
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:
...
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\...
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`.`...
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 ...
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\...
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 ...
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 ...
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\...
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:...
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\...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
category-attribute × 226magento2 × 100
category × 68
attributes × 34
magento-1.9 × 32
custom-attributes × 17
magento-2.1 × 14
magento2.3 × 12
uicomponent × 10
product-attribute × 9
adminhtml × 9
eav × 9
category-tree × 8
magento-1.8 × 7
collection × 7
wysiwyg × 7
product × 6
magento-1.7 × 6
catalog × 6
category-products × 6
database × 5
module × 4
layout × 4
filter × 4
adminform × 4