[英]How to show product attributes in list.phtml (Magento) where the Stock Availability is “In Stock”
我找到了以下代碼:
$attributeCode = 'shoe_size';
$product = Mage::getModel('catalog/product');
$productCollection = Mage::getResourceModel('eav/entity_attribute_collection')
->setEntityTypeFilter($product->getResource()->getTypeId())
->addFieldToFilter('attribute_code', $attributeCode);
$attribute = $productCollection->getFirstItem()->setEntity($product->getResource());
print_r($attribute->getData()); // print out the available attributes
$options = $attribute->getSource()->getAllOptions(false);
print_r($options); // print out attribute options
問題是它會打印所有鞋子的尺碼。 我只想要那些“有貨”的
先感謝您!
<?php
if ($_product->isConfigurable()&&$_product->isSaleable()) {
$allProducts = $_product->getTypeInstance(true)->getUsedProducts(null, $_product);
$sizeArray = array();
foreach ($allProducts as $subproduct) {
if ($subproduct->isSaleable()) {
if($subproduct->getAttributeText('ATTRIBUTES')){
$sizeArray[] = $subproduct->getAttributeText('ATTRIBUTES');
}else{
$sizeArray[] = $subproduct->getAttributeText('size_shorts_and_capris');
}
}
}
echo $this->__('Sizes: ');
for ($x = count($sizeArray)-1; $x >= 0; $x--){
echo $sizeArray[$x];
if($x != 0)echo ', ';
}
}
?>
<span class="price hover-toggle">
<?php
if ($_product->isConfigurable()&&$_product->isSaleable()) {
$allProducts = $_product->getTypeInstance(true)->getUsedProducts(null, $_product);
$sizeArray = array();
foreach ($allProducts as $subproduct) {
if ($subproduct->isSaleable()) {
if($subproduct->getAttributeText('size')){
$sizeArray[] = $subproduct->getAttributeText('size');
}else{
$sizeArray[] = $subproduct->getAttributeText('size_shorts_and_capris');
}
}
}
echo $this->__('Sizes: ');
$customSizeArr = array();
$sizeOrder = array('S','M','L','XL','XXL');
if(!empty($sizeArray)){
foreach($sizeArray as $val){
if($val =='S')
$customSizeArr[0] = $val;
if($val =='M')
$customSizeArr[1] = $val;
if($val =='L')
$customSizeArr[2] = $val;
if($val =='XL')
$customSizeArr[3] = $val;
if($val =='XXL')
$customSizeArr[4] = $val;
}
}
ksort($customSizeArr);
echo (implode(", ",$customSizeArr));
}
?>
</span>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.