问题描述
我在显示产品自定义属性时遇到问题.我已经通过Google阅读了所有资源,但仍然没有成功.问题是我必须在类别网格和列表视图上显示可配置产品的大小属性.Google上的每个解决方案都建议类似
I have an issue with displaying product custom attributes.I've read every resources through google but still no success.The problem is that I have to show size attribute of configurable product on category grid and list view.Every solution on google suggested something like
$_product->getAttributeText('size')
但是我最后只得到一个字符串-"S"或"M"而不是数组.如何轻松获取属于特定可配置产品的所有简单产品的所有可能尺寸?
but I ended up at just a single string - "S" or "M" instead of an array.How can I fetch all possible sizes of all simple products which belongs to particular configurable product without much hassle?
更新
使用约瑟夫·马斯蒂(Joseph Mastey)提出的解决方案后,我遇到了另一个问题.我设法显示了给定属性的所有可能选项,但是现在我只需要显示可购买的这些选项.例如,如果L尺寸的T恤缺货或被禁用,则不应显示L选项.我该如何解决这个问题?
After using solution proposed by Joseph Mastey I encountered another problem. I managed to show all possible options for given attribute, but now I need to show only these options which are available to buy. For example if t-shirt size L is out of stock or is disabled, L option should not be shown. How can I solve this issue?
推荐答案
在处理可配置产品时(或在任何时候,您仅在针对一种类型的产品处理概念时,就像可配置属性一样),您可能会正在使用getTypeInstance
.如下所示,我获取了该产品的可配置属性,然后找到一个用于尺寸的属性.如果需要,您也可以仅遍历每个可配置的属性.或者,如果size是唯一可配置的属性,则跳过该if()
.
When dealing with configurable products (or any time you're dealing with a concept for only one type of product, as configurable attributes are), you'll probably be working with getTypeInstance
. See below, I grab the configurable attributes for the product, then find the one for size. You could also just run through every configurable attribute if you wanted. Or if size is the only configurable attribute, just skip that if()
.
$attrs = $_product->getTypeInstance(true)->getConfigurableAttributesAsArray($_product);
foreach($attrs as $attr) {
if(0 == strcmp("size", $attr['attribute_code'])) {
$options = $attr['values'];
foreach($options as $option) {
print "{$option['store_label']}<br />";
}
}
}
希望有帮助!谢谢,
乔
这篇关于Magento和可配置的产品属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!