获取可配置的产品属性标签和magento中当前产品的ID

时间:2015-06-02 10:16:41

标签: php magento product catalog

当我print_r $myArray时,它会显示color属性的所有标签..

我想只显示当前产品使用的那些属性(标签和ID)。同时我的网站正在使用一些自定义主题我希望启用默认的可配置选项与product.currently一起显示。

<?php 
 $attribute = Mage::getModel('eav/config')->getAttribute('catalog_product', 'color');
 foreach ($attribute->getSource()->getAllOptions(true, true) as $instance) 
 {
         $myArray[$instance['value']] = $instance['label']; 
 }
 print_r($myArray);
?>

1 个答案:

答案 0 :(得分:1)

此代码可能会有所帮助我也搜索完全相同并在另一个博客上找到它

<?php $cProduct = Mage::getModel('catalog/product')->load($_product->getId());
            //check if product is a configurable type or not
            if ($cProduct->getData('type_id') == "configurable")
            {
                //get the configurable data from the product
                $config = $cProduct->getTypeInstance(true);
                //loop through the attributes
                foreach($config->getConfigurableAttributesAsArray($cProduct) as $attributes)
                {
                    ?>
                    <dl>
                        <dt><label class="required"><em>*</em><?php echo $attributes["label"]; ?></label></dt>
                        <dd>
                            <div class="input-box">
                                <select name="super_attribute[<?php echo $attributes['attribute_id'] ?>]" id="attribute<?php echo $attributes['attribute_id'] ?>">
                                    <?php
                                    foreach($attributes["values"] as $values)
                                    {
                                        echo "<option>".$values["label"]."</option>";
                                    }
                                    ?>
                                </select>
                            </div>
                        </dd>
                    </dl>
                    <?php
                }
            }?> 
相关问题