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?

7
  • Try $_product->setCustomAttribute("code", 'value'); to update the custom attributes. Commented Jul 25, 2020 at 12:54
  • @BhaumikUpadhyay Tried that too. I've edited question please check now.
    – SharmaV
    Commented Jul 25, 2020 at 13:31
  • Which Magento version you are using? Commented Jul 25, 2020 at 15:39
  • Please check this link --- magento.stackexchange.com/a/157533/85907 Commented Jul 25, 2020 at 16:12
  • @BhaumikUpadhyay Magento 2.3.3
    – SharmaV
    Commented Jul 26, 2020 at 12:18

1 Answer 1

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);
2
  • How can I update product for whole website with every store
    – SharmaV
    Commented Jul 27, 2020 at 9:32
  • Get all store ids and loop the action and update the products. Commented Jul 29, 2020 at 2:17

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.