Magento 2 : set product image programaticallyProducts not showing in frontendStoreviews not showing the same product image after importset product status programaticallymagento 2: set product image urlMagento 2: Product Import Error - Product Type is invalid or not supported. Duplicates found in column names [SOLVED]Set product image size?Magento 2: How to programatically mark product image as hidden from product page?Magento 2 : Upload product image programaticallyMagento 2: Add a product to the cart programmaticallyProgramatically Product Save with Image

Why does putting a dot after the URL remove login information?

Why is the Vasa Museum in Stockholm so Popular?

Which genus do I use for neutral expressions in German?

I am considering a visit to a Nevada brothel. What should I say at the US border?

Find a text string in a file and output only the rest of the text that follows it?

Can chords be inferred from melody alone?

Plato and the knowledge of the forms

What is the German idiom or expression for when someone is being hypocritical against their own teachings?

Make a living as a math programming freelancer?

Can a Hogwarts student refuse the Sorting Hat's decision?

Does the length of a password for Wi-Fi affect speed?

How to approach protecting my code as a research assistant? Should I be worried in the first place?

Can I enter Switzerland with only my London Driver's License?

How does LIDAR avoid getting confused in an environment being scanned by hundreds of other LIDAR?

What are the function of EM and EN spaces?

How to realistically deal with a shield user?

Purchased new computer from DELL with pre-installed Ubuntu. Won't boot. Should assume its an error from DELL?

How to make attic easier to traverse?

In MTG, was there ever a five-color deck that worked well?

How to check a file was encrypted (really & correctly)

What does the ISO setting for mechanical 35mm film cameras actually do?

How do I deal with large amout missing values in a data set without dropping them?

Is there a way to improve my grade after graduation?

…down the primrose path



Magento 2 : set product image programatically


Products not showing in frontendStoreviews not showing the same product image after importset product status programaticallymagento 2: set product image urlMagento 2: Product Import Error - Product Type is invalid or not supported. Duplicates found in column names [SOLVED]Set product image size?Magento 2: How to programatically mark product image as hidden from product page?Magento 2 : Upload product image programaticallyMagento 2: Add a product to the cart programmaticallyProgramatically Product Save with Image






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








2















I want to upload a product along with product image, please suggest how to set path or upload image for a product.
here is my code



 try
$_product->setName($product["name"]);
$_product->setTypeId('simple');
$_product->setAttributeSetId(4);
$_product->setSku($product["sku"]);
$_product->setWebsiteIds(array(1));
$_product->setVisibility(4);
$_product->setDescription($product["description"]);
$_product->setPrice($product["price"]);

$tmpDir = $this->getMediaDirTmpDir();
$this->file->checkAndCreateFolder($tmpDir);
$newFileName = $tmpDir . baseName($imageUrl);
$result = $this->file->read($imageUrl, $newFileName);
if ($result)
/** add saved file to the $product gallery */
$_product->setMediaGallery(array('images'=>array (),'values'=>array ()));
$_product->addImageToMediaGallery(how to set path, what to do,array('image', 'small_image', 'thumbnail'), true, $visible);


'manage_stock' => 1, //manage stock
'min_sale_qty' => 1, //Minimum Qty Allowed in Shopping Cart
'max_sale_qty' => 2, //Maximum Qty Allowed in Shopping Cart
'is_in_stock' => $product['stockstatus'], //Stock Availability
'qty' => $product['quantity']
)
);
$_product->save();


catch(Exception $e)
echo $e->getMessage();










share|improve this question
























  • i am suggesting you please try this in test script from root folder and hard code the image path first, if it works then do it dynamically next stackoverflow.com/questions/40259303/…

    – Nagaraju Kasa
    Aug 14 '17 at 9:36












  • i did but always got image does not exist this error

    – Jay Kapoor
    Aug 14 '17 at 9:44

















2















I want to upload a product along with product image, please suggest how to set path or upload image for a product.
here is my code



 try
$_product->setName($product["name"]);
$_product->setTypeId('simple');
$_product->setAttributeSetId(4);
$_product->setSku($product["sku"]);
$_product->setWebsiteIds(array(1));
$_product->setVisibility(4);
$_product->setDescription($product["description"]);
$_product->setPrice($product["price"]);

$tmpDir = $this->getMediaDirTmpDir();
$this->file->checkAndCreateFolder($tmpDir);
$newFileName = $tmpDir . baseName($imageUrl);
$result = $this->file->read($imageUrl, $newFileName);
if ($result)
/** add saved file to the $product gallery */
$_product->setMediaGallery(array('images'=>array (),'values'=>array ()));
$_product->addImageToMediaGallery(how to set path, what to do,array('image', 'small_image', 'thumbnail'), true, $visible);


