1

I am trying to get a list of colors for the simple products in my store without showing the disabled ones. My function gives me all of the colors of products that are disabled or not.

    public function getSimpleColors($productId){

    $product = Mage::getModel('catalog/product')->load($productId);
    $configurable= Mage::getModel('catalog/product_type_configurable')->setProduct($product);
    $simpleCollection = $configurable->getUsedProductCollection()->addAttributeToSelect('*')->addFilterByRequiredOptions();
    $colors=array();

    foreach($simpleCollection as $simpleProduct){
        $colors[]= $simpleProduct->getAttributeText('color');
    }

    $colors=array_unique($colors);
    return $colors;

}

1 Answer 1

3

In this instead of using Mage::getModel('catalog/product_type_configurable') and getting child product color option.

You can use product collection resource model Mage::getResourceModel('catalog/product_collection') and filter that collection by that configurable child ids

Then group by that collection attribute color using function groupByAttribute($attributeCode). and you get your desire result

<?php
// get child ids in an array
$childIds=Mage::getResourceSingleton('catalog/product_type_configurable')

    ->getChildrenIds($_product->getId());

// Create a collection and filter child ids 

$collection =Mage::getResourceModel('catalog/product_collection')
        ->addAttributeToSelect('color')
      ->addAttributeToFilter('entity_id', array('in' =>  array_values($childIds[0])))
      ->addAttributeToFilter('status',
          array('eq' => Mage_Catalog_Model_Product_Status::STATUS_ENABLED));


// groupByAttribute($attributeCode)

  $collection->groupByAttribute('color');
  foreach($collection as $eachPro){
          $colors[]=$eachPro->getAttributeText('color');
  }

Note:

If catalog product flat enable then the collection does not contain disable product.For getting disable products in collection whenever flat enable,then use question: How to get disable product collection whenever flat enable at frontend

6
  • The core problem happens to be the indexing schema design is flawed... +1 Amit
    – mprototype
    Commented Oct 13, 2015 at 17:07
  • I still cant get just the enabled products. It still shows me all of the colors even if the simple it is associated to, is in fact disabled.
    – iPetey
    Commented Oct 13, 2015 at 19:07
  • add ->addAttributeToFilter( 'status', array('eq' => Mage_Catalog_Model_Product_Status::STATUS_DISABLED) ); to collection getting only enable simple products
    – Amit Bera
    Commented Oct 13, 2015 at 19:15
  • I just cant seem to get it to behave! still shows me the colors of products that are disabled. $collection =Mage::getResourceModel('catalog/product_collection') ->addAttributeToSelect('color')-> addAttributeToFilter('entity_id', array('in' => array_values($childIds[0])))->addAttributeToFilter('status', array('eq' => Mage_Catalog_Model_Product_Status::STATUS_DISABLED));
    – iPetey
    Commented Oct 13, 2015 at 19:55
  • my typo mistake Mage_Catalog_Model_Product_Status::STATUS_DISABLED should be Mage_Catalog_Model_Product_Status::STATUS_ENABLEcollection getting only enable simple products – Amit Bera 1 hour ago
    – Amit Bera
    Commented Oct 13, 2015 at 20:22

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.