简体   繁体   English

如何在库存可用性为“有库存”的list.phtml(Magento)中显示产品属性

[英]How to show product attributes in list.phtml (Magento) where the Stock Availability is “In Stock”

I have found this code: 我找到了以下代码:

$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

The problem is it prints all the shoe sizes. 问题是它会打印所有鞋子的尺码。 I want only those which are "in stock" 我只想要那些“有货”的

Thank you in advance! 先感谢您!

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM