0

I'm trying to update a product using productRepository ( migrating from a flow using product factory with load ).

Problem is tha when updating the name it is updating in store 1 ( default store ) but not in store 0 ( admin store view )

Bellow there is a sample code

    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $productRepository = $objectManager->get('\Magento\Catalog\Api\ProductRepositoryInterface');
    $product = $productRepository->getById('product id here', true, 0);

    $product->setName('ts');
    $productRepository->save($product);

Newly created products have only one name in admin view , using the code above a new entry is added in catalog_product_entity_varchar table for store view 1 leaving store view 0 unchanged.

The only way to update the title seems using $product->save(). Is there any other way without using $product->save() ?

Is seems very strange to me, i'm running this code from a console command and i've already set the state to adminhtml

I'm running Magento 2.3.4

0

You can try to use:

\Magento\Catalog\Model\ResourceModel\Product\Action $productAction

for:

$this->productAction->updateAttributes(
    [$row->getProductId()], //array with product id's
    [
        $row->getAttributeCode() => $row->getAttributeValue(),
        $row->getOtherAttributeCode() => $row->getOtherAttributeValue()
    ],
    $storeId
);

But you dont have to load product from specific store.

$product = $productRepository->getById('product id here');

should be fine.

If You need to use ProductRepo, I think You can try to set storeId on product before save, or You will have to set storeId as current store

$this->_storeManager->setCurrentStore(0);

but its not good solution.

2
  • You are right, i can update specific attributes one by one, but there seems no way to save and update the product as a whole ( in admin store view). Such a shame, Magento doesn't provide a standard way of handling product data. – sarakinos Jun 13 '20 at 14:11
  • Oh If you want to save attribute for whole stores just use $product = $productRepository->getById('product id here');, then set attribute and save. should be fine. However! If on product on specific store for ttribute is unselect checkbock use default values / values from default store, You have to check it, or ser valuses store by store – Konrad Siamro Jun 15 '20 at 6:12

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.