Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/229.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何在list.phtml(Magento)中显示产品属性,其中库存可用性为;库存;_Php_Magento - Fatal编程技术网

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

Php 如何在list.phtml(Magento)中显示产品属性,其中库存可用性为;库存;,php,magento,Php,Magento,我发现了以下代码: $attributeCode = 'shoe_size'; $product = Mage::getModel('catalog/product'); $productCollection = Mage::getResourceModel('eav/entity_attribute_collection') ->setEntityTypeFilter($product->getResource()->getTypeId()) ->addField

我发现了以下代码:

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