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?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | 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); |
Feel free to comment down in the comment section below in case of any doubts or queries. I would be happy to help.
Also, don’t forget to share the solution with your Magento community via social media.