1

I have two attribute sets say
i)Shirts ii)Pants
The configurable product options are
shirt_color/shirt_size
pant_color/pant_size
They have one thing in common.Same Attribute Labels

How to display only the available sizes for each configurable product if available? So far I tried..

<div class='divider'>
               <?php /*?> <?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)
            { 
             echo count($attributes["values"])." availables";
                ?>
                            <div class="super_attribute[<?php echo                                                                                               attributes['attribute_id'] ?>]" id="attribute<?php echo $attributes['attribute_id'] ?>">
                                <?php
                                foreach($attributes["values"] as $values)
                                {
                                    echo $values["label"];
                                }
                                ?>
                            </div>
                  <?php
            }
        }?><?php */?>

        <?php

         ?> </div>

My output was
Sample Confi g products with configurable options
I need to fetch only the available sizes.. Need a solution...

1 Answer 1

1
 <div class="available options">
<?php $cProduct = Mage::getModel('catalog/product')->load($_product->getId());?>
        <?php if ($cProduct->getData('type_id') == "configurable")
        {
 $configAttributesArray = $_product->getTypeInstance(true)-

>getConfigurableAttributesAsArray($_product);?>
        <div class="available-color">
        <?php
        //filter so only Color is left and select the values in the array
$configAttributesFilteredByColor = array_filter($configAttributesArray, function($v) { return $v['label'] == 'Color'; });
$configAttributesFilteredByColorValues = $configAttributesFilteredByColor[key($configAttributesFilteredByColor)]['values'];
if(count($configAttributesFilteredByColorValues)>1){
echo count($configAttributesFilteredByColorValues)." colors available";
}else 
echo "only ". count($configAttributesFilteredByColorValues)." color available";


?>
</div>
<div class="available-size">
            <?php
                //filter so only Size is left and select the values in the array
$configAttributesFilteredBySize = array_filter($configAttributesArray, function($v) { return $v['label'] == 'Size'; });
$configAttributesFilteredBySizeValues = $configAttributesFilteredBySize[key($configAttributesFilteredBySize)]['values'];
//get the configurable product its childproducts
$childProducts = Mage::getModel('catalog/product_type_configurable')->getUsedProducts(null,$_product);

//loop the values, and the childproducts and match them
echo 'Size: ';

foreach($configAttributesFilteredBySizeValues as $configAttributeValue){

    echo $configAttributeValue['label'] . ' ';
    $value = $configAttributeValue['value_index'];
    foreach($childProducts as $childProduct){
        if($childProduct->getSize() == $value){
            echo $childProduct->getAttributeText('Size') . ',';
        }
    }
}
}?>
1
  • 1
    feel free to accept your own answer so that the question does not show up as unanswered anymore Commented Dec 17, 2015 at 8:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.