1

I'm having a hard time on how to display those configurable products which associated products are in stock only.

For example, if I have a configurable product and the quantity stock status of its associated products are out of stock, then when I create a custom product collection, that configurable product will not be displayed or will not be included in my query.

Here's my custom collection code:

<?php $attributeCode = 'violator_tags';                                    
 // load attribute                                                 
  $attribute = Mage::getSingleton('eav/config')->getAttribute('catalog_product', $attributeCode);                   
  $categoryLink = Mage::getModel("catalog/category")->load(146)->getUrl(); 
     //146 category ID of BestSeller                                     
     //get option id and label array                                       
  if ($attribute->usesSource()) {                                    
    $options = $attribute->getSource()->getAllOptions(false);               
  }                                                                 

  $products = Mage::getModel('catalog/product')
            ->getCollection()
                ->addAttributeToSelect('*')                    
                ->addAttributeToFilter(
                            array(
                                  array('attribute'=>$attributeCode, 'finset'=>132)
                            )
                        )
                ->addAttributeToFilter('status', 1);                       

  // show only enabled products   
        Mage::getSingleton('catalog/product_status')
        ->addVisibleFilterToCollection($products); 

        Mage::getSingleton('cataloginventory/stock')->addInStockFilterToCollection($products);                                      

 // show only visible products 
        Mage::getSingleton('catalog/product_visibility')
        ->addVisibleInCatalogFilterToCollection($products);                 
 ?>

Currently, it displays the configurable products with OUT OF STOCK associated products. Did I miss something?

Using Magento 1.9 CE Please help.

Thanks.

1 Answer 1

0

Please try this, code is not tested

$attributeCode = 'violator_tags';
$category = Mage::getModel('catalog/category')->load(Mage::app()->getStore()->getRootCategoryId());

$collection = $category->getProductCollection();

$collection->addAttributeToSelect('*')                    
            ->addAttributeToFilter(
                array(
                      array('attribute'=>$attributeCode, 'finset'=>132)
                )
            )

Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($collection);
Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($collection);
2
  • Hello! Thanks for the suggestion and I tried it but it didn't work. The configurable product with OUT OF STOCK associate products is still there. When I click the configurable product, this is the result. prnt.sc/euy69b
    – nosnevetzy
    Apr 11, 2017 at 1:39
  • I was just trying to display the products on the homepage. This is the result when I tried your suggested code. prnt.sc/euy70q . You can see that the configurable product is still there. It should be removed since there are NO IN STOCK associated products on it. Please help. Thanks!
    – nosnevetzy
    Apr 11, 2017 at 1:41

Your Answer

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

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