How Importing Images with Labels using addImageToMediaGallery in magento2? Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) Announcing the arrival of Valued Associate #679: Cesar Manara Unicorn Meta Zoo #1: Why another podcast?How to programatically import product and set images to default?Adding images with CSV importMagmi import extra images, how?Importing Images with Labels using addImageToMediaGalleryAvs FastSimpleImport - Import all imagesStoreviews not showing the same product image after importBase Image, Small Image, Thumbnail Image radio buttons are not selected after product images CSV importhow to uncheck exclude image checkbox at mass image importMagento 2.2 adding images to product programmatically does not set small_image
Are my PIs rude or am I just being too sensitive?
Is it possible to ask for a hotel room without minibar/extra services?
Replacing HDD with SSD; what about non-APFS/APFS?
3 doors, three guards, one stone
Windows 10: How to Lock (not sleep) laptop on lid close?
Was credit for the black hole image misattributed?
Stars Make Stars
Mortgage adviser recommends a longer term than necessary combined with overpayments
How to politely respond to generic emails requesting a PhD/job in my lab? Without wasting too much time
Classification of bundles, Postnikov towers, obstruction theory, local coefficients
Need a suitable toxic chemical for a murder plot in my novel
How many things? AとBがふたつ
Can a non-EU citizen traveling with me come with me through the EU passport line?
Direct Experience of Meditation
What LEGO pieces have "real-world" functionality?
How is simplicity better than precision and clarity in prose?
90's book, teen horror
Is 1 ppb equal to 1 μg/kg?
Understanding this description of teleportation
Stop battery usage [Ubuntu 18]
How does the Nova's Burn power work at the 7-9 level?
Sorting inherited template fields
Stopping real property loss from eroding embankment
How does modal jazz use chord progressions?
How Importing Images with Labels using addImageToMediaGallery in magento2?
Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)
Announcing the arrival of Valued Associate #679: Cesar Manara
Unicorn Meta Zoo #1: Why another podcast?How to programatically import product and set images to default?Adding images with CSV importMagmi import extra images, how?Importing Images with Labels using addImageToMediaGalleryAvs FastSimpleImport - Import all imagesStoreviews not showing the same product image after importBase Image, Small Image, Thumbnail Image radio buttons are not selected after product images CSV importhow to uncheck exclude image checkbox at mass image importMagento 2.2 adding images to product programmatically does not set small_image
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
Magento2.1.x
I had a problem adding a product Image Label programatically. Here's the code I used:
$imageLabel1 = 'Test1';
$imageLabel2 = 'Test2';
$imageLabel3 = 'Test3';
$product->setMediaGallery (array('images'=>array (), 'values'=>array ()))
->addImageToMediaGallery('/test1.jpg', array('image'), false, false);
->addImageToMediaGallery('/test2.jpg', array('thumbnail'), false, false);
->addImageToMediaGallery('/test3.jpg', array('small_image'), false, false);
Where i can set $imageLabel1 - 2 - 3?
magento-2.1 import product-images programmatically
bumped to the homepage by Community♦ yesterday
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
add a comment |
Magento2.1.x
I had a problem adding a product Image Label programatically. Here's the code I used:
$imageLabel1 = 'Test1';
$imageLabel2 = 'Test2';
$imageLabel3 = 'Test3';
$product->setMediaGallery (array('images'=>array (), 'values'=>array ()))
->addImageToMediaGallery('/test1.jpg', array('image'), false, false);
->addImageToMediaGallery('/test2.jpg', array('thumbnail'), false, false);
->addImageToMediaGallery('/test3.jpg', array('small_image'), false, false);
Where i can set $imageLabel1 - 2 - 3?
magento-2.1 import product-images programmatically
bumped to the homepage by Community♦ yesterday
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
add a comment |
Magento2.1.x
I had a problem adding a product Image Label programatically. Here's the code I used:
$imageLabel1 = 'Test1';
$imageLabel2 = 'Test2';
$imageLabel3 = 'Test3';
$product->setMediaGallery (array('images'=>array (), 'values'=>array ()))
->addImageToMediaGallery('/test1.jpg', array('image'), false, false);
->addImageToMediaGallery('/test2.jpg', array('thumbnail'), false, false);
->addImageToMediaGallery('/test3.jpg', array('small_image'), false, false);
Where i can set $imageLabel1 - 2 - 3?
magento-2.1 import product-images programmatically
Magento2.1.x
I had a problem adding a product Image Label programatically. Here's the code I used:
$imageLabel1 = 'Test1';
$imageLabel2 = 'Test2';
$imageLabel3 = 'Test3';
$product->setMediaGallery (array('images'=>array (), 'values'=>array ()))
->addImageToMediaGallery('/test1.jpg', array('image'), false, false);
->addImageToMediaGallery('/test2.jpg', array('thumbnail'), false, false);
->addImageToMediaGallery('/test3.jpg', array('small_image'), false, false);
Where i can set $imageLabel1 - 2 - 3?
magento-2.1 import product-images programmatically
magento-2.1 import product-images programmatically
edited Oct 31 '18 at 18:40
sv3n
9,96762557
9,96762557
asked Aug 30 '17 at 18:12
Magento2 DevloperMagento2 Devloper
9431135
9431135
bumped to the homepage by Community♦ yesterday
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
bumped to the homepage by Community♦ yesterday
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Try this:
$imageLabel1 = 'Test1';
$existingMediaGalleryEntries = $product->getMediaGalleryEntries();
foreach ($existingMediaGalleryEntries as $key => $entry)
$entry->setLabel($imageLabel1);
$product->setStoreId(0);
$product->setMediaGalleryEntries($existingMediaGalleryEntries);
UPDATED
The code above is the only way to set label for a media gallery entry, because there's no direct API from Product to set them.
It also means, you still using your current code to set new media gallery, then use the code above to update label for it after $product->save() (another mean, you have to save() product twice)
Pseudo code
public function addImageToMediaGallery(Product $product)
// your code
$product->save();
public function updateImageLabel(Product $product, $label)
// my code
$product->save();
public function updateProduct()
// ....
$this->addImageToMediaGallery($product);
foreach ($labels as $label)
$this->updateImageLabel($label);
// ....
Thanx, but where can I set this code? I mean below my code? Is it working with multiple image set? see my updated question@ Toan
– Magento2 Devloper
Aug 31 '17 at 5:34
@Magento2Devloper I updated my question. Hope it makes more sense to you now :)
– Toan Nguyen
Aug 31 '17 at 7:00
It's working but image label not set image wise. ex - test1.jpg in set imageLabel3. any idea regarding this?
– Magento2 Devloper
Aug 31 '17 at 8:17
@Magento2Devloper I don't understand what you mean. Can you update it in your question again?
– Toan Nguyen
Aug 31 '17 at 8:33
I need to set image label like ex - test1.jpg in set 'Test1' Label. in your code set test1.jpg in 'Test3' label. now understand?
– Magento2 Devloper
Aug 31 '17 at 8:42
|
show 2 more comments
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f191269%2fhow-importing-images-with-labels-using-addimagetomediagallery-in-magento2%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
Try this:
$imageLabel1 = 'Test1';
$existingMediaGalleryEntries = $product->getMediaGalleryEntries();
foreach ($existingMediaGalleryEntries as $key => $entry)
$entry->setLabel($imageLabel1);
$product->setStoreId(0);
$product->setMediaGalleryEntries($existingMediaGalleryEntries);
UPDATED
The code above is the only way to set label for a media gallery entry, because there's no direct API from Product to set them.
It also means, you still using your current code to set new media gallery, then use the code above to update label for it after $product->save() (another mean, you have to save() product twice)
Pseudo code
public function addImageToMediaGallery(Product $product)
// your code
$product->save();
public function updateImageLabel(Product $product, $label)
// my code
$product->save();
public function updateProduct()
// ....
$this->addImageToMediaGallery($product);
foreach ($labels as $label)
$this->updateImageLabel($label);
// ....
Thanx, but where can I set this code? I mean below my code? Is it working with multiple image set? see my updated question@ Toan
– Magento2 Devloper
Aug 31 '17 at 5:34
@Magento2Devloper I updated my question. Hope it makes more sense to you now :)
– Toan Nguyen
Aug 31 '17 at 7:00
It's working but image label not set image wise. ex - test1.jpg in set imageLabel3. any idea regarding this?
– Magento2 Devloper
Aug 31 '17 at 8:17
@Magento2Devloper I don't understand what you mean. Can you update it in your question again?
– Toan Nguyen
Aug 31 '17 at 8:33
I need to set image label like ex - test1.jpg in set 'Test1' Label. in your code set test1.jpg in 'Test3' label. now understand?
– Magento2 Devloper
Aug 31 '17 at 8:42
|
show 2 more comments
Try this:
$imageLabel1 = 'Test1';
$existingMediaGalleryEntries = $product->getMediaGalleryEntries();
foreach ($existingMediaGalleryEntries as $key => $entry)
$entry->setLabel($imageLabel1);
$product->setStoreId(0);
$product->setMediaGalleryEntries($existingMediaGalleryEntries);
UPDATED
The code above is the only way to set label for a media gallery entry, because there's no direct API from Product to set them.
It also means, you still using your current code to set new media gallery, then use the code above to update label for it after $product->save() (another mean, you have to save() product twice)
Pseudo code
public function addImageToMediaGallery(Product $product)
// your code
$product->save();
public function updateImageLabel(Product $product, $label)
// my code
$product->save();
public function updateProduct()
// ....
$this->addImageToMediaGallery($product);
foreach ($labels as $label)
$this->updateImageLabel($label);
// ....
Thanx, but where can I set this code? I mean below my code? Is it working with multiple image set? see my updated question@ Toan
– Magento2 Devloper
Aug 31 '17 at 5:34
@Magento2Devloper I updated my question. Hope it makes more sense to you now :)
– Toan Nguyen
Aug 31 '17 at 7:00
It's working but image label not set image wise. ex - test1.jpg in set imageLabel3. any idea regarding this?
– Magento2 Devloper
Aug 31 '17 at 8:17
@Magento2Devloper I don't understand what you mean. Can you update it in your question again?
– Toan Nguyen
Aug 31 '17 at 8:33
I need to set image label like ex - test1.jpg in set 'Test1' Label. in your code set test1.jpg in 'Test3' label. now understand?
– Magento2 Devloper
Aug 31 '17 at 8:42
|
show 2 more comments
Try this:
$imageLabel1 = 'Test1';
$existingMediaGalleryEntries = $product->getMediaGalleryEntries();
foreach ($existingMediaGalleryEntries as $key => $entry)
$entry->setLabel($imageLabel1);
$product->setStoreId(0);
$product->setMediaGalleryEntries($existingMediaGalleryEntries);
UPDATED
The code above is the only way to set label for a media gallery entry, because there's no direct API from Product to set them.
It also means, you still using your current code to set new media gallery, then use the code above to update label for it after $product->save() (another mean, you have to save() product twice)
Pseudo code
public function addImageToMediaGallery(Product $product)
// your code
$product->save();
public function updateImageLabel(Product $product, $label)
// my code
$product->save();
public function updateProduct()
// ....
$this->addImageToMediaGallery($product);
foreach ($labels as $label)
$this->updateImageLabel($label);
// ....
Try this:
$imageLabel1 = 'Test1';
$existingMediaGalleryEntries = $product->getMediaGalleryEntries();
foreach ($existingMediaGalleryEntries as $key => $entry)
$entry->setLabel($imageLabel1);
$product->setStoreId(0);
$product->setMediaGalleryEntries($existingMediaGalleryEntries);
UPDATED
The code above is the only way to set label for a media gallery entry, because there's no direct API from Product to set them.
It also means, you still using your current code to set new media gallery, then use the code above to update label for it after $product->save() (another mean, you have to save() product twice)
Pseudo code
public function addImageToMediaGallery(Product $product)
// your code
$product->save();
public function updateImageLabel(Product $product, $label)
// my code
$product->save();
public function updateProduct()
// ....
$this->addImageToMediaGallery($product);
foreach ($labels as $label)
$this->updateImageLabel($label);
// ....
edited Aug 31 '17 at 7:00
answered Aug 31 '17 at 4:27
Toan NguyenToan Nguyen
2,0021138
2,0021138
Thanx, but where can I set this code? I mean below my code? Is it working with multiple image set? see my updated question@ Toan
– Magento2 Devloper
Aug 31 '17 at 5:34
@Magento2Devloper I updated my question. Hope it makes more sense to you now :)
– Toan Nguyen
Aug 31 '17 at 7:00
It's working but image label not set image wise. ex - test1.jpg in set imageLabel3. any idea regarding this?
– Magento2 Devloper
Aug 31 '17 at 8:17
@Magento2Devloper I don't understand what you mean. Can you update it in your question again?
– Toan Nguyen
Aug 31 '17 at 8:33
I need to set image label like ex - test1.jpg in set 'Test1' Label. in your code set test1.jpg in 'Test3' label. now understand?
– Magento2 Devloper
Aug 31 '17 at 8:42
|
show 2 more comments
Thanx, but where can I set this code? I mean below my code? Is it working with multiple image set? see my updated question@ Toan
– Magento2 Devloper
Aug 31 '17 at 5:34
@Magento2Devloper I updated my question. Hope it makes more sense to you now :)
– Toan Nguyen
Aug 31 '17 at 7:00
It's working but image label not set image wise. ex - test1.jpg in set imageLabel3. any idea regarding this?
– Magento2 Devloper
Aug 31 '17 at 8:17
@Magento2Devloper I don't understand what you mean. Can you update it in your question again?
– Toan Nguyen
Aug 31 '17 at 8:33
I need to set image label like ex - test1.jpg in set 'Test1' Label. in your code set test1.jpg in 'Test3' label. now understand?
– Magento2 Devloper
Aug 31 '17 at 8:42
Thanx, but where can I set this code? I mean below my code? Is it working with multiple image set? see my updated question@ Toan
– Magento2 Devloper
Aug 31 '17 at 5:34
Thanx, but where can I set this code? I mean below my code? Is it working with multiple image set? see my updated question@ Toan
– Magento2 Devloper
Aug 31 '17 at 5:34
@Magento2Devloper I updated my question. Hope it makes more sense to you now :)
– Toan Nguyen
Aug 31 '17 at 7:00
@Magento2Devloper I updated my question. Hope it makes more sense to you now :)
– Toan Nguyen
Aug 31 '17 at 7:00
It's working but image label not set image wise. ex - test1.jpg in set imageLabel3. any idea regarding this?
– Magento2 Devloper
Aug 31 '17 at 8:17
It's working but image label not set image wise. ex - test1.jpg in set imageLabel3. any idea regarding this?
– Magento2 Devloper
Aug 31 '17 at 8:17
@Magento2Devloper I don't understand what you mean. Can you update it in your question again?
– Toan Nguyen
Aug 31 '17 at 8:33
@Magento2Devloper I don't understand what you mean. Can you update it in your question again?
– Toan Nguyen
Aug 31 '17 at 8:33
I need to set image label like ex - test1.jpg in set 'Test1' Label. in your code set test1.jpg in 'Test3' label. now understand?
– Magento2 Devloper
Aug 31 '17 at 8:42
I need to set image label like ex - test1.jpg in set 'Test1' Label. in your code set test1.jpg in 'Test3' label. now understand?
– Magento2 Devloper
Aug 31 '17 at 8:42
|
show 2 more comments
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f191269%2fhow-importing-images-with-labels-using-addimagetomediagallery-in-magento2%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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