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
Related
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?
The above function then checks for a condition for two scenarios:
1. Image is selected: a post is created from the first function when an image is selected. Because it already has the post_id, it simply updates the "dummy" content with the real content from the input fields
2. Image is not selected: as an image is not selected, then there is no post created from the first function. Because there is no post (thus no post_id), it creates a new post using the input fields.
Problem:
There are total 4 input fields that I need to have: title (title), description(content), tag(rh_tag) and custom field (custom_1).
I am having a problem with tag and custom field in a scenario when an image is selected (the function is not completed).
So, let say, if I do not select an image (thus no post is created from the first function), then the second function (and "else" part) simply creates a new post with all the four input. This works just fine.
However, if I select an image, then I am missing something and I can't somehow make the function work. I tried to add same $post =array as the second function, but it was not successful.
Can someone help me out how to "complete" the first function so that I can add tag and custom post meta even when an image is selected?
At first, look at WordPress codex. As I see, you have incorrect usage of update_post_meta. You should use post_id as first argument of the function, not array
At second, before accessing value from $_POST array, be sure that it exists:
$customValue = isset($_POST['custom_1']) ? $_POST['custom_1'] : 'default value';
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.
i have several attribute sets in my magento store. (v. 1.6.2)
When i make a new product "socks" is the default attribute set. But i would like "clothing" to be the default.
How can i change this?
I hope anyone can help.
Original question didn't seem to get answered here and I was looking for what Ronny also wanted.
Found there has been an extension released that does it simply for you.
http://www.magentocommerce.com/magento-connect/default-attribute-set.html
Unfortunately, it's not possible. You should create new product.
To change an existing product attribute set, you can Create a new Attribute Set that contain all the attributes you need
You can either install this extension (Change Attribute Set) or make the changes in the database (you will need to know the products id and attribute set id)
To manually change (Always backup before making changes):
Go to eav_attribute_set table and look for the attribute_set_id of the newly create attribute set (you could also get this in the admin by look at the url www.xxxx/edit/id/10/key/xxx on Edit Attribute Set 'xxxx' page)
After you have a list of all the product ids and corresponding attribute set ids, then go to the table below and update those fields
Table Name : catalog_product_entity
Field Name : attribute_set_id
Field Name : entity_id // is product_id
Another way is to delete those product and recreate using the new attribute set.
It is possible to accomplish what you want by editing the database directly. Only do this if you are comfortable with MySQL commands or another tool like phpMyAdmin.
To make this change you will need to do two things.
Get the ID of your new attribute set, in this case your "clothing" attribute set. You can locate the id in the eav_attribute_set database table or in the browser URL when editing the attribute set from the Magento admin area. I prefer to just use the database since we need to go in there anyways to make the change.Table Name : eav_attribute_set
Column Name: attribute_set_id
Look for the row in the table that has the name you used when you created the new set - it should be pretty obvious, you'll see all the attribute set names in the column called attribute_set_name
Once you have the preferred attribute set ID you then need to visit the eav_entity_type table and update the value in default_attribute_set_id column for the row whereentity_type_id = 4 and entity_type_code = catalog_product. In a fresh installation of Magento, the value to look for in this column is also 4.
Table Name : eav_entity_type
Column Name: default_attribute_set_id
Done! Once you've changed the id value you will notice your preferred attribute set is the one selected by default the next time you go to create a new product. I believe this is what the question was asking for.
NOTE: This should work for both Magento 1 and 2. At the time of writing this post the current Magento versions are: 1.9.2.4 and 2.0.7
I have the following scenario:
I have a content type called 'Product' in which there are the default input fields and a number of custom input fields that have been created using CCK.
When a new product is created, all of the data entered is posted as a node (as normal).
Part of the problem is that I don't want the values from the custom input fields to be within the node content. Therefore, my thinking was to hide the display of these fields. I could then create a block of the custom input fields using views.
Is it possible within views to set up some filtering that will only display the values of the custom input fields on the node from which it was originally created? For example if the product I created had a node id of 1, is it possible to filter the view to display the values of the custom input fields that were created from node id of 1 and then display these values as a block on node id 1?
This might sound a confusing approach but I am keen to separate some of the data entered into the Product content type from the main node content, mainly for styling reasons.
Thanks, Mark.
Ah worked it out. It's always an easy solution isn't it?
In the Arguments section of the view, add Node: Node ID. Select the action of 'Provide default argument' and then select 'Node ID from URL' from the 'Default argument type:'.
This will then just display the values of the custom input fields that was created on a particular node.