cancel
Showing results for 
Search instead for 
Did you mean: 

Products are not updating programmatically Magento2

Highlighted

Products are not updating programmatically Magento2

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?

 

 

4 REPLIES 4
Highlighted

Re: Products are not updating programmatically Magento2

Hello @vartika_sharma ,

 

These are default fields name in Magento (for ex: Name) and this is the correct way to update it, might be this issue is occurring because of other piece of code, please update the code to the below code for debugging error:

public function updateProducts(){
            $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); // instance of object manager
            $product = $objectManager->create('\Magento\Catalog\Model\Product');
            $product->load(3734);
            if(isset($product->getSku())){
                try{
                    $product->setStoreId(1)
                    ->setSku('sku')
                    ->setName('name')
                    ->setStatus(1)
                    ->setWeight(10)
                    ->setVisibility(4)
                    ->setPrice(100)
                    ->setCustomAttribute('case_back','Black')
                    ->setCustomAttribute('case_material','Silver')
                    ->save();
                }catch(\Exception $e){
                    echo $e->getMessage();
                }
            }
            else{
                echo "Product Not Found.";
            }
        echo "<pre>"; print_r($product->getData());
    }

if you get correct data from last print but again it reflect the same problem, then it is a different plugin which is updating the name again to something else.

 

Problem Solved ? Click on 'Kudos' & Accept as Solution to encourage to write more answers !
Highlighted

Re: Products are not updating programmatically Magento2

I got correct data from last print and facing the same issue. But why only fields with Store View Scope are not updating

Highlighted

Re: Products are not updating programmatically Magento2

@vartika_sharma 

 

If you got correct data from last execution of print, assuming that you got the changed name from last print and didn't get any error from try block, then it means that product name is updating from different code again, there must be some other plugin which is changing name of product back.

try to find "setName" in app/code folder if there is any other code which is setting name or any other field programatically.

 

Problem Solved ? Click on 'Kudos' & Accept as Solution to encourage to write more answers !
Highlighted

Re: Products are not updating programmatically Magento2

I have removed every code from app/code folder which is setting details for product but still getting same issue.