Magento 2 Tutorial: How To Assign Product To Category Programmatically?

by admin

By default, Magento only allows manually adding and removing products to categories one by one.

The process is to go to the Category edit page, expand the Products in Category section, search for a product in your store, and choose to add or remove it from the category.

It may work fine if you handle a few products. But assigning a bulk of products is very time-consuming and tiresome.

That’s why we write this Magento 2 tutorial to show you how to assign products to category programmatically.

Let’s explore!

Magento 2 Set Product Position In Category Programmatically – Fastest & Easiest Way

Magento enables you to use custom codes to assign products to categories programmatically. However, that method is hard to follow and requires heavy coding knowledge

To make it easy for you, BSS offers you our new module:

Magento 2 Dynamic Category

This extension enables you to automatically associate products to categories according to your product conditions.

Using our Magento 2 Dynamic Category module, you can assign a mass number of products to categories in just a matter of minutes.

No more manual work, more time-saving!

Highlight features of Magento 2 Dynamic Category:

  • Mass assign products to a category
  • Manage products on catalog pages more systematically
  • Set conditions to run sales and seasonal campaigns
  • Tailor particular product listings for your purposes

Magento 2 Assign Product to Category Programmatically Tutorial

In this section, we will show you a comprehensive step-by-step guide on how to use Magento 2 Dynamic Category to assign product to category programmatically.

Step 1: Enable Magento 2 Dynamic Category

After install our module, please go to Stores Configuration Catalog BSS COMMERCEDynamic Category to enable it.

Step 2: Setup the rule for the dynamic category

Next, go to Catalog Categories. Choose an existing category or create a new one, then expand the Products in Category section.

Here you will find the Dynamic Category configuration.

In Dynamic Category, choose Yes to allow the module to assign the products for the category based on the conditions you set up below.

In case you want to import the rule from another dynamic category that you had set up in your store, you can select that category in Import Category Rules.

Or else, you can create a new set of product conditions for the category in Dynamic Category Rule.

Our module uses default Magento product attributes (including SKU, color, attribute, etc.) to set conditions for creating dynamic categories.

After setting up the dynamic category rule, click the Refresh matching products button to preview all products that meet the rule you set up in a product grid.

Then you click Save to save the dynamic category settings.

And that’s done!


Using BSS’s Magento 2 Dynamic Category module to assign products to Magento 2 categories programmatically is a great solution to enhance catalog management in your store.

We hope this article is helpful for you!


Next Reading Suggestions

© 2019 BSS Commerce owned by THANH CONG INTER ., JSC. All Rights Reserved.
Business registration certificate no. 0106064469 issued by Hanoi Department of Planning and Investment on 19 December 2019.
Legal Representative: Mr. Nguyen Quang Trung.