cancel
Showing results for 
Search instead for 
Did you mean: 

Products are not updating programmatically Magento2

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?

 

 

6 REPLIES 6

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 !

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

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 !

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.

Re: Products are not updating programmatically Magento2

Hi @vartika_sharma

 

did you ever get a solution to that problem?

I have a similar issue where the update of attributes is working programmatically but then I cannot update them anymore in the back-end... different store view id is created.

Please tell me if you have a solution or if you need some more information on my issue.

Thanks. 

Jerome

 

Re: Products are not updating programmatically Magento2

If anybody else is hitting this wall, here is what I have done:

 

My script handles the creation and the update of the products.

At the beginning I prepare the settings like so:

$bootstrap = Bootstrap::create(BP, $_SERVER);
$objMan =  $bootstrap->getObjectManager();
$state = $objMan->get('\Magento\Framework\App\State');
$state->setAreaCode('adminhtml');

If you look up in the database for example under the table "catalog_product_entity_varchar" you will see that the product are saved after import with store_id: 0

 

When you update them, you should make sure that the store_id is still "0" like so:

$product->setData('store_id', 0);

 

Your script should then look something like this:

if ( $isNew ) :
				// create new products
        $product->setSku($sku);
        $product->setAttributeSetId(4);               
        $product->setWebsiteIds(array(1));
        $product->setVisibility(4);      
else:
				// update products
        $product->setData('store_id', 0);
endif;

// both create or update
$product->setName($name);
$product->setStatus( 1 );   
$product->setPrice($price);  
$product->setShortDescription($sdesc);
$product->setDescription($desc);
... etc .... 

$product->save();

I hope this helps.