I have magento 2.3.4 and trying to append one word to all products. Is it possible to do using standard M2 means or phpmyadmin?
Correction, I only need to append names for items with certain attribute.
Magento Stack Exchange is a question and answer site for users of the Magento e-Commerce platform. It only takes a minute to sign up.
Sign up to join this communityI have magento 2.3.4 and trying to append one word to all products. Is it possible to do using standard M2 means or phpmyadmin?
Correction, I only need to append names for items with certain attribute.
You can mass / bulk update the collection of products in multiple ways as given below.
#1 Through Magento Product Import Export from the admin panel
At first export all the products
from your store, by using the export
option available under the admin menu System - Data Transfer - Export
Then edit the CSV file and update the name
column of all the product rows by appending a string/text of your choice to the name
column. (there are easy ways to do this in MS Excel)
keep only the sku
and name
columns and import the csv
to get the product names updated
For further reference about Magento2 product import and export, check these links below
https://docs.magento.com/user-guide/system/data-import.html
https://firebearstudio.com/blog/the-complete-guide-to-magento-2-product-import-export.html
https://www.mageplaza.com/kb/how-to-import-products-magento-2.html
#2 Update the product names through a Custom Script
its the quickest and get the job done in a few minutes
<?php
ini_set('display_errros',1);
ini_set('memory_limit',-1);
error_reporting(E_ALL);
require __DIR__ . '/app/bootstrap.php';
$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
$ObjectManager = $bootstrap->getObjectManager();
$state = $ObjectManager->get('\Magento\Framework\App\State');
$state->setAreaCode('frontend');
$attributeSetId = 184;
$_productCollection = $ObjectManager->create('\Magento\Catalog\Model\ResourceModel\Product\Collection')
->addAttributeToSelect(array('name'))
->addAttributeToFilter('attribute_set_id',$attributeSetId)
->load();
$_productAction = $ObjectManager->create('\Magento\Catalog\Model\Product\Action');
$storeId = 1; // change it your store id
$stringToAppend = 'TEST-';
try {
foreach ($_productCollection as $_product) {
$productName = $stringToAppend . $_product->getName();
echo 'Name: ' . $_product->getName() . PHP_EOL;
echo 'Name to be changed : ' . $productName . PHP_EOL;
//
$productIds = [];
$attributeData = [];
$attributeData['name'] = $productName;
$productIds[] = $_product->getId();
/* it is recommended to use updateAttributes in case of bulk product updates */
$_productAction->updateAttributes($productIds,$attributeData,$storeId);
/* You can also use save() which is slower than the above method */
// $_product->setName($productName)
// ->save();
}
} catch (Exception $ex) {
echo 'Product Save Exception : ' . $ex->getMessage() . PHP_EOL;
}
$_productCollectionAfterUpdate = $ObjectManager->create('\Magento\Catalog\Model\ResourceModel\Product\Collection')
->addAttributeToSelect(array('name'))
->addAttributeToFilter('attribute_set_id',$attributeSetId)
->load();
foreach ($_productCollectionAfterUpdate as $_product) {
echo 'Name after appending string : ' . $_product->getName() . PHP_EOL;
}
Disclaimer: Use of ObjectManager Directly is highly discouraged in a production environment. Inject the dependencies in the respective class instead.