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;
 }
- 
 Is the product attribute's able to be used in product listing "used_in_product_listing"?Raj– Raj2015年04月01日 07:46:18 +00:00Commented Apr 1, 2015 at 7:46
1 Answer 1
Your observer.php code does not render the html to list.phtml proper
If you observer code have create perform issue on list page. Basically code :
foreach ($products as $product) {
 $helper->getAttributes($product);
 }
create main issue for all collection twice,one at observer and another is list.phtml.
It will be good call Mage::helper('Campany_swatches')->getAttribute($_product); at list.phtml inside foreack loop of products ,this one line code is make all solution.
Explore related questions
See similar questions with these tags.