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

  • Can do with Script or in DB?
    – Jackson
    Feb 8 '17 at 9:17
  • Yes I am thinking with script only but no idea how to do in magento2
    – Keith
    Feb 8 '17 at 10:11

Below is the script to import the CSV of category:

      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');

      $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);
      $csv=array(); //csv array with key as Category Id
      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()
                  ->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

         $metatitle= $csv[$id]['meta_title'];
         $metadesc= $csv[$id]['meta_description'];
         $metakeyword= $csv[$id]['meta_keyword'];




      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       

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.