cancel
Showing results for 
Search instead for 
Did you mean: 

How to update product store_view attributes (name, description) programmatically?

How to update product store_view attributes (name, description) programmatically?

There is possible to have different product name and description values because these attributes have store_view scope.
Please take a look at screenshot

product store_viewproduct store_view

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

To update product programmatically it should be something like (please ignore wrong object injection style):

$sku = 'sku1';
$storeId = 0;
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$_product = $obj->create('\Magento\Catalog\Api\ProductRepositoryInterface')->get($sku,true, $storeId, true);
$_product->setPrice(100);
$_product->setShortDescription('short description');
// $_product->setCustomAttribute("abc", 'abc');
$_product->save($_product);

But in that case product short_description attribute will be updated in all scopes.
How to update product in selected store_view scope?

3 REPLIES 3

Re: How to update product store_view attributes (name, description) programmatically?

Hello @dmitrij_rebrov 

 

Check the below solution:

<?php

 

namespace [Vendor]\[Module]\Helper;

 

use Magento\Framework\App\Helper\AbstractHelper;
use Magento\Framework\App\Helper\Context;
use Magento\Catalog\Model\ProductFactory;
use Magento\Catalog\Model\ResourceModel\Product;

 

class Data extends AbstractHelper
{
    protected $productFactory;
    protected $productResourceModel;

 

    public function __construct(
        Context $context,
        ProductFactory $productFactory,
        Product $productResourceModel
    )
    {
        $this->productFactory = $productFactory;
        $this->productResourceModel = $productResourceModel;
        parent::__construct($context);
    }
    
    public function setStoreViewValueProduct($productId, $storeId, $name, $desc)
    {
        $product = $this->productFactory->create();
        $this->productResourceModel->load($product, $productId);
        $product->setStoreId($storeId);
        $product->setName($name);
        $product->setDescription($desc);
        $this->productResourceModel->saveAttribute($product, $name);
        $this->productResourceModel->saveAttribute($product, $desc);
    }
}

 

/** @var $helper [Vendor]\[Module]\Helper */
now simply call helper function like : $helper->setStoreViewValueProduct(11, 2, 'Storeview Product 2', 'description storeview 2');

I hope it helps.

Problem solved? Click Kudos and "Accept as Solution".
200+ Magento 2 Extensions for Enhanced Shopping Experience.

Re: How to update product store_view attributes (name, description) programmatically?

Your code gives and error:

 

PHP Fatal error:  Uncaught Error: Call to a member function getBackend() on bool in /mnt/c/Development/fruits.loc/vendor/magento/module-eav/Model/Entity
/AbstractEntity.php:1666
Stack trace:
#0 /mnt/c/Development/fruits.loc/generated/code/Magento/Catalog/Model/ResourceModel/Product/Interceptor.php(661): Magento\Eav\Model\Entity\AbstractEntit
y->saveAttribute()
#1 /mnt/c/Development/fruits.loc/app/code/Funami/MadImport/Helper/updateProductHelper.php(38): Magento\Catalog\Model\ResourceModel\Product\Interceptor->
saveAttribute()
#2 /mnt/c/Development/fruits.loc/app/code/Funami/MadImport/Console/Command/XmlCheck.php(102): Funami\MadImport\Helper\updateProductHelper->setStoreViewV
alueProduct()
#3 /mnt/c/Development/fruits.loc/vendor/symfony/console/Command/Command.php(255): Funami\MadImport\Console\Command\XmlCheck->execute()
#4 /mnt/c/Development/fruits.loc/vendor/magento/framework/Interception/Interceptor.php(58): Symfony\Component\Console\Command\Command->run()
#5 /mnt/c/Development/fruits.loc/vendor/magento/framework/Interception/ in /mnt/c/Development/fruits.loc/vendor/magento/module-eav/Model/Entity/Abstract
Entity.php on line 1666

 

 

I've cleared cache and deleted generated/code directory before execution.

Also method: 

setSDescription

does not exist in \Magento\Catalog\Model\Product class.

Re: How to update product store_view attributes (name, description) programmatically?

$storeId = $this->storeRepository->get($storeCode)->getId();
$product = $this->productRepository->get(
'your_sku',
false,
$storeId,
true
);
$product->addAttributeUpdate(
ProductAttributeInterface::CODE_NAME,
'your product name',
$storeId
);
$product->addAttributeUpdate(
ProductAttributeInterface::CODE_SHORT_DESCRIPTION,
'your product short description',
$storeId
);
$product->addAttributeUpdate(
ProductAttributeInterface::CODE_PRICE,
9.99,
$storeId
);

 see method \Magento\Catalog\Model\Product::addAttributeUpdate

/**
     * Save current attribute with code $code and assign new value.
     *
     * @param string $code Attribute code
     * @param mixed $value New attribute value
     * @param int $store Store ID
     * @return void
     */
    public function addAttributeUpdate($code, $value, $store)
    {
        $oldValue = $this->getData($code);
        $oldStore = $this->getStoreId();

        $this->setData($code, $value);
        $this->setStoreId($store);
        $this->getResource()->saveAttribute($this, $code);

        $this->setData($code, $oldValue);
        $this->setStoreId($oldStore);
    }