47 votes
Accepted

Magento 2 get base url (current store) in admin static block/page?

You can add current store URL in static block. {{store url=""}} If you wanto add store url with category URL. {{store url="category-url"}}
user avatar
41 votes
Accepted

Magento 2 : How to check if we are on a category page or product page?

You can try below code it might help you. Inject an instance of \Magento\Framework\App\Request\Http in your class constructor. If you are in a controller you don't need to do it. You can already ...
user avatar
  • 3,446
30 votes
Accepted

Magento 2.x Elasticsearch Category Products Not Showing

I located an exception in ./magento/var/log/exception.log that showed an issue with the configuration of a custom attribute. The attribute was set to a type of text for the attribute custom_attribute ...
user avatar
29 votes

How to get first item from product collection

To get the first item in a collection simply use the getFirstItem() function on the collection. Example: // this gets all the products $productCollection = Mage::getResourceModel('catalog/...
user avatar
  • 2,876
25 votes

Magento 2 get current category Id using registry

If you need the category Id in Magento2 ,then you can get the values using the following steps to be followed 1.Include the use Magento\Framework\Registry in your class file. <?php /** * class ...
user avatar
  • 1,222
22 votes

Magento 2: How to delete all categories at once?

This one worked for me in Magento ver. 2.1.0 SET FOREIGN_KEY_CHECKS = 0; TRUNCATE TABLE catalog_category_entity; TRUNCATE TABLE catalog_category_entity_datetime; TRUNCATE TABLE ...
user avatar
22 votes

Assign products to category programatically in magento2

I think, this needs an updated answer which does not make use of the object manager. Also, there are some kinks involved which are not mentioned anywhere. In your constructor, inject the ...
user avatar
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\...
user avatar
21 votes

How to get Category Collection per Store in Magento 2?

Use directly objectmanager is not best/Recommended way to do in magento use Block with consturct and fetch method in your phtml file. $categoryFactory = $objectManager->create('Magento\Catalog\...
user avatar
20 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 $...
user avatar
  • 5,244
19 votes

Magento 2 - Category list for custom magento system configuration section

VendorName/ModuleName/etc/adminhtml/system.xml <?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:...
user avatar
  • 1,491
18 votes
Accepted

Get full list of categories using sql

Using @mpaepper answer I was able to get a query that seems to be pulling the correct information back. I was able to test it on two different magento databases and each looked right. Anyway here it ...
user avatar
  • 2,154
18 votes
Accepted

Shop by category is missing when going to subcategory page

Layout wise, magento distinguishes categories as default categories and layered categories. Magento uses is anchor option in order to make this categorization. If is anchor is set to yes, then that ...
user avatar
18 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( ... \...
user avatar
  • 195k
17 votes
Accepted

Assign products to category programatically in magento2

You need to get category Ids and product Id to set data: impliment this : $this->getCategoryLinkManagement()->assignProductToCategories( $product->getSku(), $...
user avatar
15 votes

Get full list of categories using sql

Ok, so here are the tables and what you will need to do (I will let you do the MySQL join fun for yourself ;)): catalog_category_entity is the base table which has your category ID (entity_id). You ...
user avatar
  • 4,660
15 votes
Accepted

Magento 2 - Remove Category Image on top of Category Page

Take a look these files: - The layout vendor/magento/module-catalog/view/frontend/layout/catalog_category_view.xml - Template vendor/magento/module-catalog/view/frontend/templates/category/image....
user avatar
14 votes

Get custom category attribute

Try something like that: $value = Mage::getModel('catalog/category')->load($category->getId())->getData('catalog_pdf'); EDIT: Better way (without loading whole category model): Mage::...
user avatar
  • 616
14 votes

Hide or remove "category" filter from the layered navigation

Based on stackexchange.com answer: If you want to do it via XML you should edit catalog.xml replace: <block type="catalog/layer_view" name="catalog.leftnav" after="currency" template="...
user avatar
  • 6,490
14 votes
Accepted

How to retrieve the Category name, Category URL and Category image using Category-Id?

Ravi Chan,you can only do this Magento Category Model Call. $Category=Mage::getModel('catalog/category')->load($cat); $name=$Category->getName(); $url=$Category->getUrl(); $image=$Category-&...
user avatar
  • 75.6k
14 votes
Accepted

In which database table are products and categories coupled

product and category relation has been saved at table catalog_category_product and catalog_category_product_index.catalog_category_product is the main table
user avatar
  • 75.6k
14 votes
Accepted

How to redirect a category to another link?

Simply go to catalog > URL rewrite management and choose "add rewrite" then choose your "custom" from dropdown. In Request path section enter your category link (ex. if url is www.example.com/...
user avatar
14 votes

Move Category Description to the bottom after product list Magento 2.0.4

In your theme folder, add a catalog_category_view.xml layout under Magento_Catalog/layout (the complete path should be app/design/frontend/[Company]/[theme_name]/Magento_Catalog/layout/...
user avatar
  • 3,368
13 votes
Accepted

Get Categories and Its Sub Categories

<?php $_helper = Mage::helper('catalog/category'); $_categories = $_helper->getStoreCategories(); if (count($_categories) > 0){ foreach($_categories as $_category){ $_category = ...
user avatar
  • 574
12 votes
Accepted

Check if product belongs to category?

$categoryIds = $product->getCategoryIds(); $isInCategory = in_array($category, $categoryIds); If the $category variable is an object then do this: $isInCategory = in_array($category->getId(), $...
user avatar
  • 195k
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 $...
user avatar
  • 34.3k
12 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" /> </...
user avatar
11 votes

How to get list of all subcategories which are set “Include in Navigation Menu” as no

Try this: $collection = Mage::getResourceModel('catalog/category_collection') ->addAttributeToSelect('*') ->addAttributeToFilter('is_active', 1) //only active categories ->...
user avatar
  • 195k
11 votes
Accepted

applying layered navigation to CMS page filled with new products

Layered navigation needs an active category to be able to work. If I remember correctly this should do it. <block type="catalog/layer_view" name="catalog.leftnav" before="-" template="catalog/...
user avatar
  • 37.4k
11 votes
Accepted

update category attributes programmatically

If you have many categories and want to update few attributes than you can use code similar to this below. It's fast and saves only values for these attributes which you want to update. You can run ...
user avatar
  • 349

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