'manage_stock' => 1, //manage stock
'min_sale_qty' => 1, //Minimum Qty Allowed in Shopping Cart
'max_sale_qty' => 2, //Maximum Qty Allowed in Shopping Cart
'is_in_stock' => $product['stockstatus'], //Stock Availability
'qty' => $product['quantity']
)
);
$_product->save();


catch(Exception $e)
echo $e->getMessage();










share|improve this question
























  • i am suggesting you please try this in test script from root folder and hard code the image path first, if it works then do it dynamically next stackoverflow.com/questions/40259303/…

    – Nagaraju Kasa
    Aug 14 '17 at 9:36












  • i did but always got image does not exist this error

    – Jay Kapoor
    Aug 14 '17 at 9:44













2












2








2


1






I want to upload a product along with product image, please suggest how to set path or upload image for a product.
here is my code



 try
$_product->setName($product["name"]);
$_product->setTypeId('simple');
$_product->setAttributeSetId(4);
$_product->setSku($product["sku"]);
$_product->setWebsiteIds(array(1));
$_product->setVisibility(4);
$_product->setDescription($product["description"]);
$_product->setPrice($product["price"]);

$tmpDir = $this->getMediaDirTmpDir();
$this->file->checkAndCreateFolder($tmpDir);
$newFileName = $tmpDir . baseName($imageUrl);
$result = $this->file->read($imageUrl, $newFileName);
if ($result)
/** add saved file to the $product gallery */
$_product->setMediaGallery(array('images'=>array (),'values'=>array ()));
$_product->addImageToMediaGallery(how to set path, what to do,array('image', 'small_image', 'thumbnail'), true, $visible);


'manage_stock' => 1, //manage stock
'min_sale_qty' => 1, //Minimum Qty Allowed in Shopping Cart
'max_sale_qty' => 2, //Maximum Qty Allowed in Shopping Cart
'is_in_stock' => $product['stockstatus'], //Stock Availability
'qty' => $product['quantity']
)
);
$_product->save();


catch(Exception $e)
echo $e->getMessage();










share|improve this question














I want to upload a product along with product image, please suggest how to set path or upload image for a product.
here is my code



 try
$_product->setName($product["name"]);
$_product->setTypeId('simple');
$_product->setAttributeSetId(4);
$_product->setSku($product["sku"]);
$_product->setWebsiteIds(array(1));
$_product->setVisibility(4);
$_product->setDescription($product["description"]);
$_product->setPrice($product["price"]);

$tmpDir = $this->getMediaDirTmpDir();
$this->file->checkAndCreateFolder($tmpDir);
$newFileName = $tmpDir . baseName($imageUrl);
$result = $this->file->read($imageUrl, $newFileName);
if ($result)
/** add saved file to the $product gallery */
$_product->setMediaGallery(array('images'=>array (),'values'=>array ()));
$_product->addImageToMediaGallery(how to set path, what to do,array('image', 'small_image', 'thumbnail'), true, $visible);


'manage_stock' => 1, //manage stock
'min_sale_qty' => 1, //Minimum Qty Allowed in Shopping Cart
'max_sale_qty' => 2, //Maximum Qty Allowed in Shopping Cart
'is_in_stock' => $product['stockstatus'], //Stock Availability
'qty' => $product['quantity']
)
);
$_product->save();


catch(Exception $e)
echo $e->getMessage();







magento2 product product-images






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Aug 14 '17 at 9:31









Jay KapoorJay Kapoor

3034 silver badges22 bronze badges




3034 silver badges22 bronze badges















  • i am suggesting you please try this in test script from root folder and hard code the image path first, if it works then do it dynamically next stackoverflow.com/questions/40259303/…

    – Nagaraju Kasa
    Aug 14 '17 at 9:36












  • i did but always got image does not exist this error

    – Jay Kapoor
    Aug 14 '17 at 9:44

















  • i am suggesting you please try this in test script from root folder and hard code the image path first, if it works then do it dynamically next stackoverflow.com/questions/40259303/…

    – Nagaraju Kasa
    Aug 14 '17 at 9:36












  • i did but always got image does not exist this error

    – Jay Kapoor
    Aug 14 '17 at 9:44
















i am suggesting you please try this in test script from root folder and hard code the image path first, if it works then do it dynamically next stackoverflow.com/questions/40259303/…

– Nagaraju Kasa
Aug 14 '17 at 9:36






i am suggesting you please try this in test script from root folder and hard code the image path first, if it works then do it dynamically next stackoverflow.com/questions/40259303/…

– Nagaraju Kasa
Aug 14 '17 at 9:36














i did but always got image does not exist this error

– Jay Kapoor
Aug 14 '17 at 9:44





i did but always got image does not exist this error

