2

How to Update product details using sku via script?? for example update description , short desc, price etc.. for a configurable product or a simple product

1

You need to create folder in root of your system and create one php file inside those folder,

Keep below content in your file and run from browser,

<?php
// MAGENTO START
include('app/bootstrap.php');
use Magento\Framework\App\Bootstrap;
$bootstrap = Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();

$state = $objectManager->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');

$productCollectionFactory = $objectManager->get('\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory');
$collection = $productCollectionFactory->create();
$collection->addAttributeToFilter('sku', '24-MB01');
$collection->addAttributeToSelect('*');
foreach ($collection as $product) 
{
    $product->setData('short_description', 'test description');
    $product->save();
    echo "Product Updated". " ";
} 
  • Where to get the sku value in database?? – Jaisa Oct 23 '17 at 12:45
  • You need to pass manually or need to get from csv file – Rakesh Jesadiya Oct 23 '17 at 12:54
  • Thank You Sir. I already passed it manually and works fine .But if i need to update for 100 products means how do i modify the above code and What are the fields i need to keep in my CSV file?? – Jaisa Oct 23 '17 at 13:08
  • How do i import product details using SKU – Jaisa Oct 24 '17 at 4:42
  • You need to do $product->setData('description', 'long description'); $product->save(); – Rakesh Jesadiya Oct 24 '17 at 4:45
0

You can use the Endpoint "/rest/V1/products/xxxx" (where XXX is the SKU) with a PUT request to update products.

The body would look something similar to -

{
 "product": {
  "custom_attributes": [
    {
        "attribute_code": "description",
        "value": "What you want to change the description to"
    }
  ]
  }
}

For price -

{
   "product": {
   "price": 12.34
   }
}

Might be worth looking over the Swagger documentation and the authentication needed to use this.

EDIT:

I was using Postman to test this. If you wanted a php / cURL script for this it would look something like:

<?php

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => "<YOUR SITE GOES HERE>/rest/V1/products/<SKU OF PRODUCT>",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "PUT",
  CURLOPT_POSTFIELDS => "{\n\"product\": {\n\"price\": <PRICE GOES HERE>\n \n}\n}",
  CURLOPT_HTTPHEADER => array(
"authorization: Bearer <BEARER TOKEN GOES HERE>",
"cache-control: no-cache",
"content-type: application/json",
  ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
// Whatever else you need to do if the request works goes here
}

Obviosuly filling in yhe details that are needed. You could make a simple HTML page before this that a user can fill in SKU / PRICE/ DESCRIPTION etc.

Or expand the script out further to work for as many products as needed.

  • Need clear explanation for this – Jaisa Oct 23 '17 at 11:23
  • Added an example Postman / PHP script for this. – BigDaddyL Oct 23 '17 at 11:34
  • I am totally new to magento 2 and I cannot understand your code. I wish to use php script to update product price or product description using sku. – Jaisa Oct 23 '17 at 11:39
  • This isn't Magento 2 specific - it's using PHP / cURL to update a product. It is using a specific Magento 2 endpoint to update the given product though. This endpoint needs to know the SKU of the product and what the changes are. It may be worth looking at some example cURL stuff to get an idea. Also using Postman makes this process a lot easier for testing. – BigDaddyL Oct 23 '17 at 11:46

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.