0

I'm trying to update product details by this code -

public function updateProducts(){
            $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); // instance of object manager
            $product = $objectManager->create('\Magento\Catalog\Model\Product');
            $product->load(3734);
            $product->setStoreId(1);
            $product->setSku('sku'); // Set your sku here
            $product->setName('name'); // Name of Product
            $product->setStatus(1); // Status on product enabled/ disabled 1/0
            $product->setWeight(10); // weight of product
            $product->setVisibility(4); 
            $product->setPrice(100); // price of product
            $product->setCustomAttribute('case_back','Black');
            $product->setCustomAttribute('case_material','Silver');
            
            $product->save();
        
    }

Some of the values are updated like Sku and Price but Name and others are not. I tried to update custom attributes also but it results same. For example - case_back updated but case_material not.

I found out that fields with Global scope are updating but Store View scope are not even though I set storeId also.

So how can I update all of the details of product?

1

You can use the updateAttribute function to update the existing product attribute easily with storewise also.

Here is the sample Code:

$updateAttributes['case_back'] = "Black";
$updateAttributes['case_material'] = "Silver";
$action = objectManager->create('\Magento\Catalog\Model\ResourceModel\Product\Action');
$action->updateAttributes([3734], $updateAttributes, $storeId);
| improve this answer | |
  • How can I update product for whole website with every store – vartika sharma Jul 27 at 9:32
  • Get all store ids and loop the action and update the products. – Sathishkumar Jul 29 at 2: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.