Magento 2 get product attribute value of a multivalued field - php

I have a custom attribute called 'Color', which is a select box,
I have a instance of the product, and I want to get the selected value of my field 'Color'. The problem is when I try to do it its always returning null.
This is my code:
$product->getCustomAttribute('prj_color')
This is always null. But if I try to get a different field, which is not a multivalued field, a textbox for example, I can get the value normally.
I'm using Magento 2.1.5. Someone know whats going on?

Related

Magento, I changed product attribute configuration, now my products are not storing new value of it

I changed configuration of one attribute, "Input Validation" for Store Owner "none" to "decimal". Now all the existing products are not storing new value of it, they show previous value for this attribute, however I indexed my system and tried to save but it get previously inputted value only, the old value is not changing,
Thanks guys for your suggestions, Magento uses EAV, my attribute was text type when i created it but later on, we changed it to decimal so the products which are previously created getting values from catalog_product_entity_text table and storing new value at catalog_product_entity_decimal table, i don't know why it happened.
I removed entries from catalog_product_entity_text table and now its working fine.

PHP- Get index/associated value of option selected in dropdown box

I want to show values in a dropdown box (either from an array or database - please advise on which option is method)
Then
when a user selects a value in dropdown box, i want to have and get its associated index value (like 1, 2).
For example: dropdown box shows values:
"Car"
"Bicycle"
If user selects "Car", when i get dropdown selected value, i should get 1, similarly for "Bicycle" i get 2 .. and so on.
Please advise easy and simplest method to implement this
Thanks
You can use two methods:
1) Set up prior to display a code that holds associative array with key -> value i.e: 1 => Car (you can keep it in config file if it doesn't change frequently, you can pull it from database or you can keep it in some other form: serialized, file etc.) and use it when the submitted form is being processed.
2) Use array with key and value with the same string i.e: Car => Car and when the form is processed you will have value right away. This solution has some limitations and be troublesome with using more words or other characters that need to be sanitized.
I would advise option 1, you will have to set up the list before and use it after form has been processed but it allows more freedom and its less maintenance.

Magento selected Attribute result

I would like to add a Text depends on selected Attribute option: I create a Attribute with the optiones YES or NO.
I want to create with php if I select in the Admin Panel yes that if should start another php code (that will show a few text on the category pages)
But if I select No it should not run through the code, I mean don't display the text
If you created a Yes/No attribute (select Yes/No for the Catalog Input Type field), then you can check the value simply using
$_product->getData('my_attribute_code');
This will return either 0 or 1.
So if you try
if($_product->getData('my_attribute_code')){
echo 'My text';
}
Note that depending on the context you may have to load the product first to get this value, so if nothing comes out of the getAttribute function, so you may have to execute this code before the previous one :
$_product->load($_product->getId())
And this should work.
EDIT: Note that this will not work if you create a dropdown attribute and set manually Yes and No as values. You need to chose Yes/No for the Catalog Input Type field of your attribute.
Cheers

How to get value from multiple drop down fields with the same name

I have added 5 drop down fields in my form with the same name. These field will be displayed based on selected country's radio button.I have 5 radio buttons named as "USA,INDIA,UK,EURO,AUS".
If i select USA, 1st drop down contains US sizes, will be displayed.
If i select INDIA,2nd drop down contains INDIAN sizes, will be displayed.
All are working Good. Now i have an issue with store that value to my DB in magento. When i submit the form, 5th drop down field(EURO sizes) value only saved.
If i select size other than EURO,I got an empty value. If i select Euro size, I got correct value.I hope you understand my problem. Please guide me to solve mine issue guys!
You can add disabled attribute to other drop-downs when you show the selected one. This way the browser will ignore the disabled ones.
Or, you could append [] to your drop-downs' name, like this:
<select name="name[]"> ... </select>
This way you will get an array in the request variable, then you can decide which to use in the array according to your radio button value.

Symfony Set Drop Down Value Based Upon ID

I am still getting my hands dirty with Symfony, so I am a bit ignorant on how to do something that should be pretty simple.
I have a form that I am creating in the controller and passing to the view:
$form = $this->createForm(new PurchaseOrderType($account), $purchaseOrder);
The form displays exactly how I need it to, no problems at all! I am trying to now make the form more dynamic so that it can auto select a drop down list based upon an "id" variable that I am passing into the form. The id equals 23 by the way.
So, I have a drop down of suppliers and one of the options value is 23. How do I automatically select this option? Sorry for my ignorance :)
Thanks!
Without the code of your form type, I can make only suggestion.
If I get it correctly, inside the purchase order entity, there is an other entity mapped, and that one is represented with an id.
The object, $purchaseOrder has to have the other entity, then in the form type, when you set up the drop down field, you have to specify - with the correct name it shouldn't be a problem - the foreign id.
But you need of course data for the drop down field what can be the result of a SELECT * query.

Categories