jcadima
6/8/2017 - 7:35 PM

Get list of products by Attribute value(dropdown)

Get list of products by Attribute value(dropdown)

http://blog.chapagain.com.np/magento-get-all-products-by-attribute-id-and-attribute-option/
https://stackoverflow.com/questions/11723959/magento-getproducturl-is-not-returning-the-right-url-random

<?php
// the 'Attribute Code'  is  'brand_label' which is a dropdown type with the following values
// these key values are taken from the html source after this is created
$brands = array(
	'5' => 'Argo',
	'4' => 'Cannon',
	'3' => 'Forus',
	'15' => 'Icare',
	'16' => 'Kowa',
	'7' => 'Optomol',
	'8' => 'Potec',
	'9' => 'Reichert',
	'9' => 'Sonomed Escalon',
	'11' => 'Shin-Nippon',
	'12' => 'Tomey',
	'13' => 'Vissio',	
	'14' => 'Hans Heiss',
	'6' => 'Welch Allyn'
) ;




<div>
<?php	
$attributeOptionId = 5; 
$attributeCode = 'brand_label';
 
$products = Mage::getModel('catalog/product')
                    ->getCollection()
                    ->addAttributeToSelect('*')
                    ->addAttributeToFilter($attributeCode, $attributeOptionId);
 
// show only enabled products
Mage::getSingleton('catalog/product_status')
            ->addVisibleFilterToCollection($products);
 
// show only visible products            
Mage::getSingleton('catalog/product_visibility')
            ->addVisibleInCatalogFilterToCollection($products);
             
// print all products' with attribute value 'Argo'
foreach ($products->getItems() as $product) {
    echo 'Product Name: ' . $product->getName() . '<br>' ;
    echo 'Product Price: ' . $product->getPrice()  .  '<br>';
    echo 'Product SKU: ' . $product->getSku() . '<br>' ;
    echo 'Product URL: ' . $product->getProductUrl() . '<br>' ;
    echo 'Product URL Path: ' . $product->getUrlPath() . '<br>' ;
    echo 'Product Img URL: ' . $product->getImageUrl() . '<br>' ; 
    echo '==========================================<br>'; 
}
?>                        
</div> 


<a href="<?php echo Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_LINK) . $product->getUrlPath() ; ?>">My Link</a>