0

I want to show a custom_attribute of a simple product only.

I need this on the home page.

I want to foreach loop the 10 highest priced configurable products and show the lowest priced simple product custom_attribute

I can do it on the product page itself with its own simple products just fine with the following:

    if($_product->getTypeId() == "configurable"): ?>
    <?php 
        $childIds=Mage::getResourceSingleton('catalog/product_type_configurable')
            ->getChildrenIds($_product->getId());
        $simple_collection=Mage::getResourceModel('catalog/product_collection')->
        addAttributeToFilter('entity_id', array('in' =>  array_values($childIds[0])))
        ->addAttributeToSelect('*')->addAttributeToSort('price', 'asc');   
                // add  in stock to collection
        $cond = array(
            '{{table}}.use_config_manage_stock = 0 AND {{table}}.manage_stock=1 AND {{table}}.is_in_stock=1',
            '{{table}}.use_config_manage_stock = 0 AND {{table}}.manage_stock=0',
        );

            $cond[] = '{{table}}.use_config_manage_stock = 1 AND {{table}}.is_in_stock=1';

        $simple_collection->joinField(
            'inventory_in_stock',
            'cataloginventory/stock_item',
            'is_in_stock',
            'product_id=entity_id',
            '(' . join(') OR (', $cond) . ')'
        );

foreach($simple_collection as $simple_product){ 

        echo $sample_attribute
        echo $sample_attribute_2

}

1 Answer 1

2

Not sure I understood what you need, but you could try using somethig like this in your loop:

$top10 = Mage::getModel('catalog/product')->getCollection();
$top10
    ->addPriceData()
    ->addAttributeToFilter('type_id', Mage_Catalog_Model_Product_Type::TYPE_CONFIGURABLE)
    ->addAttributeToSort('price', Varien_Data_Collection::SORT_ORDER_DESC)
    ->setPageSize(10)
    ->setCurPage(1);

foreach ($top10 as $product) {   
    $simpleCollection = Mage::getModel('catalog/product_type_configurable')
        ->getUsedProductCollection($product);

    $simpleCollection
        ->addPriceData()
        ->addAttributeToSelect('*')
        ->addAttributeToSort('price', Varien_Data_Collection::SORT_ORDER_ASC);

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

    $lowestSimple = $simpleCollection->getFirstItem();
    echo $lowestSimple->getAttributeName();
}

2nd part of your question in comments (how to add category filter):

$category = Mage::getModel('catalog/category')->load($categoryId);

$top10 = Mage::getModel('catalog/product')->getCollection();
$top10
    ->addPriceData()
    ->addAttributeToFilter('type_id', Mage_Catalog_Model_Product_Type::TYPE_CONFIGURABLE)
    ->addAttributeToSort('price', Varien_Data_Collection::SORT_ORDER_DESC)
    ->addCategoryFilter($category)
    ->setPageSize(10)
    ->setCurPage(1);

foreach ($top10 as $product) {  
    $simpleCollection = Mage::getModel('catalog/product_type_configurable')
        ->getUsedProductCollection($product);

    $simpleCollection
        ->addPriceData()
        ->addAttributeToSelect('*')
        ->addAttributeToSort('price', Varien_Data_Collection::SORT_ORDER_ASC);

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

    $lowestSimple = $simpleCollection->getFirstItem();
    echo $lowestSimple->getAttributeName();
}
11
  • I cant include this into the code I have for the product page as it doesn't work on the homepage. I need it to be independent. I need to get the 10 highest priced products and show the custom attribute for the lowest price simple product of each of the 10 configurables Commented Feb 21, 2016 at 21:53
  • Ok, I modified my code, see if it works. I did not test it. Commented Feb 21, 2016 at 22:11
  • This is some amazing coding. Works great. thank you so much. Just one more thing. If I wanted to filter by category also, would this be possible? Commented Feb 21, 2016 at 22:38
  • See my addendum. Commented Feb 21, 2016 at 22:43
  • I added $categoryId = 2; and my custom_attribute but page isn't working. Commented Feb 21, 2016 at 23:10

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.