– Jay Kapoor
Aug 14 '17 at 9:44










1 Answer
1






active

oldest

votes


















0














Simply add this code after product price (No need another code for upload image)



 $imagePath = "D:imagesample.jpg"; // path of the image
$_product->addImageToMediaGallery($imagePath, array('image', 'small_image', 'thumbnail'), true, false);
$_product->save();





share|improve this answer



























  • what a stupid answer :)

    – HaFiz Umer
    Jul 15 at 12:21











  • is the same path of all the words system ?

    – HaFiz Umer
    Jul 15 at 12:27













Your Answer








StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "479"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f189042%2fmagento-2-set-product-image-programatically%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














Simply add this code after product price (No need another code for upload image)



 $imagePath = "D:imagesample.jpg"; // path of the image
$_product->addImageToMediaGallery($imagePath, array('image', 'small_image', 'thumbnail'), true, false);
$_product->save();





share|improve this answer



























  • what a stupid answer :)

    – HaFiz Umer
    Jul 15 at 12:21











  • is the same path of all the words system ?

    – HaFiz Umer
    Jul 15 at 12:27















0














Simply add this code after product price (No need another code for upload image)



 $imagePath = "D:imagesample.jpg"; // path of the image
$_product->addImageToMediaGallery($imagePath, array('image', 'small_image', 'thumbnail'), true, false);
$_product->save();





share|improve this answer



























  • what a stupid answer :)

    – HaFiz Umer
    Jul 15 at 12:21











  • is the same path of all the words system ?

    – HaFiz Umer
    Jul 15 at 12:27













0












0








0







Simply add this code after product price (No need another code for upload image)



 $imagePath = "D:imagesample.jpg"; // path of the image
$_product->addImageToMediaGallery($imagePath, array('image', 'small_image', 'thumbnail'), true, false);
$_product->save();





share|improve this answer















Simply add this code after product price (No need another code for upload image)



 $imagePath = "D:imagesample.jpg"; // path of the image
$_product->addImageToMediaGallery($imagePath, array('image', 'small_image', 'thumbnail'), true, false);
$_product->save();






share|improve this answer














share|improve this answer



share|improve this answer








edited Oct 22 '17 at 15:44









sv3n

10.2k6 gold badges25 silver badges57 bronze badges




10.2k6 gold badges25 silver badges57 bronze badges










answered Oct 22 '17 at 13:10









Israt Jahan SimuIsrat Jahan Simu

91 bronze badge




91 bronze badge















  • what a stupid answer :)

    – HaFiz Umer
    Jul 15 at 12:21











  • is the same path of all the words system ?

    – HaFiz Umer
    Jul 15 at 12:27

















  • what a stupid answer :)

    – HaFiz Umer
    Jul 15 at 12:21











  • is the same path of all the words system ?

    – HaFiz Umer
    Jul 15 at 12:27
















what a stupid answer :)

– HaFiz Umer
Jul 15 at 12:21





what a stupid answer :)

– HaFiz Umer
Jul 15 at 12:21













is the same path of all the words system ?

– HaFiz Umer
Jul 15 at 12:27





is the same path of all the words system ?

– HaFiz Umer
Jul 15 at 12:27

















draft saved

draft discarded
















































Thanks for contributing an answer to Magento Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f189042%2fmagento-2-set-product-image-programatically%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Get product attribute by attribute group code in magento 2get product attribute by product attribute group in magento 2Magento 2 Log Bundle Product Data in List Page?How to get all product attribute of a attribute group of Default attribute set?Magento 2.1 Create a filter in the product grid by new attributeMagento 2 : Get Product Attribute values By GroupMagento 2 How to get all existing values for one attributeMagento 2 get custom attribute of a single product inside a pluginMagento 2.3 How to get all the Multi Source Inventory (MSI) locations collection in custom module?Magento2: how to develop rest API to get new productsGet product attribute by attribute group code ( [attribute_group_code] ) in magento 2

Category:9 (number) SubcategoriesMedia in category "9 (number)"Navigation menuUpload mediaGND ID: 4485639-8Library of Congress authority ID: sh85091979ReasonatorScholiaStatistics

Magento 2.3: How do i solve this, Not registered handle, on custom form?How can i rewrite TierPrice Block in Magento2magento 2 captcha not rendering if I override layout xmlmain.CRITICAL: Plugin class doesn't existMagento 2 : Problem while adding custom button order view page?Magento 2.2.5: Overriding Admin Controller sales/orderMagento 2.2.5: Add, Update and Delete existing products Custom OptionsMagento 2.3 : File Upload issue in UI Component FormMagento2 Not registered handleHow to configured Form Builder Js in my custom magento 2.3.0 module?Magento 2.3. How to create image upload field in an admin form