is their any way i can assign products to category using sql in program ? as category are too large request gets heavy so not able to save the traditional way.


category and Products are map on catalog_category_product table .

where are 4 columns like : entity_id,category_id,product_id and position.

where entity_id is auto increment.

so you can run this by sql -:

INSERT INTO `catalog_category_product`(`category_id`, `product_id`) VALUES ('category_id','product_id')

You can use it by simple corePHP program like

  1. Create A temporary table on your database having 4 column (id,category_id,product_id,flag)and stored data .
  2. Create a php file on root ,and Connect database.
  3. Select From this temporary table and insert into catalog_category_product table and if update success update flag .
  • Hello, how to use it in program ? – devhs Apr 20 at 4:31
  • I hope Now you better Understand it. – smruti Apr 20 at 8:48
  • Thankyou for the answer – devhs Apr 20 at 9:25

You can also assign products to category programmatically by using CategoryLinkManagementInterface By this way.

  * @var CategoryLinkManagementInterface
 protected $categoryLinkManagement;
 public function __construct(
     \Magento\Catalog\Api\CategoryLinkManagementInterface $categoryLinkManagementInterface
 ) {
     $this->categoryLinkManagement = $categoryLinkManagementInterface;
public function execute()

By Using object Manager

categoryLinkManagement = \Magento\Framework\App\ObjectManager::getInstance()

  • When there are more then 1000+ products to execute with this function it gets 504 not found error – devhs Apr 20 at 9:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.