Magento – magento2 bulk edit the meta-titles for the category pages

categorymagento2meta-title

I need to bulk edit category meta title in magento2.
Can anyone tell me the method or any way?

Best Answer

Below is the script to import the CSV of category:

<?php
      ini_set("memory_limit","-1");
      ini_set('max_execution_time', 300); 
      error_reporting(E_ALL | E_STRICT);
      ini_set('display_errors', 1);
      use Magento\Framework\App\Bootstrap;
      require str_replace(basename(__DIR__) . '/' . basename(__FILE__), '', realpath(__FILE__)) . '/app/bootstrap.php';

      $params = $_SERVER;

      $bootstrap = Bootstrap::create(BP, $params);

      $obj = $bootstrap->getObjectManager();

      $state = $obj->get('Magento\Framework\App\State');
      $state->setAreaCode('frontend');

      $resource = $obj->get('Magento\Framework\App\ResourceConnection');
      $connection = $resource->getConnection();

      $file='category_import.csv';// Name of CSV file

      $rows = array_map('str_getcsv', file($file));
      $header = array_shift($rows);
      $category_ids=array();
      $csv=array(); //csv array with key as Category Id
      $a=array();
      $i=0;
      foreach ($rows as $row) { 

          $csv[$row[0]] = array_combine($header, $row);
      }

      $category_ids=array_keys($csv); // All the Category Id which we want to update

      //************************ Load Categories which we want to Update ************************

      $categoryFactory = $obj->create('Magento\Catalog\Model\ResourceModel\Category\CollectionFactory');
      $categories = $categoryFactory->create()
                  ->addAttributeToSelect("*")
                  ->addAttributeToFilter('entity_id', array('in'=>$category_ids));

      $tableName = $resource->getTableName('catalog_category_entity_varchar');
      $tableDesc = $resource->getTableName('catalog_category_entity_text');

      foreach($categories as $category)
      {
          $id=$category->getId(); // Print name of categories ;just for our info

         $name=$csv[$id]['name'];
         $metatitle= $csv[$id]['meta_title'];
         $metadesc= $csv[$id]['meta_description'];
         $metakeyword= $csv[$id]['meta_keyword'];
         $urlkey=$csv[$id]['url-key'];
         $description=$csv[$id]['description'];


         $category->setName($name);
         $category->setMetaTitle($metatitle);
         $category->setMetaDescription($metadesc);
         $category->setMetaKeyword($metakeyword);
         $category->setUrlKey($urlkey);
         $category->setDescription($description);


          $category->save();

      }

      echo ' Categories Updated';

Save file in the root path and run it via SSH. CSV format must be as below :

    //************************ Import CSV File ************************
// Sample CSV Format
//category_id   meta_title      meta_description    meta_keywords
//1986          Test 123        test            test, test123           
//1904          Test 123        test            test, test123