0

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

To update product programmatically code should be something like:

$sku = 'sku1';
$storeId = 0;
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$_product = $obj->create('\Magento\Catalog\Api\ProductRepositoryInterface')->get($sku,true, $storeId, true);

$_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?

1
  • 1
    change $storeId = 0; to $storeId = ...your store id here...;
    – Marius
    Commented Aug 19, 2020 at 14:42

4 Answers 4

2

Please try this below:

$sku = 'sku1';
$storeId = 0;
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$product = $objectManager->create('Magento\Catalog\Model\Product')->loadByAttribute('sku', $sku);

$attribute_code = 'short_description';
$value = 'short description';

$product->addAttributeUpdate($attribute_code, $value, $storeId);
1
  • This will update "store" scope, but not "store_view" scope. Commented Aug 21, 2020 at 7:27
1

You can also update a products attribute by scope if it's the default content, (name, sku, short description etc), using the product action class Magento\Catalog\Model\Product\Action

  use Magento\Catalog\Model\Product\Action as ProductAction;
  ...
  protected ProductAction $productAction;
  
  public function __construct(
    ProductAction $productAction
    ...
  ) {
    $this->productAction = $productAction;
  }

  /**
   * @param int $productId
   */
  public function updateData(int $productId)
  {
    $storeId = 4;
    $value = 'short description';
    $this->productAction->updateAttributes([$productId], ['short_description' => $value], $storeId);
  }
0

Either change the store ID like @Dimitri suggested, or switch to the csv uploader. I find that to be the easiest option for bulk updating attributes. All you need to use a CSV is 2 columns, one with the sku, and other with the attribute that you want to update

1
  • I aware of that CSV import possibility, but use custom product import and need some solution for multilanguage product import. Commented Aug 21, 2020 at 7:32
0

In case somebody will have same issue (please ignore incorrect object injection):

        $sku = '7550055';
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $_product = $obj->create('\Magento\Catalog\Api\ProductRepositoryInterface')->get($sku);
        $_product->setName('Russian name');
        $_product->setData('store_view_code', 'russian');
        $_product->setData('description', 'russian description');
        $_product->save($_product);

Take a note if product previously created without store_view_code field - attributes will be changed in all store_view scopes of current store.

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.