php 自动获取 Magento 中自定义属性的选定下拉值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17737547/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting selected dropdown value for custom attribute in Magento automatically
提问by user1597438
I'm trying to get a custom dropdown attribute's selected value using
我正在尝试使用以下方法获取自定义下拉属性的选定值
echo $_product->getProductSize();
and
和
echo $_product->getAttributeText('product_size');
Then, I clear my cache and reindex and reload my page. I tried selecting a value from the dropdown but either one returns anything. So basically, my question is, how can I retrieve the selected value from a custom dropdown attribute? I'm planning on using this to display different contents on my product page depending on the selected value. Thanks in advance for any help and advice.
然后,我清除缓存并重新索引并重新加载我的页面。我尝试从下拉列表中选择一个值,但任何一个都返回任何值。所以基本上,我的问题是,如何从自定义下拉属性中检索选定的值?我计划使用它根据所选值在我的产品页面上显示不同的内容。在此先感谢您的任何帮助和建议。
Addition: I'm trying to call it on the product page where the same dropdown is called.
另外:我试图在调用相同下拉列表的产品页面上调用它。
After trying to play with the attribute a little bit through the admin panel, I noticed how the value I selected echoed on the page. However, I was hoping of being able to retrieve it dynamically on the frontend. Is this possible? For example, in the frontend, the customer selects option B, then I would display information related to option B. Then if he changed to option D then the information would change to option D's information.
在尝试通过管理面板稍微使用该属性后,我注意到我选择的值如何在页面上回显。但是,我希望能够在前端动态检索它。这可能吗?比如在前端,客户选择选项B,然后我会显示选项B的相关信息。然后如果他更改为选项D,则信息将更改为选项D的信息。
回答by Deependra Singh
echo $_product->getAttributeText('product_size');
It should work unless your theme is dependent on this setting 'Used in Product Listing' and 'Visible on Product View Page on Front-end' for your attribute from backend in Manage Attribute. Additionally check if your attribute code does not contain any spaces. Although magento do not let you use spaces in attribute code through form submission but if attribute is created programmatically or from sql query then it is possible. Other wise it is something else not your code or attribute causing problem.
它应该可以工作,除非您的主题依赖于“在产品列表中使用”和“在前端的产品视图页面上可见”的“管理属性”后端属性设置。另外检查您的属性代码是否不包含任何空格。虽然 magento 不允许您通过表单提交在属性代码中使用空格,但如果属性是以编程方式或从 sql 查询创建的,那么它是可能的。否则,这不是您的代码或属性导致问题的原因。
回答by Shatir
Refer to this code, it might be useful.
The code fetches all custom options with their values
参考此代码,它可能有用。
代码获取所有自定义选项及其值
foreach ($_product->getOptions() as $value)
{
echo "<br/><strong>".$value->getTitle()."</strong><br/>";
$values = $value->getValues();// Getting Values if it has option values, case of select,dropdown,radio,multiselect
?>
<select id = "<?php echo 'select_'.$value->getId() ?>" name = "<?php echo 'options['.$value->getId() .']'?>">
<?php
foreach ($values as $val)
{
echo "<option price = " . $val->getPrice(). " value = ".$val->getOptionTypeId() . ">" .$val->getTitle()."</option>";
}
?>
</select>
<?php
$i++;
}
Note : The code outputs the custom options& their valuesjust as they would be required if they were to be used for adding the product to the cart.
You can remove the select if you just want to get the option values(to reduce the complexity of the code).
注意:代码输出自定义选项及其值,就像将它们用于将产品添加到购物车一样。
如果您只想获取选项值(以降低代码的复杂性),则可以删除选择。
回答by Zaheerabbas
you can try below code
你可以试试下面的代码
<?php if ($_product->getData('attribute_name')): ?>
<p><?php echo nl2br($_product->getResource()->getAttribute('attribute_name')
->getFrontend()->getValue($_product)) ?>
</p>

