How to Programmatically Update Product Attribute Value Store Wise in Magento 2

Magento 2 allows multiple store creation, and sometimes, the store owners may require to set product attribute value store-wise. For example, if you are running your store in multiple languages, you may need to set product attributes in the respective language for different store views.

Magento 2 allows the store owners to manually set different product attribute values for different store views from the back-end. However, updating the product attribute value store-wise for all the products can be time-consuming and tedious.

Even it can get worse if you have thousands of products in your existing store-view. I have provided the complete solution to programmatically update product attribute value store wise in Magento 2.

We have provided complete code for Magento 2 add product attribute programmatically, so that you can save a lot of time focusing on productive tasks.

How to Programmatically Update Product Attribute Value Store Wise in Magento 2?

use Magento\Store\Model\StoreManagerInterface;
use Magento\Catalog\Model\ProductFactory;

protected $productFactroy;
protected $storeManager;

public function __construct(
StoreManagerInterface $storeManager,
ProductFactory $productFactory)
{
$this->productFactroy = $productFactory;
$this->storeManager = $storeManager;
}

$websiteId = 1;
$productId = 1;

$storeId = $this->storeManager->getWebsite($websiteId)->getDefaultStore()->getId();

$product = $this->productFactroy->create()->load(productId);
$product->addAttributeUpdate("name", "Product Name", $storeId);

That’s it.

Also, don’t forget to share the solution with your Magento community via social media.

Table of Contents