如何在list.phtml(Magento)中显示产品属性,其中库存可用性是"库存"

时间:2014-09-21 18:34:46

标签: php magento

我找到了这段代码:

$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

问题是它会打印所有鞋码。我只想要“有货”的那些

提前谢谢!

2 个答案:

答案 0 :(得分:0)

<?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 ', ';
    }
}
?>

答案 1 :(得分:0)

<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>