2

I am using below code to update product by sku but that is updating price for english store only , i will price of product to get updated in all store view . getObjectManager();

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

$productCollectionFactory = $objectManager->get('\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory');
$collection = $productCollectionFactory->create();
$collection->addAttributeToFilter('sku', 'A960-CQ');
$collection->addAttributeToSelect('*');
foreach ($collection as $product) 
{
    $product->setData('name', 'test description');
    $product->save();
    echo "Product Updated". " ";
} 
10
  • what is the error you get?
    – fmsthird
    Feb 11, 2019 at 3:49
  • no error but record is not getting uupdated Feb 11, 2019 at 3:50
  • no error but record is not getting uupdated Feb 11, 2019 at 3:50
  • hi jjust noticed above code changes data of my english store view , how to make it work for all store view Feb 11, 2019 at 4:24
  • you have to get the store id and loop through it
    – fmsthird
    Feb 11, 2019 at 4:38

2 Answers 2

5

You can used below code to update product attributes for all store Added $ for obj

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$storeManager = $objectManager->create('\Magento\Store\Model\StoreManagerInterface');
$storeIds = array_keys($storeManager->getStores());
$action = $objectManager->create('\Magento\Catalog\Model\ResourceModel\Product\Action');
$updateAttributes['name'] = "test";
$updateAttributes['price'] = 100;
$productCollectionFactory = $objectManager->get('\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory');
$collection = $productCollectionFactory->create();
$collection->addAttributeToFilter('sku', 'A960-CQ');
$collection->addAttributeToSelect('*');
foreach ($collection as $product) 
{
    foreach ($storeIds as $storeId) {
        $action->updateAttributes([$product->getId()], $updateAttributes, $storeId);
    }
}

We can use “updateAttributes” method to update Specific Attribute for product instead update all the data

You can pass multiple attribute that you want to updates detail.As show price and name are pass in an array and updateAttributes will update the name and price of the product

1
  • Hi, I tried this solution but didn't work for me. I'm using magento version 2.3.3 Jul 27, 2020 at 8:34
1

You can use this code to update product data for all store

$sku = 'sku1';
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$_product = $obj->create('\Magento\Catalog\Api\ProductRepositoryInterface')->get($sku,true, 0, true);
$_product->setPrice(100);
$_product->setShortDescription('short description');
$_product->setCustomAttribute("abc", 'abc');
$_product->save($_product);
1
  • Note: Relying on the product model to save itself is a deprecated practice. Aug 25, 2022 at 16:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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