Timeline for Magento2 - programmatically add product attribute options
Current License: CC BY-SA 4.0
23 events
when toggle format | what | by | license | comment | |
---|---|---|---|---|---|
S Feb 3, 2022 at 22:16 | history | suggested | Mukesh Khatri | CC BY-SA 4.0 |
TypeError: trim() expects parameter 1 to be string, object given in root/vendor/magento/module-eav/Model/Entity/Attribute/OptionManagement.php:63 fix with $option->setLabel($optionLabel->getLabel());
|
Feb 3, 2022 at 17:57 | review | Suggested edits | |||
S Feb 3, 2022 at 22:16 | |||||
Sep 16, 2021 at 15:29 | comment | added | Francesco Gasparetto | The code is working on Magento 2.4.1, except for a little bug: the check if the option value already exists is done in case sensitive, but Magento give then an error saying the value already exists, if the same label with different case is found. I solved it by adding strtolower on the label in the last isset check | |
Jul 16, 2020 at 12:29 | comment | added | Kharidas Chebotaryov | update: 2020-07-07 it still does not return an option id. cool | |
Apr 17, 2019 at 7:21 | comment | added | Nadeem0035 | @JELLEJ If you are getting issue Uncaught TypeError: Argument 3 passed to Magento\Eav\Model\Entity\Attribute\OptionManagement::setOptionValue() must be of the type string, object given in foreach function then change $option->setLabel($optionLabel); to $option->setLabel($label); at line 102 | |
Mar 23, 2019 at 11:45 | comment | added | Sourav | Yes this code not working | |
Dec 12, 2018 at 14:57 | comment | added | JJ15 | if I add this in a foreach function, in the second iteration I will get the error "Magento\Eav\Model\Entity\Attribute\OptionManagement::setOptionValue() must be of the type string, object given" | |
Nov 20, 2017 at 7:36 | comment | added | Ajay Patel | @RyanHoerr i have try this code but option not created | |
Oct 13, 2017 at 12:44 | comment | added | DEEP JOSHI | Hello Ryan, How can I execute same process at time of module's installation. | |
Jun 7, 2017 at 4:16 | comment | added | Erfan |
Why not use \Magento\Eav\Api\AttributeOptionManagementInterface ?
|
|
Sep 12, 2016 at 20:35 | history | edited | Ryan Hoerr | CC BY-SA 3.0 |
Corrected setValue() reference per @quickshiftin.
|
Sep 12, 2016 at 19:37 | comment | added | quickshiftin |
Hi Ryan, you shouldn't set the value on the option, this is the internal id magento uses and I found out the hard way that if you set the value to a string value with a leading number like '123 abc corp' it causes some serious problems due to the implementation of Magento\Eav\Model\ResourceModel\Entity\Attribute::_processAttributeOptions . See for yourself, if you remove the $option->setValue($label); statement from your code, it will save the option, then when you fetch it Magento will return the value from an auto-increment on the eav_attribute_option table.
|
|
Sep 9, 2016 at 14:53 | history | edited | Ryan Hoerr | CC BY-SA 3.0 |
Updated solution to resolve runtime error in M2.1.
|
Jun 17, 2016 at 18:22 | comment | added | Bartosz Kubicki | I am writing it also in Magento 2 :) I know Magento 1.9 too, so tutorial I posted isn't exactly useful, but can be easily adopted. Names of methods are similar or even the same and I have already found the way to get admin value of atrribute's option it can be done (I think) with use of some collecion and proper filtering :) | |
Jun 17, 2016 at 18:16 | comment | added | Ryan Hoerr | @lord_of_strings: Note that the code I posted is for Magento 2. If you're not using Magento 2, it won't be of much use to you. | |
Jun 17, 2016 at 18:01 | comment | added | Bartosz Kubicki |
Ok, thank you I will test in on monday, the key is change something here: $productModel = Mage::getModel('catalog/product'); $attr = $productModel->getResource()->getAttribute("size"); if ($attr->usesSource()) { echo $size_id = $attr->getSource()->getOptionId("XL"); } to force to look not i default scope but in admin. Is it correct if I have some integer value for attribute option in scope of admin and some string for frontend - I don't know if I understand concept of these labels correctly.
|
|
Jun 17, 2016 at 17:54 | comment | added | Ryan Hoerr |
@lord_of_strings: Yes, that's correct. Where you see 0 => $label, , that's an array of store translations. The key is the store ID, value is the text for that store. The code assumes you'll be setting and getting everything in the default scope, but you could adapt it to use a store.
|
|
Jun 17, 2016 at 17:48 | comment | added | Bartosz Kubicki |
Thanks Ryan H. it's very useful piece of code. Actually I have a question. If option is added to store with id 0 (admin) it will be rendered with the same label in front in store f.e. with id 1. What can I do to change label for frontend? Just 1 => 'something else' ? If I have done it adopting this tutorial: http://magentorex.com/magento-get-product-attributes-option-id-from-option-label/ But function getting option id, only returns ids of attributes for store 1 (frontend) and not for admin.
|
|
Mar 3, 2016 at 7:45 | vote | accept | werd | ||
Feb 29, 2016 at 15:35 | comment | added | Ryan Hoerr | It's as official as you're going to get. All of the lookups and option adding go through core Magento. My class is just a wrapper for those core methods that makes them easier to use. | |
Feb 29, 2016 at 15:27 | comment | added | werd | Thanks! I guess this is not the "official" way of solving the issue, right? However I was not able to find any actual reference to product attribute option management in Magento2 source itself. | |
Feb 29, 2016 at 15:09 | history | edited | Ryan Hoerr | CC BY-SA 3.0 |
added 74 characters in body
|
Feb 29, 2016 at 14:54 | history | answered | Ryan Hoerr | CC BY-SA 3.0 |