I'm trying to display size shoes attribute for each product in product list (something like a native configurable swatches ) , But I got all sizes on the top of the page instead of getting them under each product name . Is there any way to create dynamic block and append it for each product or another alternative ?
I create a module and I'm using an observer to handle the collection , I don't like to have a bunch of code in list.phtml
my code is the following : observer.php
<?php
class Campany_Swatches_Model_Observer extends Mage_Core_Model_Abstract
{
public function productListCollectionLoadAfter(Varien_Event_Observer $observer)
{
$collection = $observer->getCollection();
$helper = Mage::helper('Campany_swatches');
$products = $collection->getItems();
/* @var $product Mage_Catalog_Model_Product */
foreach ($products as $product) {
$helper->getAttributes($product);
}
}
}
helper Data.php
<?php
class Campany_Swatches_Helper_Data extends Mage_Core_Helper_Abstract
{
private $_html;
public function getAttributes($_product = false)
{
if($_product->isConfigurable()){
//get attributes
$attributes = $_product->getTypeInstance(true)->getConfigurableAttributes($_product) ;
if(count($attributes)){
foreach($attributes as $att){
$pAtt = $att->getProductAttribute();
//get the product children
$allProducts = $_product->getTypeInstance(true)->getUsedProducts(null, $_product);
$frontValues =array();
if ($pAtt->getFrontendLabel() == "Shoe Size"){
foreach($allProducts as $p){
//check stock, status, ...
//do not show unsaleable options
if(!$p->isSaleable()) continue;
$out=$p->getAttributeText($pAtt->getName());
$frontValues[$out]=$out;
}
$this->_html = '<div class="shoeSize">';
$this->_html .= '<ul class="configurable-swatch-list configurable-swatch-size_clothes ulshoeSize clearfix">';
$this->_html .='<li class="swatch-label">';
$this->_html .= implode('</li><li class="swatch-label">', $frontValues);
$this->_html .='</li> </ul></div>';
}
}
}
}
return $this->_html;
}