2
  • I'm trying to programatically update configurable product name and keep other product data unchanged.
  • The store has 6 store views.
  • Each store view has unique name - all of them need changing.

I'm using this code:

...
$product = $this->productRepository->get('111111111');
$product->setName('New Name');
$this->productRepository->save($product);

It updates the name but also removes the product from other storeviews (keeps for the default store view only) and removes all product variations.

Questions:

  1. How do I update product name for each store view separately?
  2. Why Product Repository's save method removes stuff + how to prevent it?

THanks!

1

You can just try with below solutions,

public function __construct(\Magento\Catalog\Model\Product\Action $action,
\Magento\Store\Model\StoreManagerInterface $storeManager){
    $this->action = $action;
    $this->storeManager = $storeManager;
}

public function saveProductAttribute(){
    $id = '1'; //product id
    $websiteId = $this->storeManager->getWebsite()->getWebsiteId();
    $store = $this->storeManager->getStore(); 
    $storeId = $store->getStoreId();  // Get Store ID
    $this->action->updateAttributes([$id],['name' => 'test1'],$storeId);
}
  • can we update with product object? – jafar pinjar Dec 1 '18 at 12:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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